summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Migrate/Flyspray.pm
blob: bed3d33053a96a165b684797798286c7cf9c0ede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Migrate::Flyspray;

use 5.14.0;
use strict;
use warnings;

use parent qw(Bugzilla::Migrate);

use Bugzilla::Constants;
use Bugzilla::Util qw(validate_email_syntax);
use Bugzilla::DB;

use IO::File;
use List::Util qw(first none);
use SQL::Abstract;
use File::MimeInfo;

# FIXME: this is currently broken in Bugzilla::Migrate
#use constant REQUIRED_MODULES => [
#{
#package => 'SQL-Abstract',
#module  => 'SQL::Abstract',
#version => 0,
#},
#];

use constant FIELD_MAP => {
  'BugID'          => 'bug_id',
  'Category'       => 'product',
  'Subject'        => 'short_desc',
  'Responsible'    => 'assigned_to',
  'State'          => 'bug_status',
  'Class'          => 'cf_type',
  'Classification' => '',
  'Originator'     => 'reporter',
  'CreationTS'     => 'creation_ts',
  'Last-Modified'  => 'delta_ts',
  'Release'        => 'version',
  'Severity'       => 'bug_severity',
  'comment'        => 'comment',
  'comments'       => 'comments',
  'history'        => 'history',
  'attachments'    => 'attachments',
};

use constant VALUE_MAP => {
  bug_status => {
    'unconfirmed'         => 'NEW',
    'new'                 => 'NEW',
    'unassigned'          => 'NEW',
    'assigned'            => 'CONFIRMED',
    'researching'         => 'CONFIRMED',
    'waiting on response' => 'WAITING',
    'requires testing'    => 'WAITING',
  },
  resolution => {
    'no response'      => 'NORESPONSE',
    'upstream'         => 'UPSTREAM',
    'not a bug'        => 'NOTABUG',
    'won\'t fix'       => 'WONTFIX',
    'won\'t implement' => 'WONTFIX',
    'works for me'     => 'WORKSFORME',
    'duplicate'        => 'DUPLICATE',
    'deferred'         => 'DEFERRED',
    'fixed'            => 'FIXED',
    'implemented'      => 'FIXED',
  },
  priority => {
    0 => "--",
    1 => "Low",
    2 => "Normal",
    3 => "High",
    4 => "Highest",
    5 => "Immediate",
    6 => "Flash",
  },
  bug_severity => {
    0 => "--",
    1 => "enhancement",
    2 => "minor",
    3 => "normal",
    4 => "major",
    5 => "blocker",
  },
};

use constant FLYSPRAY_CONFIG_VARS => (
  {
    name    => 'flyspray_db',
    default => 'bugzilla_flyspray',
    desc    => <<END,
# The database name of the flyspray databse.
END
  },
  {
    name    => 'flyspray_prefix',
    default => 'flyspray_',
    desc    => <<'END',
# The prefix of the flyspray database tables.
END
  },
  {
    name    => 'component_owner',
    default => 'bluewind@xinu.at',
    desc    => <<'END',
# The username of the user that is set as the initial owner for components.
END
  },
  {
    name    => 'unknown_user_email',
    default => 'nobody@localhost.localdomain',
    desc    => <<'END',
# The email address for the fallback user if a task is created by/assigned to an unknown user.
END
  },
  {
    name    => 'attachment_path',
    default => './flyspray-attachments',
    desc    => <<'END',
# The path of the flyspray attachments directory (or a copy).
END
  },
);

sub CONFIG_VARS {
  my $self      = shift;
  my @vars      = (FLYSPRAY_CONFIG_VARS, $self->SUPER::CONFIG_VARS);
  my $field_map = first { $_->{name} eq 'translate_fields' } @vars;
  $field_map->{default} = FIELD_MAP;
  my $value_map = first { $_->{name} eq 'translate_values' } @vars;
  $value_map->{default} = VALUE_MAP;
  return @vars;
}

use constant NON_COMMENT_FIELDS => qw(
);

sub new {
  my ($class) = @_;
  my $self = {};
  bless $self, $class;
  $self->{dbh} = Bugzilla->dbh;
  $self->{sql} = SQL::Abstract->new;
  return $self;
}

