blob: c62854a8d9d110e6eafeebd3521a372ce86b37f7 [file] [log] [blame]
ddkilzer@apple.comf573e632009-07-03 02:14:39 +00001#!/usr/bin/env perl -wT
timothy@apple.comf42518d2008-02-06 20:19:16 +00002# -*- Mode: perl; indent-tabs-mode: nil -*-
3#
4# The contents of this file are subject to the Mozilla Public
5# License Version 1.1 (the "License"); you may not use this file
6# except in compliance with the License. You may obtain a copy of
7# the License at http://www.mozilla.org/MPL/
8#
9# Software distributed under the License is distributed on an "AS
10# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11# implied. See the License for the specific language governing
12# rights and limitations under the License.
13#
14# The Original Code is the Bugzilla Bug Tracking System.
15#
16# The Initial Developer of the Original Code is Netscape Communications
17# Corporation. Portions created by Netscape are
18# Copyright (C) 1998 Netscape Communications Corporation. All
19# Rights Reserved.
20#
21# Contributor(s): Terry Weissman <terry@mozilla.org>
22# Dan Mosedale <dmose@mozilla.org>
23# Dave Miller <justdave@syndicomm.com>
24# Christopher Aillon <christopher@aillon.com>
25# Myk Melez <myk@mozilla.org>
26# Jeff Hedlund <jeff.hedlund@matrixsi.com>
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000027# Frédéric Buclin <LpSolit@gmail.com>
28# Lance Larsh <lance.larsh@oracle.com>
29# Akamai Technologies <bugzilla-dev@akamai.com>
ddkilzer@apple.com097da082009-07-03 02:14:25 +000030# Max Kanat-Alexander <mkanat@bugzilla.org>
timothy@apple.comf42518d2008-02-06 20:19:16 +000031
32# Implementation notes for this file:
33#
34# 1) the 'id' form parameter is validated early on, and if it is not a valid
35# bugid an error will be reported, so it is OK for later code to simply check
36# for a defined form 'id' value, and it can assume a valid bugid.
37#
38# 2) If the 'id' form parameter is not defined (after the initial validation),
39# then we are processing multiple bugs, and @idlist will contain the ids.
40#
41# 3) If we are processing just the one id, then it is stored in @idlist for
42# later processing.
43
44use strict;
45
ddkilzer@apple.com097da082009-07-03 02:14:25 +000046use lib qw(. lib);
timothy@apple.comf42518d2008-02-06 20:19:16 +000047
48use Bugzilla;
49use Bugzilla::Constants;
timothy@apple.comf42518d2008-02-06 20:19:16 +000050use Bugzilla::Bug;
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000051use Bugzilla::BugMail;
52use Bugzilla::Mailer;
timothy@apple.comf42518d2008-02-06 20:19:16 +000053use Bugzilla::User;
54use Bugzilla::Util;
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000055use Bugzilla::Error;
56use Bugzilla::Field;
57use Bugzilla::Product;
58use Bugzilla::Component;
59use Bugzilla::Keyword;
timothy@apple.comf42518d2008-02-06 20:19:16 +000060use Bugzilla::Flag;
ddkilzer@apple.com097da082009-07-03 02:14:25 +000061use Bugzilla::Status;
62use Bugzilla::Token;
63
64use Storable qw(dclone);
timothy@apple.comf42518d2008-02-06 20:19:16 +000065
66my $user = Bugzilla->login(LOGIN_REQUIRED);
timothy@apple.comf42518d2008-02-06 20:19:16 +000067
68my $cgi = Bugzilla->cgi;
69my $dbh = Bugzilla->dbh;
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000070my $template = Bugzilla->template;
ddkilzer@apple.com097da082009-07-03 02:14:25 +000071my $vars = {};
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000072$vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
73
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000074######################################################################
75# Subroutines
76######################################################################
77
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000078# Used to send email when an update is done.
79sub send_results {
80 my ($bug_id, $vars) = @_;
81 my $template = Bugzilla->template;
82 if (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
83 Bugzilla::BugMail::Send($bug_id, $vars->{'mailrecipients'});
84 }
85 else {
86 $template->process("bug/process/results.html.tmpl", $vars)
87 || ThrowTemplateError($template->error());
88 }
89 $vars->{'header_done'} = 1;
90}
timothy@apple.comf42518d2008-02-06 20:19:16 +000091
ddkilzer@apple.com097da082009-07-03 02:14:25 +000092# Tells us whether or not a field should be changed by process_bug.
93sub should_set {
94 # check_defined is used for fields where there's another field
95 # whose name starts with "defined_" and then the field name--it's used
96 # to know when we did things like empty a multi-select or deselect
97 # a checkbox.
98 my ($field, $check_defined) = @_;
99 my $cgi = Bugzilla->cgi;
100 if ( defined $cgi->param($field)
101 || ($check_defined && defined $cgi->param("defined_$field")) )
102 {
103 return 1;
104 }
105 return 0;
106}
107
timothy@apple.comf42518d2008-02-06 20:19:16 +0000108######################################################################
109# Begin Data/Security Validation
110######################################################################
111
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000112# Create a list of objects for all bugs being modified in this request.
113my @bug_objects;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000114if (defined $cgi->param('id')) {
115 my $id = $cgi->param('id');
116 ValidateBugID($id);
117
118 # Store the validated, and detainted id back in the cgi data, as
119 # lots of later code will need it, and will obtain it from there
120 $cgi->param('id', $id);
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000121 push(@bug_objects, new Bugzilla::Bug($id));
timothy@apple.comf42518d2008-02-06 20:19:16 +0000122} else {
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000123 my @ids;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000124 foreach my $i ($cgi->param()) {
125 if ($i =~ /^id_([1-9][0-9]*)/) {
126 my $id = $1;
127 ValidateBugID($id);
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000128 push(@ids, $id);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000129 }
130 }
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000131 @bug_objects = @{Bugzilla::Bug->new_from_list(\@ids)};
timothy@apple.comf42518d2008-02-06 20:19:16 +0000132}
133
134# Make sure there are bugs to process.
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000135scalar(@bug_objects) || ThrowUserError("no_bugs_chosen", {action => 'modify'});
timothy@apple.comf42518d2008-02-06 20:19:16 +0000136
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000137my $first_bug = $bug_objects[0]; # Used when we're only updating a single bug.
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000138
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000139# Delete any parameter set to 'dontchange'.
140if (defined $cgi->param('dontchange')) {
141 foreach my $name ($cgi->param) {
142 next if $name eq 'dontchange'; # But don't delete dontchange itself!
143 # Skip ones we've already deleted (such as "defined_$name").
144 next if !defined $cgi->param($name);
145 if ($cgi->param($name) eq $cgi->param('dontchange')) {
146 $cgi->delete($name);
147 $cgi->delete("defined_$name");
timothy@apple.comf42518d2008-02-06 20:19:16 +0000148 }
149 }
150}
151
timothy@apple.comf42518d2008-02-06 20:19:16 +0000152# do a match on the fields if applicable
153
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000154# The order of these function calls is important, as Flag::validate
155# assumes User::match_field has ensured that the values
timothy@apple.comf42518d2008-02-06 20:19:16 +0000156# in the requestee fields are legitimate user email addresses.
157&Bugzilla::User::match_field($cgi, {
158 'qa_contact' => { 'type' => 'single' },
159 'newcc' => { 'type' => 'multi' },
160 'masscc' => { 'type' => 'multi' },
161 'assigned_to' => { 'type' => 'single' },
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000162 '^requestee(_type)?-(\d+)$' => { 'type' => 'multi' },
timothy@apple.comf42518d2008-02-06 20:19:16 +0000163});
164
165# Validate flags in all cases. validate() should not detect any
166# reference to flags if $cgi->param('id') is undefined.
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000167Bugzilla::Flag::validate($cgi->param('id'));
168
169print $cgi->header() unless Bugzilla->usage_mode == USAGE_MODE_EMAIL;
170
171# Check for a mid-air collision. Currently this only works when updating
172# an individual bug.
173if (defined $cgi->param('delta_ts')
174 && $cgi->param('delta_ts') ne $first_bug->delta_ts)
175{
176 ($vars->{'operations'}) =
177 Bugzilla::Bug::GetBugActivity($first_bug->id, undef,
178 scalar $cgi->param('delta_ts'));
179
180 $vars->{'title_tag'} = "mid_air";
181
182 ThrowCodeError('undefined_field', { field => 'longdesclength' })
183 if !defined $cgi->param('longdesclength');
184
185 $vars->{'start_at'} = $cgi->param('longdesclength');
186 # Always sort midair collision comments oldest to newest,
187 # regardless of the user's personal preference.
188 $vars->{'comments'} = Bugzilla::Bug::GetComments($first_bug->id,
189 "oldest_to_newest");
190 $vars->{'bug'} = $first_bug;
191 # The token contains the old delta_ts. We need a new one.
192 $cgi->param('token', issue_hash_token([$first_bug->id, $first_bug->delta_ts]));
193
194 # Warn the user about the mid-air collision and ask them what to do.
195 $template->process("bug/process/midair.html.tmpl", $vars)
196 || ThrowTemplateError($template->error());
197 exit;
198}
199
200# We couldn't do this check earlier as we first had to validate bug IDs
201# and display the mid-air collision page if delta_ts changed.
202# If we do a mass-change, we use session tokens.
203my $token = $cgi->param('token');
204
205if ($cgi->param('id')) {
206 check_hash_token($token, [$first_bug->id, $first_bug->delta_ts]);
207}
208else {
209 check_token_data($token, 'buglist_mass_change', 'query.cgi');
210}
timothy@apple.comf42518d2008-02-06 20:19:16 +0000211
212######################################################################
213# End Data/Security Validation
214######################################################################
215
timothy@apple.comf42518d2008-02-06 20:19:16 +0000216$vars->{'title_tag'} = "bug_processed";
217
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000218# Set up the vars for navigational <link> elements
219my @bug_list;
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000220if ($cgi->cookie("BUGLIST")) {
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000221 @bug_list = split(/:/, $cgi->cookie("BUGLIST"));
timothy@apple.comf42518d2008-02-06 20:19:16 +0000222 $vars->{'bug_list'} = \@bug_list;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000223}
224
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000225my ($action, $next_bug);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000226if (defined $cgi->param('id')) {
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000227 $action = Bugzilla->user->settings->{'post_bug_submit_action'}->{'value'};
timothy@apple.comf42518d2008-02-06 20:19:16 +0000228
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000229 if ($action eq 'next_bug') {
230 my $cur = lsearch(\@bug_list, $cgi->param('id'));
231 if ($cur >= 0 && $cur < $#bug_list) {
232 $next_bug = $bug_list[$cur + 1];
233 # No need to check whether the user can see the bug or not.
234 # All we want is its ID. An error will be thrown later
235 # if the user cannot see it.
236 $vars->{'bug'} = {bug_id => $next_bug};
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000237 }
238 }
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000239 # Include both action = 'same_bug' and 'nothing'.
timothy@apple.comf42518d2008-02-06 20:19:16 +0000240 else {
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000241 $vars->{'bug'} = {bug_id => $cgi->param('id')};
242 }
243}
244else {
245 # param('id') is not defined when changing multiple bugs at once.
246 $action = 'nothing';
247}
248
249# For each bug, we have to check if the user can edit the bug the product
250# is currently in, before we allow them to change anything.
251foreach my $bug (@bug_objects) {
252 if (!Bugzilla->user->can_edit_product($bug->product_obj->id) ) {
253 ThrowUserError("product_edit_denied",
254 { product => $bug->product });
timothy@apple.comf42518d2008-02-06 20:19:16 +0000255 }
256}
257
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000258# For security purposes, and because lots of other checks depend on it,
259# we set the product first before anything else.
260my $product_change; # Used only for strict_isolation checks, right now.
261if (should_set('product')) {
262 foreach my $b (@bug_objects) {
263 my $changed = $b->set_product(scalar $cgi->param('product'),
264 { component => scalar $cgi->param('component'),
265 version => scalar $cgi->param('version'),
266 target_milestone => scalar $cgi->param('target_milestone'),
267 change_confirmed => scalar $cgi->param('confirm_product_change'),
268 other_bugs => \@bug_objects,
269 });
270 $product_change ||= $changed;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000271 }
272}
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000273
274# strict_isolation checks mean that we should set the groups
275# immediately after changing the product.
276foreach my $b (@bug_objects) {
277 foreach my $group (@{$b->product_obj->groups_valid}) {
278 my $gid = $group->id;
279 if (should_set("bit-$gid", 1)) {
280 # Check ! first to avoid having to check defined below.
281 if (!$cgi->param("bit-$gid")) {
282 $b->remove_group($gid);
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000283 }
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000284 # "== 1" is important because mass-change uses -1 to mean
285 # "don't change this restriction"
286 elsif ($cgi->param("bit-$gid") == 1) {
287 $b->add_group($gid);
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000288 }
289 }
timothy@apple.comf42518d2008-02-06 20:19:16 +0000290 }
291}
292
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000293if ($cgi->param('id') && (defined $cgi->param('dependson')
294 || defined $cgi->param('blocked')) )
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000295{
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000296 $first_bug->set_dependencies(scalar $cgi->param('dependson'),
297 scalar $cgi->param('blocked'));
298}
299# Right now, you can't modify dependencies on a mass change.
300else {
301 $cgi->delete('dependson');
302 $cgi->delete('blocked');
303}
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000304
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000305my $any_keyword_changes;
306if (defined $cgi->param('keywords')) {
307 foreach my $b (@bug_objects) {
308 my $return =
309 $b->modify_keywords(scalar $cgi->param('keywords'),
310 scalar $cgi->param('keywordaction'));
311 $any_keyword_changes ||= $return;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000312 }
313}
314
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000315# Component, target_milestone, and version are in here just in case
316# the 'product' field wasn't defined in the CGI. It doesn't hurt to set
317# them twice.
318my @set_fields = qw(op_sys rep_platform priority bug_severity
319 component target_milestone version
320 bug_file_loc status_whiteboard short_desc
321 deadline remaining_time estimated_time);
322push(@set_fields, 'assigned_to') if !$cgi->param('set_default_assignee');
323push(@set_fields, 'qa_contact') if !$cgi->param('set_default_qa_contact');
324my @custom_fields = Bugzilla->active_custom_fields;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000325
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000326my %methods = (
327 bug_severity => 'set_severity',
328 rep_platform => 'set_platform',
329 short_desc => 'set_summary',
330 bug_file_loc => 'set_url',
331);
332foreach my $b (@bug_objects) {
333 if (should_set('comment') || $cgi->param('work_time')) {
334 # Add a comment as needed to each bug. This is done early because
335 # there are lots of things that want to check if we added a comment.
336 $b->add_comment(scalar($cgi->param('comment')),
337 { isprivate => scalar $cgi->param('commentprivacy'),
338 work_time => scalar $cgi->param('work_time') });
339 }
340 foreach my $field_name (@set_fields) {
341 if (should_set($field_name)) {
342 my $method = $methods{$field_name};
343 $method ||= "set_" . $field_name;
344 $b->$method($cgi->param($field_name));
345 }
346 }
347 $b->reset_assigned_to if $cgi->param('set_default_assignee');
348 $b->reset_qa_contact if $cgi->param('set_default_qa_contact');
349
350 # And set custom fields.
351 foreach my $field (@custom_fields) {
352 my $fname = $field->name;
353 if (should_set($fname, 1)) {
354 $b->set_custom_field($field, [$cgi->param($fname)]);
355 }
356 }
357}
358
359# Certain changes can only happen on individual bugs, never on mass-changes.
360if (defined $cgi->param('id')) {
361 # Since aliases are unique (like bug numbers), they can only be changed
362 # for one bug at a time.
363 if (Bugzilla->params->{"usebugaliases"} && defined $cgi->param('alias')) {
364 $first_bug->set_alias($cgi->param('alias'));
365 }
366
367 # reporter_accessible and cclist_accessible--these are only set if
368 # the user can change them and they appear on the page.
369 if (should_set('cclist_accessible', 1)) {
370 $first_bug->set_cclist_accessible($cgi->param('cclist_accessible'))
371 }
372 if (should_set('reporter_accessible', 1)) {
373 $first_bug->set_reporter_accessible($cgi->param('reporter_accessible'))
374 }
375
376 # You can only mark/unmark comments as private on single bugs. If
377 # you're not in the insider group, this code won't do anything.
378 foreach my $field (grep(/^defined_isprivate/, $cgi->param())) {
379 $field =~ /(\d+)$/;
380 my $comment_id = $1;
381 $first_bug->set_comment_is_private($comment_id,
382 $cgi->param("isprivate_$comment_id"));
383 }
384}
385
386# We need to check the addresses involved in a CC change before we touch
387# any bugs. What we'll do here is formulate the CC data into two arrays of
388# users involved in this CC change. Then those arrays can be used later
389# on for the actual change.
390my (@cc_add, @cc_remove);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000391if (defined $cgi->param('newcc')
392 || defined $cgi->param('addselfcc')
393 || defined $cgi->param('removecc')
394 || defined $cgi->param('masscc')) {
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000395
timothy@apple.comf42518d2008-02-06 20:19:16 +0000396 # If masscc is defined, then we came from buglist and need to either add or
397 # remove cc's... otherwise, we came from bugform and may need to do both.
398 my ($cc_add, $cc_remove) = "";
399 if (defined $cgi->param('masscc')) {
400 if ($cgi->param('ccaction') eq 'add') {
401 $cc_add = join(' ',$cgi->param('masscc'));
402 } elsif ($cgi->param('ccaction') eq 'remove') {
403 $cc_remove = join(' ',$cgi->param('masscc'));
404 }
405 } else {
406 $cc_add = join(' ',$cgi->param('newcc'));
407 # We came from bug_form which uses a select box to determine what cc's
408 # need to be removed...
409 if (defined $cgi->param('removecc') && $cgi->param('cc')) {
410 $cc_remove = join (",", $cgi->param('cc'));
411 }
412 }
413
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000414 push(@cc_add, split(/[\s,]+/, $cc_add)) if $cc_add;
415 push(@cc_add, Bugzilla->user) if $cgi->param('addselfcc');
416
417 push(@cc_remove, split(/[\s,]+/, $cc_remove)) if $cc_remove;
418}
419
420foreach my $b (@bug_objects) {
421 $b->remove_cc($_) foreach @cc_remove;
422 $b->add_cc($_) foreach @cc_add;
423 # Theoretically you could move a product without ever specifying
424 # a new assignee or qa_contact, or adding/removing any CCs. So,
425 # we have to check that the current assignee, qa, and CCs are still
426 # valid if we've switched products, under strict_isolation. We can only
427 # do that here. There ought to be some better way to do this,
428 # architecturally, but I haven't come up with it.
429 if ($product_change) {
430 $b->_check_strict_isolation();
timothy@apple.comf42518d2008-02-06 20:19:16 +0000431 }
432}
433
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000434my $move_action = $cgi->param('action') || '';
435if ($move_action eq Bugzilla->params->{'move-button-text'}) {
436 Bugzilla->params->{'move-enabled'} || ThrowUserError("move_bugs_disabled");
timothy@apple.comf42518d2008-02-06 20:19:16 +0000437
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000438 $user->is_mover || ThrowUserError("auth_failure", {action => 'move',
439 object => 'bugs'});
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000440
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000441 $dbh->bz_start_transaction();
timothy@apple.comf42518d2008-02-06 20:19:16 +0000442
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000443 # First update all moved bugs.
444 foreach my $bug (@bug_objects) {
445 $bug->add_comment('', { type => CMT_MOVED_TO, extra_data => $user->login });
446 }
447 # Don't export the new status and resolution. We want the current ones.
448 local $Storable::forgive_me = 1;
449 my $bugs = dclone(\@bug_objects);
450
451 my $new_status = Bugzilla->params->{'duplicate_or_move_bug_status'};
452 foreach my $bug (@bug_objects) {
453 $bug->set_status($new_status, {resolution => 'MOVED', moving => 1});
454 }
455 $_->update() foreach @bug_objects;
456 $dbh->bz_commit_transaction();
457
458 # Now send emails.
459 foreach my $bug (@bug_objects) {
460 $vars->{'mailrecipients'} = { 'changer' => $user->login };
461 $vars->{'id'} = $bug->id;
462 $vars->{'type'} = "move";
463 send_results($bug->id, $vars);
464 }
465 # Prepare and send all data about these bugs to the new database
466 my $to = Bugzilla->params->{'move-to-address'};
467 $to =~ s/@/\@/;
468 my $from = Bugzilla->params->{'moved-from-address'};
469 $from =~ s/@/\@/;
470 my $msg = "To: $to\n";
471 $msg .= "From: Bugzilla <" . $from . ">\n";
472 $msg .= "Subject: Moving bug(s) " . join(', ', map($_->id, @bug_objects))
473 . "\n\n";
474
475 my @fieldlist = (Bugzilla::Bug->fields, 'group', 'long_desc',
476 'attachment', 'attachmentdata');
477 my %displayfields;
478 foreach (@fieldlist) {
479 $displayfields{$_} = 1;
480 }
481
482 $template->process("bug/show.xml.tmpl", { bugs => $bugs,
483 displayfields => \%displayfields,
484 }, \$msg)
485 || ThrowTemplateError($template->error());
486
487 $msg .= "\n";
488 MessageToMTA($msg);
489
490 # End the response page.
491 unless (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
492 $template->process("bug/navigate.html.tmpl", $vars)
493 || ThrowTemplateError($template->error());
494 $template->process("global/footer.html.tmpl", $vars)
495 || ThrowTemplateError($template->error());
496 }
497 exit;
498}
499
500
501# You cannot mark bugs as duplicates when changing several bugs at once
502# (because currently there is no way to check for duplicate loops in that
503# situation).
504if (!$cgi->param('id') && $cgi->param('dup_id')) {
505 ThrowUserError('dupe_not_allowed');
506}
507
508# Set the status, resolution, and dupe_of (if needed). This has to be done
509# down here, because the validity of status changes depends on other fields,
510# such as Target Milestone.
511foreach my $b (@bug_objects) {
512 if (should_set('bug_status')) {
513 $b->set_status(
514 scalar $cgi->param('bug_status'),
515 {resolution => scalar $cgi->param('resolution'),
516 dupe_of => scalar $cgi->param('dup_id')}
517 );
518 }
519 elsif (should_set('resolution')) {
520 $b->set_resolution(scalar $cgi->param('resolution'),
521 {dupe_of => scalar $cgi->param('dup_id')});
522 }
523 elsif (should_set('dup_id')) {
524 $b->set_dup_id(scalar $cgi->param('dup_id'));
timothy@apple.comf42518d2008-02-06 20:19:16 +0000525 }
526}
527
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000528##############################
529# Do Actual Database Updates #
530##############################
531foreach my $bug (@bug_objects) {
532 $dbh->bz_start_transaction();
timothy@apple.comf42518d2008-02-06 20:19:16 +0000533
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000534 my $timestamp = $dbh->selectrow_array(q{SELECT NOW()});
535 my $changes = $bug->update($timestamp);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000536
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000537 my %notify_deps;
538 if ($changes->{'bug_status'}) {
539 my ($old_status, $new_status) = @{ $changes->{'bug_status'} };
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000540
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000541 # If this bug has changed from opened to closed or vice-versa,
542 # then all of the bugs we block need to be notified.
543 if (is_open_state($old_status) ne is_open_state($new_status)) {
544 $notify_deps{$_} = 1 foreach (@{$bug->blocked});
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000545 }
timothy@apple.comf42518d2008-02-06 20:19:16 +0000546
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000547 # We may have zeroed the remaining time, if we moved into a closed
548 # status, so we should inform the user about that.
549 if (!is_open_state($new_status) && $changes->{'remaining_time'}) {
550 $vars->{'message'} = "remaining_time_zeroed"
551 if Bugzilla->user->in_group(Bugzilla->params->{'timetrackinggroup'});
timothy@apple.comf42518d2008-02-06 20:19:16 +0000552 }
553 }
554
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000555 # To get a list of all changed dependencies, convert the "changes" arrays
556 # into a long string, then collapse that string into unique numbers in
557 # a hash.
558 my $all_changed_deps = join(', ', @{ $changes->{'dependson'} || [] });
559 $all_changed_deps = join(', ', @{ $changes->{'blocked'} || [] },
560 $all_changed_deps);
561 my %changed_deps = map { $_ => 1 } split(', ', $all_changed_deps);
562 # When clearning one field (say, blocks) and filling in the other
563 # (say, dependson), an empty string can get into the hash and cause
564 # an error later.
565 delete $changed_deps{''};
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000566
567 # $msgs will store emails which have to be sent to voters, if any.
568 my $msgs;
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000569 if ($changes->{'product'}) {
570 # If some votes have been removed, RemoveVotes() returns
571 # a list of messages to send to voters.
572 # We delay the sending of these messages till tables are unlocked.
573 $msgs = RemoveVotes($bug->id, 0, 'votes_bug_moved');
574 CheckIfVotedConfirmed($bug->id, Bugzilla->user->id);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000575 }
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000576
timothy@apple.comf42518d2008-02-06 20:19:16 +0000577 # Set and update flags.
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000578 Bugzilla::Flag->process($bug, undef, $timestamp, $vars);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000579
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000580 $dbh->bz_commit_transaction();
581
582 ###############
583 # Send Emails #
584 ###############
timothy@apple.comf42518d2008-02-06 20:19:16 +0000585
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000586 # Now is a good time to send email to voters.
587 foreach my $msg (@$msgs) {
588 MessageToMTA($msg);
589 }
590
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000591 my $old_qa = $changes->{'qa_contact'} ? $changes->{'qa_contact'}->[0] : '';
592 my $old_own = $changes->{'assigned_to'} ? $changes->{'assigned_to'}->[0] : '';
593 my $old_cc = $changes->{cc} ? $changes->{cc}->[0] : '';
594 $vars->{'mailrecipients'} = {
595 cc => [split(/[\s,]+/, $old_cc)],
596 owner => $old_own,
597 qacontact => $old_qa,
598 changer => Bugzilla->user->login };
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000599
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000600 $vars->{'id'} = $bug->id;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000601 $vars->{'type'} = "bug";
602
603 # Let the user know the bug was changed and who did and didn't
604 # receive email about the change.
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000605 send_results($bug->id, $vars);
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000606
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000607 # If the bug was marked as a duplicate, we need to notify users on the
608 # other bug of any changes to that bug.
609 my $new_dup_id = $changes->{'dup_id'} ? $changes->{'dup_id'}->[1] : undef;
610 if ($new_dup_id) {
timothy@apple.comf42518d2008-02-06 20:19:16 +0000611 $vars->{'mailrecipients'} = { 'changer' => Bugzilla->user->login };
612
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000613 $vars->{'id'} = $new_dup_id;
timothy@apple.comf42518d2008-02-06 20:19:16 +0000614 $vars->{'type'} = "dupe";
615
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000616 # Let the user know a duplication notation was added to the
617 # original bug.
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000618 send_results($new_dup_id, $vars);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000619 }
620
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000621 my %all_dep_changes = (%notify_deps, %changed_deps);
622 foreach my $id (sort { $a <=> $b } (keys %all_dep_changes)) {
623 $vars->{'mailrecipients'} = { 'changer' => Bugzilla->user->login };
624 $vars->{'id'} = $id;
625 $vars->{'type'} = "dep";
timothy@apple.comf42518d2008-02-06 20:19:16 +0000626
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000627 # Let the user (if he is able to see the bug) know we checked to
628 # see if we should email notice of this change to users with a
629 # relationship to the dependent bug and who did and didn't
630 # receive email about it.
631 send_results($id, $vars);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000632 }
633}
634
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000635# Determine if Patch Viewer is installed, for Diff link
636# (NB: Duplicate code with show_bug.cgi.)
637eval {
638 require PatchReader;
639 $vars->{'patchviewerinstalled'} = 1;
640};
641
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000642if (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
643 # Do nothing.
644}
645elsif ($action eq 'next_bug') {
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000646 if ($next_bug) {
647 if (detaint_natural($next_bug) && Bugzilla->user->can_see_bug($next_bug)) {
648 my $bug = new Bugzilla::Bug($next_bug);
649 ThrowCodeError("bug_error", { bug => $bug }) if $bug->error;
650
651 $vars->{'bugs'} = [$bug];
652 $vars->{'nextbug'} = $bug->bug_id;
653
654 $template->process("bug/show.html.tmpl", $vars)
655 || ThrowTemplateError($template->error());
656
657 exit;
658 }
659 }
660} elsif ($action eq 'same_bug') {
661 if (Bugzilla->user->can_see_bug($cgi->param('id'))) {
662 my $bug = new Bugzilla::Bug($cgi->param('id'));
timothy@apple.comf42518d2008-02-06 20:19:16 +0000663 ThrowCodeError("bug_error", { bug => $bug }) if $bug->error;
664
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000665 $vars->{'bugs'} = [$bug];
timothy@apple.comf42518d2008-02-06 20:19:16 +0000666
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000667 $template->process("bug/show.html.tmpl", $vars)
timothy@apple.comf42518d2008-02-06 20:19:16 +0000668 || ThrowTemplateError($template->error());
669
670 exit;
671 }
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000672} elsif ($action ne 'nothing') {
673 ThrowCodeError("invalid_post_bug_submit_action");
timothy@apple.comf42518d2008-02-06 20:19:16 +0000674}
675
676# End the response page.
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000677unless (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000678 $template->process("bug/navigate.html.tmpl", $vars)
679 || ThrowTemplateError($template->error());
680 $template->process("global/footer.html.tmpl", $vars)
681 || ThrowTemplateError($template->error());
682}
683
6841;