You need to sign in or sign up before continuing.
importxml.pl 21.8 KB
Newer Older
1
#!/usr/bin/perl -w 
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
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Dawn Endico <endico@mozilla.org>


# This script reads in xml bug data from standard input and inserts 
# a new bug into bugzilla. Everything before the beginning <?xml line
# is removed so you can pipe in email messages.

use strict;

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#####################################################################
#
# This script is used import bugs from another installation of bugzilla.
# Moving a bug on another system will send mail to an alias provided by
# the administrator of the target installation (you). Set up an alias
# similar to the one given below so this mail will be automatically 
# run by this script and imported into your database.  Run 'newaliases'
# after adding this alias to your aliases file. Make sure your sendmail
# installation is configured to allow mail aliases to execute code. 
#
# bugzilla-import: "|/usr/bin/perl /opt/bugzilla/importxml.pl"
#
#####################################################################


45 46 47
# figure out which path this script lives in. Set the current path to
# this and add it to @INC so this will work when run as part of mail
# alias by the mailer daemon
48 49 50 51 52
# since "use lib" is run at compile time, we need to enclose the
# $::path declaration in a BEGIN block so that it is executed before
# the rest of the file is compiled.
BEGIN {
 $::path = $0;
53 54
 $::path =~ m#(.*)/[^/]+#;
 $::path = $1;
55 56
 $::path ||= '.';  # $0 is empty at compile time.  This line will
                   # have no effect on this script at runtime.
57
}
58

59 60
chdir $::path;
use lib ($::path);
61

62
use Bugzilla;
63
use Bugzilla::Config qw(:DEFAULT $datadir);
64
use Bugzilla::BugMail;
65

66 67 68
use XML::Parser;
use Data::Dumper;
$Data::Dumper::Useqq = 1;
69
use Bugzilla::BugMail;
70
use Bugzilla::User;
71 72 73

require "globals.pl";

74
GetVersionTable();
75

76

77 78
sub sillyness {
    my $zz;
79
    $zz = $Data::Dumper::Useqq;
80
    $zz = %::versions;
81
    $zz = %::keywordsbyname;
82 83 84 85 86 87 88 89 90
    $zz = @::legal_bug_status;
    $zz = @::legal_opsys;
    $zz = @::legal_platform;
    $zz = @::legal_priority;
    $zz = @::legal_severity;
    $zz = @::legal_resolution;
    $zz = %::target_milestone;
}

91 92 93
# XML::Parser automatically unquotes characters when it
# parses the XML, so this routine shouldn't be needed
# for anything (see bug 109530).
94 95 96 97
sub UnQuoteXMLChars {
    $_[0] =~ s/&amp;/&/g;
    $_[0] =~ s/&lt;/</g;
    $_[0] =~ s/&gt;/>/g;
98 99
    $_[0] =~ s/&apos;/'/g;  # ' # Darned emacs colors
    $_[0] =~ s/&quot;/"/g;  # " # Darned emacs colors
100 101 102 103
#    $_[0] =~ s/([\x80-\xFF])/&XmlUtf8Encode(ord($1))/ge;
    return($_[0]);
}

104 105 106 107 108 109 110
sub MailMessage {
  my $subject = shift @_;
  my $message = shift @_;
  my @recipients = @_;

  my $to = join (", ", @recipients);
  my $header = "To: $to\n";
111 112 113
  my $from = Param("moved-from-address");
  $from =~ s/@/\@/g;
  $header.= "From: Bugzilla <$from>\n";
114 115
  $header.= "Subject: $subject\n\n";

116 117
  my $sendmessage = $header . $message . "\n";
  Bugzilla::BugMail::MessageToMTA($sendmessage);
118 119 120
}


121 122 123 124 125 126 127 128 129
my $xml;
while (<>) {
 $xml .= $_;
}
# remove everything in file before xml header (i.e. remove the mail header)
$xml =~ s/^.+(<\?xml version.+)$/$1/s;