sub _select_fs {
  my ($self, $table, $fields, $where) = @_;

  my @fields = qw(category_name shot_in_list);
  my $db     = $self->config('flyspray_db');
  my ($stmt, @bind) = $self->{sql}->select(
    [
      map {
        sprintf("%s.%s%s",
          $self->config('flyspray_db'),
          $self->config('flyspray_prefix'), $_)
      } @$table
    ],
    $fields,
    $where
  );
  $self->debug($stmt,  2);
  $self->debug(\@bind, 2);
  my $sth = $self->{dbh}->prepare($stmt);
  $sth->execute(@bind);

  return $sth;
}

sub _read_users {
  my $self = shift;

  my $sth = $self->_select_fs(["users"],
    [qw(user_id user_name real_name email_address)]);

  my @result;
  while (my $row = $sth->fetchrow_hashref()) {
    if (!validate_email_syntax($row->{email_address})) {
      $self->debug("Skipping invalid email: " . $row->{email_address}, 3);
      next;
    }
    my $item = {
      login_name    => $row->{email_address},
      realname      => $row->{real_name},
      cryptpassword => "*",
    };
    push @result, $item;
    $self->{map_user_id_to_email}->{$row->{user_id}} = $item->{login_name};
  }

  # Remove duplicate users based on email; keep the latest account
  my %seen;
  @result = reverse grep { !$seen{$_->{login_name}}++ } reverse @result;

  # fallback user for cases where the real user can not be
  # created or has been deleted
  push @result,
    {
    login_name    => $self->config("unknown_user_email"),
    realname      => 'Unknown User',
    cryptpassword => "*",
    is_enabled    => 0,
    };
  $self->{map_user_id_to_email}->{-1} = $self->config("unknown_user_email");
  $self->{map_email_to_name}->{$self->config("unknown_user_email")}
    = 'UnknownUser';

  $self->debug(\@result, 3);

  #$self->debug([grep { $_->{email} eq 'xx@x.com'  } @result], 2);
  $self->debug("Migrating " . (@result + 0) . " users");
  return \@result;
}

sub _read_products {
  my $self = shift;

  my $sth = $self->_select_fs(["projects"],
    [qw(project_id project_title intro_message)]);

  my @result;
  while (my $row = $sth->fetchrow_hashref()) {
    push @result,
      {
      name        => $row->{project_title},
      description => $row->{intro_message} || "N/A",
      isactive    => 1,
      components  => $self->_get_fs_project_components($row->{project_id}),
      version     => "unspecified",
      };
    $self->{map_project_id_to_name}->{$row->{project_id}} = $row->{project_title};
  }

  $self->debug(\@result, 3);
  $self->debug("Migrating " . (@result + 0) . " products");
  return \@result;
}

sub _get_fs_project_components {
  my $self       = shift;
  my $project_id = shift;

  my $sth = $self->_select_fs(
    ["list_category"],
    [qw(category_id category_name show_in_list)],
    {project_id => $project_id}
  );

  my @result;
  while (my $row = $sth->fetchrow_hashref()) {
    push @result,
      {
      name         => $row->{category_name},
      description  => "N/A",
      initialowner => $self->config("component_owner"),
      isactive     => 1,
      };
    $self->{map_component_id_to_name}->{$row->{category_id}}
      = $row->{category_name};
    $self->{project_id_components}->{$project_id}->{$row->{category_name}} = 1;
  }
  push @result,
    {
    name         => "Uncategorized",
    description  => "N/A",
    initialowner => $self->config("component_owner"),
    isactive     => 1,
    };
  $self->{map_component_id_to_name}->{-1} = "Uncategorized";

  $self->debug(\@result, 3);
  $self->debug("Found " . (@result + 0) . " components in project $project_id");
  return \@result;
}

