process_bug.cgi 14.3 KB
Newer Older
1
#!/usr/bin/perl -T
2 3 4
# 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/.
terry%netscape.com's avatar
terry%netscape.com committed
5
#
6 7
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
terry%netscape.com's avatar
terry%netscape.com committed
8

9
use 5.10.1;
10
use strict;
11 12
use warnings;

13
use lib qw(. lib);
14

15
use Bugzilla;
16
use Bugzilla::Constants;
17
use Bugzilla::Bug;
18
use Bugzilla::User;
19
use Bugzilla::Util;
20
use Bugzilla::Error;
21
use Bugzilla::Flag;
22
use Bugzilla::Status;
23
use Bugzilla::Token;
24

25
use List::MoreUtils qw(firstidx);
26 27
use Storable qw(dclone);

28
my $user = Bugzilla->login(LOGIN_REQUIRED);
29

30 31
my $cgi      = Bugzilla->cgi;
my $dbh      = Bugzilla->dbh;
32
my $template = Bugzilla->template;
33
my $vars     = {};
34

35 36 37 38
######################################################################
# Subroutines
######################################################################

39
# Tells us whether or not a field should be changed by process_bug.
40
sub should_set {
41 42 43 44 45 46 47 48 49 50 51 52 53

  # check_defined is used for fields where there's another field
  # whose name starts with "defined_" and then the field name--it's used
  # to know when we did things like empty a multi-select or deselect
  # a checkbox.
  my ($field, $check_defined) = @_;
  my $cgi = Bugzilla->cgi;
  if (defined $cgi->param($field)
    || ($check_defined && defined $cgi->param("defined_$field")))
  {
    return 1;
  }
  return 0;
54 55
}

56 57 58 59
######################################################################
# Begin Data/Security Validation
######################################################################

60 61
# Create a list of objects for all bugs being modified in this request.
my @bug_objects;
62
if (defined $cgi->param('id')) {
63
  my $bug = Bugzilla::Bug->check_for_edit(scalar $cgi->param('id'));
64 65
  $cgi->param('id', $bug->id);
  push(@bug_objects, $bug);
66 67 68 69 70 71
}
else {
  foreach my $i ($cgi->param()) {
    if ($i =~ /^id_([1-9][0-9]*)/) {
      my $id = $1;
      push(@bug_objects, Bugzilla::Bug->check_for_edit($id));
72
    }
73
  }
74 75
}

76
# Make sure there are bugs to process.
77
scalar(@bug_objects) || ThrowUserError("no_bugs_chosen", {action => 'modify'});
78

79
my $first_bug = $bug_objects[0];   # Used when we're only updating a single bug.
80

81 82
# Delete any parameter set to 'dontchange'.
if (defined $cgi->param('dontchange')) {
83 84 85 86 87 88 89
  foreach my $name ($cgi->param) {
    next if $name eq 'dontchange';    # But don't delete dontchange itself!
         # Skip ones we've already deleted (such as "defined_$name").
    next if !defined $cgi->param($name);
    if ($cgi->param($name) eq $cgi->param('dontchange')) {
      $cgi->delete($name);
      $cgi->delete("defined_$name");
90
    }
91
  }
92 93
}

94
# do a match on the fields if applicable
95
Bugzilla::User::match_field({
96 97 98 99
  'qa_contact'  => {'type' => 'single'},
  'newcc'       => {'type' => 'multi'},
  'masscc'      => {'type' => 'multi'},
  'assigned_to' => {'type' => 'single'},
100
});
101

102
print $cgi->header() unless Bugzilla->usage_mode == USAGE_MODE_EMAIL;
103

104 105
# Check for a mid-air collision. Currently this only works when updating
# an individual bug.
106
my $delta_ts = $cgi->param('delta_ts') || '';
107

108 109
my $productive_time = $cgi->param('productive_time') || '';