my $parser = new XML::Parser(Style => 'Tree');
my $tree = $parser->parse($xml);
130
my $dbh = Bugzilla->dbh;
131 132 133 134 135

my $maintainer;
if (defined $tree->[1][0]->{'maintainer'}) {
  $maintainer= $tree->[1][0]->{'maintainer'}; 
} else {
136 137 138
  my $subject = "Bug import error: no maintainer";
  my $message = "Cannot import these bugs because no maintainer for "; 
  $message .=   "the exporting db is given.\n";
139
  $message .=   "\n\nPlease re-open the original bug.\n";
140 141 142
  $message .= "\n\n$xml";
  my @to = (Param("maintainer"));
  MailMessage ($subject, $message, @to);
143 144 145 146 147 148 149
  exit;
}

my $exporter;
if (defined $tree->[1][0]->{'exporter'}) {
  $exporter = $tree->[1][0]->{'exporter'};
} else {
150 151
  my $subject = "Bug import error: no exporter";
  my $message = "Cannot import these bugs because no exporter is given.\n";
152
  $message .=   "\n\nPlease re-open the original bug.\n";
153 154 155 156 157 158
  $message .= "\n\n$xml";
  my @to = (Param("maintainer"), $maintainer);
  MailMessage ($subject, $message, @to);
  exit;
}

159 160 161 162 163 164 165 166 167 168 169

unless ( Param("move-enabled") ) {
  my $subject = "Error: bug importing is disabled here";
  my $message = "Cannot import these bugs because importing is disabled\n";
  $message .= "at this site. For more info, contact ";
  $message .=  Param("maintainer") . ".\n";
  my @to = (Param("maintainer"), $maintainer, $exporter);
  MailMessage ($subject, $message, @to);
  exit;
}

170
my $exporterid = login_to_id($exporter);
171 172 173 174
if ( ! $exporterid ) {
  my $subject = "Bug import error: invalid exporter";
  my $message = "The user <$tree->[1][0]->{'exporter'}> who tried to move\n";
  $message .= "bugs here does not have an account in this database.\n";
175
  $message .= "\n\nPlease re-open the original bug.\n";
176
  $message .= "\n\n$xml";
177
  my @to = (Param("maintainer"), $maintainer, $exporter);
178
  MailMessage ($subject, $message, @to);
179 180 181 182 183 184 185
  exit;
}

my $urlbase;
if (defined $tree->[1][0]->{'urlbase'}) {
  $urlbase= $tree->[1][0]->{'urlbase'}; 
} else {
186 187
  my $subject = "Bug import error: invalid exporting database";
  my $message = "Cannot import these bugs because the name of the exporting db was not given.\n";
188
  $message .= "\n\nPlease re-open the original bug.\n";
189 190 191
  $message .= "\n\n$xml";
  my @to = (Param("maintainer"), $maintainer, $exporter);
  MailMessage ($subject, $message, @to);
192 193 194 195
  exit;
}
  