sub _read_bugs {
  my $self   = shift;
  my $fsdb   = $self->config("flyspray_db");
  my $fsprfx = $self->config("flyspray_prefix");

#my $sth = $self->_select_fs([qw(tasks users)], [qw(task_id product_category project_id item_summary detailed_desc user_name)], {'tasks.opened_by' => \' = users.user_id' });
  my $sth = $self->{dbh}->prepare("
		SELECT
			t.task_id,
			t.product_category,
			t.project_id,
			t.item_summary,
			t.detailed_desc,
			t.opened_by,
			t.task_priority,
			t.task_severity,
			t.item_status,
			t.date_opened,
			t.last_edited_time,
			a.user_id assigned_to,
			ls.status_name,
			t.is_closed,
			lr.resolution_name,
			lv.version_name
		FROM $fsdb.${fsprfx}tasks t
		LEFT OUTER JOIN $fsdb.${fsprfx}assigned a ON a.task_id = t.task_id
		LEFT OUTER JOIN $fsdb.${fsprfx}list_status ls ON ls.status_id = t.item_status
		LEFT OUTER JOIN $fsdb.${fsprfx}list_resolution lr ON lr.resolution_id = t.resolution_reason
		LEFT OUTER JOIN $fsdb.${fsprfx}list_version lv ON lv.version_id = t.product_version
		GROUP BY t.task_id, a.task_id
		");
  $sth->execute();

  #WHERE t.task_id IN (21668, 22102, 23845, 23884)

  my @result;
  while (my $row = $sth->fetchrow_hashref()) {
    my $bug = {
      short_desc => $row->{item_summary},
      product    => $self->{map_project_id_to_name}->{$row->{project_id}},
      component  => $self->{map_component_id_to_name}->{$row->{product_category}}
        // $self->{map_component_id_to_name}->{-1},
      bug_id       => $row->{task_id},
      comments     => [],
      history      => [],
      attachments  => [],
      assigned_to  => $self->_get_username($row->{assigned_to}),
      op_sys       => "Linux",
      rep_platform => "PC",
      bug_status   => $row->{status_name} // "Unknown",
      bug_severity => $row->{task_severity},
      priority     => $row->{task_priority},
      resolution   => $row->{is_closed} ? $row->{resolution_name} // 'UNKNOWN' : '',
      reporter     => $self->_get_username($row->{opened_by}),
      creation_ts  => $row->{date_opened},
      delta_ts     => $row->{last_edited_time},
      version      => $row->{version_name} // "unspecified",
      comment      => $row->{detailed_desc},
    };
    $bug->{version}    = "unspecified" if $bug->{version} eq "None";
    $bug->{resolution} = "UNSPECIFIED" if $bug->{resolution} eq "None";
    $bug->{bug_status} = "RESOLVED"    if $bug->{resolution} ne "";
    $bug->{component}  = $self->{map_component_id_to_name}->{-1}
      if not defined $self->{project_id_components}->{$row->{project_id}}
      ->{$bug->{component}};

    # handle initial comment/description
    if (length($bug->{comment}) > 65535) {
      $bug->{comment}
        = "--- The description has been moved to an attachment because it is longer than 65535 characters ---";
      unshift @{$bug->{attachments}},
        {
        submitter   => $bug->{reporter},
        data        => $row->{detailed_desc},
        description => "Initial bug description",
        mimetype    => "text/plain",
        filename    => "description.txt",
        creation_ts => $self->parse_date($row->{date_opened}),
        };
    }

    # CC/bug watchers
    my $cc_sth = $self->{dbh}->prepare_cached("
			SELECT user_id
			FROM $fsdb.${fsprfx}notifications WHERE task_id = ?");
    $cc_sth->execute($row->{task_id});
    while (my $row = $cc_sth->fetchrow_hashref()) {
      push @{$bug->{cc}}, $self->_get_username($row->{user_id});
    }


    my $comments = {};

    # fetch comments
    my $comments_sth = $self->{dbh}->prepare_cached("
			SELECT comment_id, user_id, date_added, comment_text
			FROM $fsdb.${fsprfx}comments WHERE task_id = ?");
    $comments_sth->execute($row->{task_id});
    while (my $comment = $comments_sth->fetchrow_hashref()) {
      my $comment_item = {
        who      => $self->_get_username($comment->{user_id}),
        bug_when => $self->parse_date($comment->{date_added}),
        thetext  => $comment->{comment_text},
      };
      push @{$bug->{comments}}, $comment_item;
      $comments->{$comment->{comment_id}} = $comment_item;
    }

    # get history
    my $history_sth = $self->{dbh}->prepare_cached("
			SELECT user_id,
				event_type,
				event_date,
				field_changed,
				old_value,
				new_value,
				lr.resolution_name,
				lsn.status_name status_name_new,
				lso.status_name status_name_old
			FROM $fsdb.${fsprfx}history
			LEFT OUTER JOIN $fsdb.${fsprfx}list_resolution lr ON lr.resolution_id = new_value
			LEFT OUTER JOIN $fsdb.${fsprfx}list_status lsn ON lsn.status_id = new_value
			LEFT OUTER JOIN $fsdb.${fsprfx}list_status lso ON lso.status_id = old_value
			WHERE task_id = ?");
    $history_sth->execute($row->{task_id});
    while (my $history = $history_sth->fetchrow_hashref()) {
      if ($history->{event_type} == 2) {

        # type 2 -> closed bug
        my $closure_comment = $history->{old_value};
        if ($closure_comment ne "") {
          push @{$bug->{comments}},
            {
            who      => $self->_get_username($history->{user_id}),
            bug_when => $self->parse_date($history->{event_date}),
            thetext  => $closure_comment,
            };
        }

        push @{$bug->{history}},
          {
          who      => $self->_get_username($history->{user_id}),
          bug_when => $self->parse_date($history->{event_date}),
          field    => "resolution",
          removed  => "",
          added    => $history->{resolution_name},
          };
        if ($self->translate_value('resolution', $history->{resolution_name} // "") eq
          "DUPLICATE")
        {
          if (
            $self->translate_value('resolution', $bug->{resolution} // "") eq "DUPLICATE"
            and $closure_comment =~ m/FS#(\d+)/)
          {
            push @{$self->{dupes}},
              {
              dupe_of  => int($1),
              dupe     => $row->{task_id},
              who      => $self->_get_username($history->{user_id}),
              bug_when => $self->parse_date($history->{event_date}),
              };

            push @{$bug->{comments}},
              {
              type       => CMT_DUPE_OF,
              extra_data => int($1),
              who        => $self->_get_username($history->{user_id}),
              bug_when   => $self->parse_date($history->{event_date}),
              };

            push @{$self->{bug_map}->{int($1)}->{comments}},
              {
              type       => CMT_HAS_DUPE,
              extra_data => $row->{task_id},
              who        => $self->_get_username($history->{user_id}),
              bug_when   => $self->parse_date($history->{event_date}),
              }
              if defined $self->{bug_map}->{int($1)};
          }
        }
      }
      elsif ($history->{event_type} == 3) {

        # type 3 -> field changed
        if ($history->{field_changed} eq "item_status") {
          push @{$bug->{history}},
            {
            who      => $self->_get_username($history->{user_id}),
            bug_when => $self->parse_date($history->{event_date}),
            field    => "bug_status",
            removed  => $history->{status_name_old} // "",
            added    => $history->{status_name_new},
            };
        }

        # TODO Add more field type mappings
      }

      # TODO Add more events?
      # optional for now: CC changes, assignee changes, milestone/version changes
    }

    # TODO get attachments
    my $attachments_sth = $self->{dbh}->prepare_cached("
			SELECT task_id, orig_name, file_name, file_desc, file_type, file_size, added_by, date_added
			FROM $fsdb.${fsprfx}attachments
			WHERE task_id = ?
			");
    $attachments_sth->execute($row->{task_id});
    my $attpath = $self->config("attachment_path");
    while (my $attachment = $attachments_sth->fetchrow_hashref()) {
      my $path = $attpath . '/' . $attachment->{file_name};
      next unless -f $path;
      next unless -s $path;
      my $attachment_item = {
        submitter   => $self->_get_username($attachment->{added_by}),
        creation_ts => $self->parse_date($attachment->{date_added}),
        description => $attachment->{file_desc} || $attachment->{orig_name},
        filename    => $attachment->{orig_name},

        #mimetype => $attachment->{file_type},
        ispatch   => $attachment->{file_type} =~ m/^text\/x-diff/ ? 1 : 0,
        data_path => $path,
      };

#$attachment_item->{mimetype} = mimetype($path) if $attachment_item->{mimetype} eq "" or $attachment_item->{mimetype} !~ m/.+\/.+/;
      $attachment_item->{mimetype} = mimetype($path);

      # TODO: set extra_data and type of comment when attachment is part of a comment.
      # problem: how to get attachment id? not yet in db
      # change _insert_attachments/_insert_comments?
      push @{$bug->{attachments}}, $attachment_item;

      #$self->debug($bug);
      #die if $attachment->{date_added} == 1110715864;
    }

    $self->{bug_map}->{$bug->{bug_id}} = $bug;

    push @result, $bug;
  }

  #$self->debug(\@result, 3);
  #@result = grep { $_->{bug_id} == 56677  } @result;
  #$self->debug([grep { $_->{bug_id} == 56677  } @result], 2);
  #$self->debug([keys %{{map { $_->{version} => 1 } @result}}], 2);
  #die;
  $self->debug("Migrating " . (@result + 0) . " bugs");

  # Ensure bug IDs are kept by preventing the validator from unsetting them
  my $orig_validator = \&Bugzilla::Bug::run_create_validators;
  *{Bugzilla::Bug::run_create_validators} = sub {
    my ($class, $params) = @_;
    my $bug_id = $params->{bug_id};
    my $ret    = $orig_validator->(@_);
    $ret->{bug_id} = $bug_id;
    return $ret;
  };

  # insert_create_data is not implemented in Bugzilla::Bug
  #my $orig_create = \&Bugzilla::Bug::insert_create_data;
  my $orig_create = \&Bugzilla::Object::insert_create_data;
  *{Bugzilla::Bug::insert_create_data} = sub {
    my ($class, $params) = @_;
    my $bug_id = $params->{bug_id};
    $orig_create->(@_);

# XXX: The next lines are copied from Bugzilla::Bug::insert_create_data and may need to be adjusted in the future!
# XXX: They also trigger hooks/audit again which is not really nice
    my $object = $class->new($bug_id);
    Bugzilla::Hook::process('object_end_of_create',
      {class => $class, object => $object});
    $object->audit_log(AUDIT_CREATE) if $object->AUDIT_CREATES;
    return $object;
  };

  return \@result;
}

sub _get_username {
  my ($self, $userid) = @_;
  my $email = $self->{map_user_id_to_email}->{$userid // -1}
    // $self->{map_user_id_to_email}->{-1};
  $self->{email_to_keep}->{$email} = 1;
  return $email;
}


sub translate_bug {
  my ($self, $fields) = @_;

  my ($bug, $other_fields) = $self->SUPER::translate_bug($fields);

  #TODO remove this function if not used

  return wantarray ? ($bug, $other_fields) : $bug;
}

sub after_read {
  my ($self) = @_;

  # only migrate users that have comments/attachments/are used somewhere
  $self->{users} = [grep { defined $self->{email_to_keep}->{$_->{login_name}} }
      @{$self->{users}}];
}

sub after_insert {
  my ($self) = @_;

  $self->debug("Marking duplicate bugs");
  $self->debug($self->{dupes}, 3);
  for my $entry (@{$self->{dupes}}) {
    my $dupeOf_bug = Bugzilla::Bug->new($entry->{dupe_of});
    $self->{dbh}->do("INSERT INTO duplicates (dupe_of, dupe) VALUES (?, ?)",
      undef, $entry->{dupe_of}, $entry->{dupe})
      unless defined $dupeOf_bug->{error};
  }
}


# Timestamps from flyspray are unix timestamps which Date::Parse
# can't parse so the original parse_date() sub will just die
sub parse_date {
  my ($self, $date) = @_;
  my $dt = DateTime->from_epoch(epoch => $date);
  $dt->set_time_zone(Bugzilla->local_timezone);
  return $dt->iso8601;
}

1;