110
if ($delta_ts) {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  my $delta_ts_z = datetime_from($delta_ts)
    or ThrowCodeError('invalid_timestamp', {timestamp => $delta_ts});

  my $first_delta_tz_z = datetime_from($first_bug->delta_ts);

  if ($first_delta_tz_z ne $delta_ts_z) {
    ($vars->{'operations'}) = $first_bug->get_activity(undef, $delta_ts);

    # Always sort midair collision comments oldest to newest,
    # regardless of the user's personal preference.
    my $comments
      = $first_bug->comments({order => 'oldest_to_newest', after => $delta_ts});

    # Show midair if previous changes made other than CC
    # and/or one or more comments were made
    my $do_midair = scalar @$comments ? 1 : 0;

    if (!$do_midair) {
      foreach my $operation (@{$vars->{'operations'}}) {
        foreach my $change (@{$operation->{'changes'}}) {
          if ($change->{'fieldname'} ne 'cc') {
            $do_midair = 1;
            last;
          }
135
        }
136 137 138
        last if $do_midair;
      }
    }
139

140 141 142 143 144 145 146 147 148 149 150 151
    if ($do_midair) {
      $vars->{'title_tag'} = "mid_air";
      $vars->{'comments'}  = $comments;
      $vars->{'bug'}       = $first_bug;

      # The token contains the old delta_ts. We need a new one.
      $cgi->param('token', issue_hash_token([$first_bug->id, $first_bug->delta_ts]));

      # Warn the user about the mid-air collision and ask them what to do.
      $template->process("bug/process/midair.html.tmpl", $vars)
        || ThrowTemplateError($template->error());
      exit;
152
    }
153
  }
154
}
155

156 157 158 159 160 161
# We couldn't do this check earlier as we first had to validate bug IDs
# and display the mid-air collision page if delta_ts changed.
# If we do a mass-change, we use session tokens.
my $token = $cgi->param('token');

if ($cgi->param('id')) {
162
  check_hash_token($token, [$first_bug->id, $delta_ts || $first_bug->delta_ts]);
163 164
}
else {
165
  check_token_data($token, 'buglist_mass_change', 'query.cgi');
166 167 168 169 170 171
}

######################################################################
# End Data/Security Validation
######################################################################

172 173
$vars->{'title_tag'} = "bug_processed";

174
my $action;
175
if (defined $cgi->param('id')) {
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  $action = $user->setting('post_bug_submit_action');

  if ($action eq 'next_bug') {
    my $bug_list_obj = $user->recent_search_for($first_bug);
    my @bug_list     = $bug_list_obj ? @{$bug_list_obj->bug_list} : ();
    my $cur          = firstidx { $_ eq $cgi->param('id') } @bug_list;
    if ($cur >= 0 && $cur < $#bug_list) {
      my $next_bug_id = $bug_list[$cur + 1];
      detaint_natural($next_bug_id);
      if ($next_bug_id and $user->can_see_bug($next_bug_id)) {

        # We create an object here so that $bug->send_changes can use it
        # when displaying the header.
        $vars->{'bug'} = new Bugzilla::Bug($next_bug_id);
      }
191
    }
192 193 194 195 196 197
  }

  # Include both action = 'same_bug' and 'nothing'.
  else {
    $vars->{'bug'} = $first_bug;
  }
198 199
}
else {
200 201
  # param('id') is not defined when changing multiple bugs at once.
  $action = 'nothing';
202 203
}

204 205 206
# Component, target_milestone, and version are in here just in case
# the 'product' field wasn't defined in the CGI. It doesn't hurt to set
# them twice.
207
my @set_fields = qw(op_sys rep_platform priority bug_severity
208 209 210
  component target_milestone version
  bug_file_loc status_whiteboard short_desc
  deadline remaining_time estimated_time
211
  work_time productive_time set_default_assignee set_default_qa_contact
212 213 214
  cclist_accessible reporter_accessible
  product confirm_product_change
  bug_status resolution dup_id bug_ignored);
215 216
push(@set_fields, 'assigned_to') if !$cgi->param('set_default_assignee');
push(@set_fields, 'qa_contact')  if !$cgi->param('set_default_qa_contact');
217
my %field_translation = (
218 219 220 221 222 223 224
  bug_severity           => 'severity',
  rep_platform           => 'platform',
  short_desc             => 'summary',
  bug_file_loc           => 'url',
  set_default_assignee   => 'reset_assigned_to',
  set_default_qa_contact => 'reset_qa_contact',
  confirm_product_change => 'product_change_confirmed',
225
  productive_time 	 => 'productive_time',
226 227
);

228
my %set_all_fields = (other_bugs => \@bug_objects);
229
foreach my $field_name (@set_fields) {
230 231 232 233
  if (should_set($field_name, 1)) {
    my $param_name = $field_translation{$field_name} || $field_name;
    $set_all_fields{$param_name} = $cgi->param($field_name);
  }
234 235
}