196
my $bugqty = ($#{$tree->[1]} +1 -3) / 4;
197
my $log = "Imported $bugqty bug(s) from $urlbase,\n  sent by $exporter.\n\n";
198 199 200 201
for (my $k=1 ; $k <= $bugqty ; $k++) {
  my $cur = $k*4;

  if (defined $tree->[1][$cur][0]->{'error'}) {
202 203
    $log .= "\nError in bug $tree->[1][$cur][4][2]\@$urlbase:";
    $log .= " $tree->[1][$cur][0]->{'error'}\n";
204
    if ($tree->[1][$cur][0]->{'error'} =~ /NotFound/) {
205 206
      $log .= "$exporter tried to move bug $tree->[1][$cur][4][2] here";
      $log .= " but $urlbase reports that this bug does not exist.\n"; 
207
    } elsif ( $tree->[1][$cur][0]->{'error'} =~ /NotPermitted/) {
208 209 210
      $log .= "$exporter tried to move bug $tree->[1][$cur][4][2] here";
      $log .= " but $urlbase reports that $exporter does not have access";
      $log .= " to that bug.\n";
211 212 213 214 215 216 217 218 219 220
    }
    next;
  }

  my %multiple_fields;
  foreach my $field (qw (dependson cc long_desc blocks)) {
    $multiple_fields{$field} = "x"; 
  }
  my %all_fields;
  foreach my $field (qw (dependson product bug_status priority cc version 
221
      bug_id rep_platform short_desc assigned_to bug_file_loc resolution
222
      delta_ts component reporter urlbase target_milestone bug_severity 
223
      creation_ts qa_contact keywords status_whiteboard op_sys blocks)) {
224 225
    $all_fields{$field} = "x"; 
  }
226 227
 
 
228 229
  my %bug_fields;
  my $err = "";
230
  for (my $i=3 ; $i < $#{$tree->[1][$cur]} ; $i=$i+4) {
231 232 233 234 235
    if (defined $multiple_fields{$tree->[1][$cur][$i]}) {
      if (defined $bug_fields{$tree->[1][$cur][$i]}) {
        $bug_fields{$tree->[1][$cur][$i]} .= " " .  $tree->[1][$cur][$i+1][2];
      } else {
        $bug_fields{$tree->[1][$cur][$i]} = $tree->[1][$cur][$i+1][2];
236
      }
237 238
    } elsif (defined $all_fields{$tree->[1][$cur][$i]}) {
      $bug_fields{$tree->[1][$cur][$i]} = $tree->[1][$cur][$i+1][2];
239
    } else {
240 241 242 243 244 245
      $err .= "---\n";
      $err .= "Unknown bug field \"$tree->[1][$cur][$i]\"";
      $err .= " encountered while moving bug\n";
      $err .= "<$tree->[1][$cur][$i]>";
      if (defined $tree->[1][$cur][$i+1][3]) {
        $err .= "\n";
246
        for (my $j=3 ; $j < $#{$tree->[1][$cur][$i+1]} ; $j=$j+4) {
247 248 249 250 251 252 253 254
          $err .= "  <". $tree->[1][$cur][$i+1][$j] . ">";
          $err .= " $tree->[1][$cur][$i+1][$j+1][2] ";
          $err .= "</". $tree->[1][$cur][$i+1][$j] . ">\n";
        }
      } else {
        $err .= " $tree->[1][$cur][$i+1][2] ";
      }
      $err .= "</$tree->[1][$cur][$i]>\n";
255 256 257
    }
  }

258
  my @long_descs;
259
  for (my $i=3 ; $i < $#{$tree->[1][$cur]} ; $i=$i+4) {
260 261 262 263 264 265 266
    if ($tree->[1][$cur][$i] =~ /long_desc/) {
      my %long_desc;
      $long_desc{'who'} = $tree->[1][$cur][$i+1][4][2];
      $long_desc{'bug_when'} = $tree->[1][$cur][$i+1][8][2];
      $long_desc{'thetext'} = $tree->[1][$cur][$i+1][12][2];
      push @long_descs, \%long_desc;
    }
267 268
  }

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  # instead of giving each comment its own item in the longdescs
  # table like it should have, lets cat them all into one big
  # comment otherwise we would have to lie often about who
  # authored the comment since commenters in one bugzilla probably
  # don't have accounts in the other one.
  sub by_date {my @a; my @b; $a->{'bug_when'} cmp $b->{'bug_when'}; }
  my @sorted_descs = sort by_date @long_descs;
  my $long_description = "";
  for (my $z=0 ; $z <= $#sorted_descs ; $z++) {
    unless ( $z==0 ) {
      $long_description .= "\n\n\n------- Additional Comments From ";
      $long_description .= "$sorted_descs[$z]->{'who'} "; 
      $long_description .= "$sorted_descs[$z]->{'bug_when'}"; 
      $long_description .= " ----\n\n";
    }
284
    $long_description .=  $sorted_descs[$z]->{'thetext'};
285
    $long_description .=  "\n";
286 287
  }

288 289
  my $comments;

290
  $comments .= "\n\n------- Bug moved to this database by $exporter "; 
291 292 293 294 295 296 297 298 299 300 301 302 303 304
  $comments .= time2str("%Y-%m-%d %H:%M", time);
  $comments .= " -------\n\n";
  $comments .= "This bug previously known as bug $bug_fields{'bug_id'} at ";
  $comments .= $urlbase . "\n";
  $comments .= $urlbase . "show_bug.cgi?";
  $comments .= "id=" . $bug_fields{'bug_id'} . "\n";
  $comments .= "Originally filed under the $bug_fields{'product'} ";
  $comments .= "product and $bug_fields{'component'} component.\n";
  if (defined $bug_fields{'dependson'}) {
    $comments .= "Bug depends on bug(s) $bug_fields{'dependson'}.\n";
  }
  if (defined $bug_fields{'blocks'}) {
  $comments .= "Bug blocks bug(s) $bug_fields{'blocks'}.\n";
  }
305

306 307
  my @query = ();
  my @values = ();
308
  foreach my $field ( qw(creation_ts status_whiteboard) ) {
309
      if ( (defined $bug_fields{$field}) && ($bug_fields{$field}) ){
310 311
        push (@query, "$field");
        push (@values, SqlQuote($bug_fields{$field}));
312 313
      }
  }
314

315 316 317 318 319 320 321 322
  push (@query, "delta_ts");
  if ( (defined $bug_fields{'delta_ts'}) && ($bug_fields{'delta_ts'}) ){
      push (@values, SqlQuote($bug_fields{'delta_ts'}));
  }
  else {
      push (@values, "NOW()");
  }

323
  if ( (defined $bug_fields{'bug_file_loc'}) && ($bug_fields{'bug_file_loc'}) ){
324
      push (@query, "bug_file_loc");
325
      push (@values, SqlQuote($bug_fields{'bug_file_loc'}));
326 327
      }

328
  if ( (defined $bug_fields{'short_desc'}) && ($bug_fields{'short_desc'}) ){
329
      push (@query, "short_desc");
330
      push (@values, SqlQuote($bug_fields{'short_desc'}) );
331
      }
332

333 334 335 336 337 338 339 340 341 342 343 344 345 346

  my $prod;
  my $comp;
  my $default_prod = Param("moved-default-product");
  my $default_comp = Param("moved-default-component");
  if ( (defined ($bug_fields{'product'})) &&
       (defined ($bug_fields{'component'})) ) {
     $prod = $bug_fields{'product'};
     $comp = $bug_fields{'component'};
  } else {
     $prod = $default_prod;
     $comp = $default_comp;
  }

347
  # XXX - why are these arrays??
348
  my @product;
349
  my @component;
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
  my $prod_id;
  my $comp_id;

  # First, try the given product/component
  $prod_id = get_product_id($prod);
  $comp_id = get_component_id($prod_id, $comp) if $prod_id;

  if ($prod_id && $comp_id) {
      $product[0] = $prod;
      $component[0] = $comp;
  } else {
      # Second, try the defaults
      $prod_id = get_product_id($default_prod);
      $comp_id = get_component_id($prod_id, $default_comp) if $prod_id;
      if ($prod_id && $comp_id) {
          $product[0] = $default_prod;
          $component[0] = $default_comp;
      }
  }

  if ($prod_id && $comp_id) {
    push (@query, "product_id");
    push (@values, $prod_id );
    push (@query, "component_id");
    push (@values, $comp_id );
375
  } else {
376 377 378 379 380 381 382 383 384 385
    my $subject = "Bug import error: invalid default product or component";
    my $message = "Cannot import these bugs because an invalid default ";
    $message .= "product and/or component was defined for the target db.\n";
    $message .= Param("maintainer") . " needs to fix the definitions of ";
    $message .= "moved-default-product and moved-default-component.\n";
    $message .= "\n\nPlease re-open the original bug.\n";
    $message .= "\n\n$xml";
    my @to = (Param("maintainer"), $maintainer, $exporter);
    MailMessage ($subject, $message, @to);
    exit;
386
  }
387

388
  if (defined  ($::versions{$product[0]} ) &&
389 390
     (my @version = grep(lc($_) eq lc($bug_fields{'version'}), 
                         @{$::versions{$product[0]}})) ){
391 392
    push (@values, SqlQuote($version[0]) );
    push (@query, "version");
393
  } else {
394
    push (@query, "version");
395
    push (@values, SqlQuote($::versions{$product[0]}->[0]));
396
    $err .= "Unknown version $bug_fields{'version'} in product $product[0]. ";
397
    $err .= "Setting version to \"$::versions{$product[0]}->[0]\".\n";
398
  }
399

400
  if (defined ($bug_fields{'priority'}) &&
401
       (my @priority = grep(lc($_) eq lc($bug_fields{'priority'}), @::legal_priority)) ){
402 403
    push (@values, SqlQuote($priority[0]) );
    push (@query, "priority");
404
  } else {
405 406
    push (@values, SqlQuote("P3"));
    push (@query, "priority");
407 408 409 410
    $err .= "Unknown priority ";
    $err .= (defined $bug_fields{'priority'})?$bug_fields{'priority'}:"unknown";
    $err .= ". Setting to default priority \"P3\".\n";
  }
411

412
  if (defined ($bug_fields{'rep_platform'}) &&
413
       (my @platform = grep(lc($_) eq lc($bug_fields{'rep_platform'}), @::legal_platform)) ){
414 415
    push (@values, SqlQuote($platform[0]) );
    push (@query, "rep_platform");
416
  } else {
417 418
    push (@values, SqlQuote("Other") );
    push (@query, "rep_platform");
419 420 421 422 423
    $err .= "Unknown platform ";
    $err .= (defined $bug_fields{'rep_platform'})?
                     $bug_fields{'rep_platform'}:"unknown";
    $err .= ". Setting to default platform \"Other\".\n";
  }
424

425
  if (defined ($bug_fields{'op_sys'}) &&
426
     (my @opsys = grep(lc($_) eq lc($bug_fields{'op_sys'}), @::legal_opsys)) ){
427 428
    push (@values, SqlQuote($opsys[0]) );
    push (@query, "op_sys");
429
  } else {
430 431
    push (@values, SqlQuote("other"));
    push (@query, "op_sys");
432 433 434 435
    $err .= "Unknown operating system ";
    $err .= (defined $bug_fields{'op_sys'})?$bug_fields{'op_sys'}:"unknown";
    $err .= ". Setting to default OS \"other\".\n";
  }
436

437 438
  if (Param("usetargetmilestone")) {
    if (defined  ($::target_milestone{$product[0]} ) &&
439 440
       (my @tm = grep(lc($_) eq lc($bug_fields{'target_milestone'}), 
                       @{$::target_milestone{$product[0]}})) ){
441 442
      push (@values, SqlQuote($tm[0]) );
      push (@query, "target_milestone");
443 444
    } else {
      SendSQL("SELECT defaultmilestone FROM products " .
445
              "WHERE name = " . SqlQuote($product[0]));
446
      my $tm = FetchOneColumn();
447 448
      push (@values, SqlQuote($tm));
      push (@query, "target_milestone");
449 450 451 452 453 454 455 456
      $err .= "Unknown milestone \"";
      $err .= (defined $bug_fields{'target_milestone'})?
              $bug_fields{'target_milestone'}:"unknown";
      $err .= "\" in product \"$product[0]\".\n";
      $err .= "   Setting to default milestone for this product, ";
      $err .= "\'" . $tm . "\'\n";
    }
  }
457

458
  if (defined ($bug_fields{'bug_severity'}) &&
459 460
       (my @severity= grep(lc($_) eq lc($bug_fields{'bug_severity'}), 
                           @::legal_severity)) ){
461 462
    push (@values, SqlQuote($severity[0]) );
    push (@query, "bug_severity");
463
  } else {
464 465
    push (@values, SqlQuote("normal"));
    push (@query, "bug_severity");
466 467 468 469
    $err .= "Unknown severity ";
    $err .= (defined $bug_fields{'bug_severity'})?
                     $bug_fields{'bug_severity'}:"unknown";
    $err .= ". Setting to default severity \"normal\".\n";
470 471
  }

472
  my $reporterid = login_to_id($bug_fields{'reporter'});
473
  if ( ($bug_fields{'reporter'}) && ( $reporterid ) ) {
474 475
    push (@values, SqlQuote($reporterid));
    push (@query, "reporter");
476
  } else {
477 478
    push (@values, SqlQuote($exporterid));
    push (@query, "reporter");
479 480 481 482 483 484 485 486 487 488
    $err .= "The original reporter of this bug does not have\n";
    $err .= "   an account here. Reassigning to the person who moved\n";
    $err .= "   it here, $exporter.\n";
    if ( $bug_fields{'reporter'} ) {
      $err .= "   Previous reporter was $bug_fields{'reporter'}.\n";
    } else {
      $err .= "   Previous reporter is unknown.\n";
    }
  }

489 490
  my $changed_owner = 0;
  if ( ($bug_fields{'assigned_to'}) && 
491 492
       ( login_to_id($bug_fields{'assigned_to'})) ) {
    push (@values, SqlQuote(login_to_id($bug_fields{'assigned_to'})));
493
    push (@query, "assigned_to");
494
  } else {
495 496
    push (@values, SqlQuote($exporterid) );
    push (@query, "assigned_to");
497
    $changed_owner = 1;
498
    $err .= "The original assignee of this bug does not have\n";
499
    $err .= "   an account here. Reassigning to the person who moved\n";
500
    $err .= "   it here, $exporter.\n";
501
    if ( $bug_fields{'assigned_to'} ) {
502
      $err .= "   Previous assignee was $bug_fields{'assigned_to'}.\n";
503
    } else {
504
      $err .= "   Previous assignee is unknown.\n";
505 506
    }
  }
507

508 509
  my @resolution;
  if (defined ($bug_fields{'resolution'}) &&
510
       (@resolution= grep(lc($_) eq lc($bug_fields{'resolution'}), @::legal_resolution)) ){
511 512
    push (@values, SqlQuote($resolution[0]) );
    push (@query, "resolution");
513 514 515
  } elsif ( (defined $bug_fields{'resolution'}) && (!$resolution[0]) ){
    $err .= "Unknown resolution \"$bug_fields{'resolution'}\".\n";
  }
516

517
  # if the bug's assignee changed, mark the bug NEW, unless a valid 
518 519 520
  # resolution is set, which indicates that the bug should be closed.
  #
  if ( ($changed_owner) && (!$resolution[0]) ) {
521 522
    push (@values, SqlQuote("NEW"));
    push (@query, "bug_status");
523
    $err .= "Bug reassigned, setting status to \"NEW\".\n";
524 525 526 527 528 529
    $err .= "   Previous status was \"";
    $err .= (defined $bug_fields{'bug_status'})?
                     $bug_fields{'bug_status'}:"unknown";
    $err .= "\".\n";
  } elsif ( (defined ($bug_fields{'resolution'})) && (!$resolution[0]) ){
    #if the resolution was illegal then set status to NEW
530 531
    push (@values, SqlQuote("NEW"));
    push (@query, "bug_status");
532 533 534 535 536 537
    $err .= "Resolution was invalid. Setting status to \"NEW\".\n";
    $err .= "   Previous status was \"";
    $err .= (defined $bug_fields{'bug_status'})?
                     $bug_fields{'bug_status'}:"unknown";
    $err .= "\".\n";
  } elsif (defined ($bug_fields{'bug_status'}) &&
538
       (my @status = grep(lc($_) eq lc($bug_fields{'bug_status'}), @::legal_bug_status)) ){
539
    #if a bug status was set then use it, if its legal
540 541
    push (@values, SqlQuote($status[0]));
    push (@query, "bug_status");
542
  } else {
543
    # if all else fails, make the bug new
544 545
    push (@values, SqlQuote("NEW"));
    push (@query, "bug_status");
546 547 548 549
    $err .= "Unknown status ";
    $err .= (defined $bug_fields{'bug_status'})?
                     $bug_fields{'bug_status'}:"unknown";
    $err .= ". Setting to default status \"NEW\".\n";
550 551
  }

552 553 554
  if (Param("useqacontact")) {
    my $qa_contact;
    if ( (defined $bug_fields{'qa_contact'}) &&
555
         ($qa_contact  = login_to_id($bug_fields{'qa_contact'})) ){
556
      push (@values, $qa_contact);
557
      push (@query, "qa_contact");
558
    } else {
559
      SendSQL("SELECT initialqacontact FROM components, products " .
560 561 562
              "WHERE components.product_id = products.id" .
              " AND products.name = " . SqlQuote($product[0]) .
              " AND components.name = " . SqlQuote($component[0]) );
563
      $qa_contact = FetchOneColumn();
564
      push (@values, $qa_contact);
565
      push (@query, "qa_contact");
566 567 568 569
      $err .= "Setting qa contact to the default for this product.\n";
      $err .= "   This bug either had no qa contact or an invalid one.\n";
    }
  }
570

571 572 573 574 575 576

  my $query  = "INSERT INTO bugs (\n" 
               . join (",\n", @query)
               . "\n) VALUES (\n"
               . join (",\n", @values)
               . "\n)\n";
577
  SendSQL($query);
578
  my $id = $dbh->bz_last_key('bugs', 'bug_id');
579 580 581 582

  if (defined $bug_fields{'cc'}) {
    foreach my $person (split(/[ ,]/, $bug_fields{'cc'})) {
      my $uid;
583
      if ( ($person ne "") && ($uid = login_to_id($person)) ) {
584 585 586
        SendSQL("insert into cc (bug_id, who) values ($id, " . SqlQuote($uid) .")");
      }
    }
587 588
  }

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
  if (defined ($bug_fields{'keywords'})) {
    my %keywordseen;
    foreach my $keyword (split(/[\s,]+/, $bug_fields{'keywords'})) {
      if ($keyword eq '') {
        next;
      }
      my $i = $::keywordsbyname{$keyword};
      if (!$i) {
        $err .= "Skipping unknown keyword: $keyword.\n";
        next;
      }
      if (!$keywordseen{$i}) {
        SendSQL("INSERT INTO keywords (bug_id, keywordid) VALUES ($id, $i)");
        $keywordseen{$i} = 1;
      }
    }
  }

607 608 609 610
  $long_description .= "\n" . $comments;
  if ($err) {
    $long_description .= "\n$err\n";
  }
611

612 613
  SendSQL("INSERT INTO longdescs (bug_id, who, bug_when, thetext) VALUES " .
    "($id, $exporterid, now(), " . SqlQuote($long_description) . ")");
614

615 616 617
  $log .= "Bug $urlbase/show_bug.cgi?id=$bug_fields{'bug_id'} ";
  $log .= "imported as bug $id.\n";
  $log .= Param("urlbase") . "/show_bug.cgi?id=$id\n\n";
618
  if ($err) {
619 620
    $log .= "The following problems were encountered creating bug $id.\n";
    $log .= "You may have to set certain fields in the new bug by hand.\n\n";
621
    $log .= $err;
622
    $log .= "\n\n\n";
623
  }
624

625
  Bugzilla::BugMail::Send($id, { 'changer' => $exporter });
626
}
627

628 629
my $subject = "$bugqty bug(s) successfully moved from $urlbase to " 
               . Param("urlbase") ;
630 631
my @to = ($exporter);
MailMessage ($subject, $log, @to);