236
if (should_set('keywords')) {
237 238 239 240 241
  my $action = $cgi->param('keywordaction') || '';
  # Backward-compatibility for Bugzilla 3.x and older.
  $action = 'remove' if $action eq 'delete';
  $action = 'set'    if $action eq 'makeexact';
  $set_all_fields{keywords}->{$action} = $cgi->param('keywords');
242
}
243
if (should_set('comment')) {
244 245 246 247
  $set_all_fields{comment} = {
    body       => scalar $cgi->param('comment'),
    is_private => scalar $cgi->param('comment_is_private'),
  };
248
}
249
if (should_set('see_also')) {
250
  $set_all_fields{'see_also'}->{add} = [split(/[\s]+/, $cgi->param('see_also'))];
251 252
}
if (should_set('remove_see_also')) {
253
  $set_all_fields{'see_also'}->{remove} = [$cgi->param('remove_see_also')];
254
}
255
foreach my $dep_field (qw(dependson blocked)) {
256 257 258 259
  if (should_set($dep_field)) {
    if (my $dep_action = $cgi->param("${dep_field}_action")) {
      $set_all_fields{$dep_field}->{$dep_action}
        = [split(/[\s,]+/, $cgi->param($dep_field))];
260
    }
261 262 263 264
    else {
      $set_all_fields{$dep_field}->{set} = $cgi->param($dep_field);
    }
  }
265
}
266

267
# Formulate the CC data into two arrays of users involved in this CC change.
268 269 270 271
if ( defined $cgi->param('newcc')
  or defined $cgi->param('addselfcc')
  or defined $cgi->param('removecc')
  or defined $cgi->param('masscc'))
272
{
273 274 275 276 277 278 279
  my (@cc_add, @cc_remove);

  # If masscc is defined, then we came from buglist and need to either add or
  # remove cc's... otherwise, we came from show_bug and may need to do both.
  if (defined $cgi->param('masscc')) {
    if ($cgi->param('ccaction') eq 'add') {
      @cc_add = $cgi->param('masscc');
280
    }
281 282 283 284 285 286 287 288 289 290 291 292 293 294
    elsif ($cgi->param('ccaction') eq 'remove') {
      @cc_remove = $cgi->param('masscc');
    }
  }
  else {
    @cc_add = $cgi->param('newcc');
    push(@cc_add, $user) if $cgi->param('addselfcc');

    # We came from show_bug which uses a select box to determine what cc's
    # need to be removed...
    if ($cgi->param('removecc') && $cgi->param('cc')) {
      @cc_remove = $cgi->param('cc');
    }
  }
295

296
  $set_all_fields{cc} = {add => \@cc_add, remove => \@cc_remove};
297
}
298 299 300

# Fields that can only be set on one bug at a time.
if (defined $cgi->param('id')) {
301

302 303 304 305 306 307 308 309 310 311
  # Since aliases are unique (like bug numbers), they can only be changed
  # for one bug at a time.
  if (defined $cgi->param('newalias') || defined $cgi->param('removealias')) {
    my @alias_add = split /[, ]+/, $cgi->param('newalias');

    # We came from bug_form which uses a select box to determine what
    # aliases need to be removed...
    my @alias_remove = ();
    if ($cgi->param('removealias') && $cgi->param('alias')) {
      @alias_remove = $cgi->param('alias');
312
    }
313 314 315

    $set_all_fields{alias} = {add => \@alias_add, remove => \@alias_remove};
  }
316 317 318 319
}

my %is_private;
foreach my $field (grep(/^defined_isprivate/, $cgi->param())) {
320 321 322 323
  if ($field =~ /(\d+)$/) {
    my $comment_id = $1;
    $is_private{$comment_id} = $cgi->param("isprivate_$comment_id");
  }
324 325 326
}
$set_all_fields{comment_is_private} = \%is_private;

327
my @check_groups = $cgi->param('defined_groups');
328
my @set_groups   = $cgi->param('groups');
329
my ($removed_groups) = diff_arrays(\@check_groups, \@set_groups);
330
$set_all_fields{groups} = {add => \@set_groups, remove => $removed_groups};
331

332
my @custom_fields = Bugzilla->active_custom_fields;
333
foreach my $field (@custom_fields) {
334 335 336 337
  my $fname = $field->name;
  if (should_set($fname, 1)) {
    $set_all_fields{$fname} = [$cgi->param($fname)];
  }
338
}
339

340 341
# We are going to alter the list of removed groups, so we keep a copy here.
my @unchecked_groups = @$removed_groups;
342
foreach my $b (@bug_objects) {
343 344 345 346 347 348 349 350 351 352

  # Don't blindly ask to remove unchecked groups available in the UI.
  # A group can be already unchecked, and the user didn't try to remove it.
  # In this case, we don't want remove_group() to complain.
  my @remove_groups;
  foreach my $g (@{$b->groups_in}) {
    push(@remove_groups, $g->name) if grep { $_ eq $g->name } @unchecked_groups;
  }
  local $set_all_fields{groups}->{remove} = \@remove_groups;
  $b->set_all(\%set_all_fields);
353 354
}

355
if (defined $cgi->param('id')) {
356 357 358 359 360 361 362 363 364 365 366 367 368 369

  # Flags should be set AFTER the bug has been moved into another
  # product/component. The structure of flags code doesn't currently
  # allow them to be set using set_all.
  my ($flags, $new_flags)
    = Bugzilla::Flag->extract_flags_from_cgi($first_bug, undef, $vars);
  $first_bug->set_flags($flags, $new_flags);
  # Tags can only be set to one bug at once.
  if (should_set('tag')) {
    my @new_tags = grep { trim($_) } split(/,/, $cgi->param('tag'));
    my ($tags_removed, $tags_added) = diff_arrays($first_bug->tags, \@new_tags);
    $first_bug->remove_tag($_) foreach @$tags_removed;
    $first_bug->add_tag($_)    foreach @$tags_added;
  }
370
}
371
else {
372 373 374 375 376 377 378 379 380
  # Update flags on multiple bugs. The cgi params are slightly different
  # than on a single bug, so we need to call a different sub. We also
  # need to call this per bug, since we might be updating a flag in one
  # bug, but adding it to a second bug
  foreach my $b (@bug_objects) {
    my ($flags, $new_flags)
      = Bugzilla::Flag->multi_extract_flags_from_cgi($b, $vars);
    $b->set_flags($flags, $new_flags);
  }
381
}
382

383 384 385 386
##############################
# Do Actual Database Updates #
##############################
foreach my $bug (@bug_objects) {
387 388 389 390 391 392 393 394 395
  my $changes = $bug->update();

  if ($changes->{'bug_status'}) {
    my $new_status = $changes->{'bug_status'}->[1];

    # We may have zeroed the remaining time, if we moved into a closed
    # status, so we should inform the user about that.
    if (!is_open_state($new_status) && $changes->{'remaining_time'}) {
      $vars->{'message'} = "remaining_time_zeroed" if $user->is_timetracker;
396
    }
397
  }
398

399
  $bug->send_changes($changes, $vars);
terry%netscape.com's avatar
terry%netscape.com committed
400 401
}

402 403 404
# Delete the session token used for the mass-change.
delete_token($token) unless $cgi->param('id');

405
if (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
406 407

  # Do nothing.
408
}
409
elsif ($action eq 'next_bug' or $action eq 'same_bug') {
410 411 412 413 414 415 416 417 418 419
  my $bug = $vars->{'bug'};
  if ($bug and $user->can_see_bug($bug)) {
    if ($action eq 'same_bug') {

      # $bug->update() does not update the internal structure of
      # the bug sufficiently to display the bug with the new values.
      # (That is, if we just passed in the old Bug object, we'd get
      # a lot of old values displayed.)
      $bug = new Bugzilla::Bug($bug->id);
      $vars->{'bug'} = $bug;
420
    }
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    $vars->{'bugs'} = [$bug];
    if ($action eq 'next_bug') {
      $vars->{'nextbug'} = $bug->id;
    }

    # For performance reasons, preload visibility of dependencies
    # and duplicates related to this bug.
    Bugzilla::Bug->preload([$bug]);

    $template->process("bug/show.html.tmpl", $vars)
      || ThrowTemplateError($template->error());
    exit;
  }
}
elsif ($action ne 'nothing') {
  ThrowCodeError("invalid_post_bug_submit_action");
terry%netscape.com's avatar
terry%netscape.com committed
437
}
438

439
# End the response page.
440
unless (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
441 442 443 444
  $template->process("bug/navigate.html.tmpl", $vars)
    || ThrowTemplateError($template->error());
  $template->process("global/footer.html.tmpl", $vars)
    || ThrowTemplateError($template->error());
445 446 447
}

1;