Lines 17-30
package GitBz::Commands::Edit;
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use utf8; |
|
|
21 |
|
22 |
use Getopt::Long qw(GetOptionsFromArray); |
20 |
use Getopt::Long qw(GetOptionsFromArray); |
23 |
use Try::Tiny qw(catch try); |
21 |
use Try::Tiny qw(catch try); |
24 |
use File::Temp; |
22 |
use File::Temp; |
25 |
use GitBz::Git; |
23 |
use GitBz::Git; |
26 |
use GitBz::Exception; |
24 |
use GitBz::Exception; |
27 |
use GitBz::StatusWorkflow; |
25 |
use GitBz::StatusWorkflow; |
|
|
26 |
use GitBz::Bug; |
28 |
|
27 |
|
29 |
sub new { |
28 |
sub new { |
30 |
my ( $class, $commands ) = @_; |
29 |
my ( $class, $commands ) = @_; |
Lines 76-83
sub edit_bug {
Link Here
|
76 |
my ( $self, $bug_ref, $opts ) = @_; |
75 |
my ( $self, $bug_ref, $opts ) = @_; |
77 |
|
76 |
|
78 |
my $client = $self->{commands}->{client}; |
77 |
my $client = $self->{commands}->{client}; |
79 |
my $bug = $client->get_bug($bug_ref); |
78 |
my $bug = GitBz::Bug->get( $client, $bug_ref ); |
80 |
GitBz::Exception->throw("Bug $bug_ref not found") unless $bug; |
|
|
81 |
|
79 |
|
82 |
my $template = $self->create_bug_template( $bug, $opts ); |
80 |
my $template = $self->create_bug_template( $bug, $opts ); |
83 |
my $edited = $self->edit_template($template); |
81 |
my $edited = $self->edit_template($template); |
Lines 106-112
sub edit_commits {
Link Here
|
106 |
my $changed = $self->edit_bug_with_commits( $bug_ref, $bugs{$bug_ref}, $opts ); |
104 |
my $changed = $self->edit_bug_with_commits( $bug_ref, $bugs{$bug_ref}, $opts ); |
107 |
$any_changed ||= $changed; |
105 |
$any_changed ||= $changed; |
108 |
} |
106 |
} |
109 |
|
107 |
|
110 |
return $any_changed; |
108 |
return $any_changed; |
111 |
} |
109 |
} |
112 |
|
110 |
|
Lines 114-121
sub edit_bug_with_commits {
Link Here
|
114 |
my ( $self, $bug_ref, $commits, $opts ) = @_; |
112 |
my ( $self, $bug_ref, $commits, $opts ) = @_; |
115 |
|
113 |
|
116 |
my $client = $self->{commands}->{client}; |
114 |
my $client = $self->{commands}->{client}; |
117 |
my $bug = $client->get_bug($bug_ref); |
115 |
my $bug = GitBz::Bug->get( $client, $bug_ref ); |
118 |
GitBz::Exception->throw("Bug $bug_ref not found") unless $bug; |
|
|
119 |
|
116 |
|
120 |
my $template = $self->create_bug_template_with_commits( $bug, $commits, $opts ); |
117 |
my $template = $self->create_bug_template_with_commits( $bug, $commits, $opts ); |
121 |
my $edited = $self->edit_template($template); |
118 |
my $edited = $self->edit_template($template); |
Lines 128-156
sub create_bug_template {
Link Here
|
128 |
my $client = $self->{commands}->{client}; |
125 |
my $client = $self->{commands}->{client}; |
129 |
|
126 |
|
130 |
my $template = ""; |
127 |
my $template = ""; |
131 |
$template .= "# Bug $bug->{id} - $bug->{summary}\n\n"; |
128 |
$template .= "# Bug " . $bug->id . " - " . $bug->summary . "\n\n"; |
132 |
|
129 |
|
133 |
# Show existing patches for obsoleting |
130 |
# Show existing patches for obsoleting |
134 |
my $attachments = $client->get_attachments($bug->{id}); |
131 |
my $attachments = $bug->attachments; |
135 |
if ( $attachments && @$attachments ) { |
132 |
if ( $attachments && @$attachments ) { |
136 |
for my $patch ( @$attachments ) { |
133 |
for my $patch (@$attachments) { |
137 |
next unless $patch->{is_patch} && !$patch->{is_obsolete}; |
134 |
next unless $patch->{is_patch} && !$patch->{is_obsolete}; |
138 |
$template .= "#Obsoletes: $patch->{id} - $patch->{summary}\n"; |
135 |
$template .= "#Obsoletes: $patch->{id} - $patch->{summary}\n"; |
139 |
} |
136 |
} |
140 |
$template .= "\n"; |
137 |
$template .= "\n"; |
141 |
} |
138 |
} |
142 |
|
139 |
|
143 |
# Add status options |
140 |
# Add status options |
144 |
my $workflow = GitBz::StatusWorkflow->new($client); |
141 |
my $workflow = GitBz::StatusWorkflow->new($client); |
145 |
$template .= "# Current status: $bug->{status}\n"; |
142 |
$template .= "# Current status: " . $bug->status . "\n"; |
146 |
my $status_values = $workflow->get_next_status_values($bug->{status}); |
143 |
my $status_values = $workflow->get_next_status_values( $bug->status ); |
147 |
for my $status (@$status_values) { |
144 |
for my $status (@$status_values) { |
148 |
$template .= "# Status: $status\n"; |
145 |
$template .= "# Status: $status\n"; |
149 |
} |
146 |
} |
150 |
$template .= "\n"; |
147 |
$template .= "\n"; |
151 |
|
148 |
|
152 |
# Add patch complexity options |
149 |
# Add patch complexity options |
153 |
my $complexity = $bug->{cf_patch_complexity} || ""; |
150 |
my $complexity = $bug->cf_patch_complexity || ""; |
154 |
$template .= "# Current patch-complexity: $complexity\n"; |
151 |
$template .= "# Current patch-complexity: $complexity\n"; |
155 |
my $complexity_values = $client->get_field_values('cf_patch_complexity'); |
152 |
my $complexity_values = $client->get_field_values('cf_patch_complexity'); |
156 |
if ($complexity_values) { |
153 |
if ($complexity_values) { |
Lines 159-173
sub create_bug_template {
Link Here
|
159 |
} |
156 |
} |
160 |
} |
157 |
} |
161 |
$template .= "\n"; |
158 |
$template .= "\n"; |
162 |
|
159 |
|
163 |
# Add depends options |
160 |
# Add depends options |
164 |
my $depends = $bug->{depends_on} || []; |
161 |
my $depends_list = $bug->depends_on; |
165 |
my $depends_str = ref($depends) eq 'ARRAY' ? join(' ', @$depends) : $depends; |
162 |
my $depends_str = @$depends_list ? join( ' ', @$depends_list ) : ''; |
166 |
$template .= "# Current depends: $depends_str\n"; |
163 |
$template .= "# Current depends: $depends_str\n"; |
|
|
164 |
|
165 |
# Show current depends uncommented |
166 |
for my $dep (@$depends_list) { |
167 |
$template .= "Depends: bug $dep\n"; |
168 |
} |
169 |
|
170 |
# Show skeleton commented |
167 |
$template .= "# Depends: bug xxxx\n"; |
171 |
$template .= "# Depends: bug xxxx\n"; |
168 |
$template .= "# Depends: bug yyyy\n"; |
172 |
$template .= "# Depends: bug yyyy\n"; |
169 |
$template .= "\n"; |
173 |
$template .= "\n"; |
170 |
|
174 |
|
171 |
$template .= "# Enter comment below. Lines starting with '#' will be ignored.\n"; |
175 |
$template .= "# Enter comment below. Lines starting with '#' will be ignored.\n"; |
172 |
$template .= "# To obsolete patches, uncomment the appropriate Obsoletes lines.\n"; |
176 |
$template .= "# To obsolete patches, uncomment the appropriate Obsoletes lines.\n"; |
173 |
|
177 |
|
Lines 217-230
sub update_bug {
Link Here
|
217 |
|
221 |
|
218 |
my @lines = split /\n/, $edited; |
222 |
my @lines = split /\n/, $edited; |
219 |
my @non_comment_lines = grep { !/^#/ && /\S/ } @lines; |
223 |
my @non_comment_lines = grep { !/^#/ && /\S/ } @lines; |
220 |
|
224 |
|
221 |
# Early return if no non-comment lines - no API calls needed |
225 |
# Early return if no non-comment lines - no API calls needed |
222 |
return 0 unless @non_comment_lines; |
226 |
return 0 unless @non_comment_lines; |
223 |
|
227 |
|
224 |
my @obsoletes; |
228 |
my @obsoletes; |
225 |
my @comment_lines; |
229 |
my @comment_lines; |
226 |
my %update_params; |
230 |
my %update_params; |
227 |
|
231 |
|
228 |
for my $line (@non_comment_lines) { |
232 |
for my $line (@non_comment_lines) { |
229 |
if ( $line =~ /^\s*Status\s*:\s*(.+)/ ) { |
233 |
if ( $line =~ /^\s*Status\s*:\s*(.+)/ ) { |
230 |
$update_params{status} = $1; |
234 |
$update_params{status} = $1; |
Lines 240-310
sub update_bug {
Link Here
|
240 |
push @comment_lines, $line; |
244 |
push @comment_lines, $line; |
241 |
} |
245 |
} |
242 |
} |
246 |
} |
243 |
|
247 |
|
244 |
my $comment = join( "\n", @comment_lines ); |
248 |
my $comment = join( "\n", @comment_lines ); |
245 |
$comment =~ s/^\s+|\s+$//g; |
249 |
$comment =~ s/^\s+|\s+$//g; |
246 |
|
250 |
|
247 |
# Early return if no changes |
251 |
# Early return if no changes |
248 |
return 0 unless %update_params || $comment || @obsoletes; |
252 |
return 0 unless %update_params || $comment || @obsoletes; |
249 |
|
253 |
|
250 |
# Only fetch bug data if we have changes to process |
254 |
# Only fetch bug data if we have changes to process |
251 |
my $bug = $client->get_bug($bug_ref); |
255 |
my $bug = GitBz::Bug->get( $client, $bug_ref ); |
252 |
my $changed = 0; |
256 |
my $changed = 0; |
253 |
|
257 |
|
254 |
# Check if there are actual changes before making API call |
258 |
# Check if there are actual changes before making API call |
255 |
my %actual_changes; |
259 |
my %actual_changes; |
256 |
if ($update_params{status} && $update_params{status} ne $bug->{status}) { |
260 |
if ( $update_params{status} && $update_params{status} ne $bug->status ) { |
257 |
$actual_changes{status} = $update_params{status}; |
261 |
$actual_changes{status} = $update_params{status}; |
258 |
} |
262 |
} |
259 |
if ($update_params{resolution} && $update_params{resolution} ne ($bug->{resolution} || '')) { |
263 |
if ( $update_params{resolution} && $update_params{resolution} ne ( $bug->resolution || '' ) ) { |
260 |
$actual_changes{resolution} = $update_params{resolution}; |
264 |
$actual_changes{resolution} = $update_params{resolution}; |
261 |
} |
265 |
} |
262 |
if ($update_params{cf_patch_complexity} && $update_params{cf_patch_complexity} ne ($bug->{cf_patch_complexity} || '')) { |
266 |
if ( $update_params{cf_patch_complexity} |
|
|
267 |
&& $update_params{cf_patch_complexity} ne ( $bug->cf_patch_complexity || '' ) ) |
268 |
{ |
263 |
$actual_changes{cf_patch_complexity} = $update_params{cf_patch_complexity}; |
269 |
$actual_changes{cf_patch_complexity} = $update_params{cf_patch_complexity}; |
264 |
} |
270 |
} |
265 |
if ($update_params{depends_on}) { |
271 |
if ( $update_params{depends_on} ) { |
266 |
$actual_changes{depends_on} = $update_params{depends_on}; |
272 |
my @new_depends = @{ $update_params{depends_on} }; |
|
|
273 |
my @old_depends = @{ $bug->depends_on }; |
274 |
|
275 |
my @to_add = grep { |
276 |
my $new = $_; |
277 |
!grep { $_ eq $new } @old_depends |
278 |
} @new_depends; |
279 |
my @to_remove = grep { |
280 |
my $old = $_; |
281 |
!grep { $_ eq $old } @new_depends |
282 |
} @old_depends; |
283 |
|
284 |
if ( @to_add || @to_remove ) { |
285 |
my %depends_update; |
286 |
$depends_update{add} = \@to_add if @to_add; |
287 |
$depends_update{remove} = \@to_remove if @to_remove; |
288 |
$actual_changes{depends_on} = \%depends_update; |
289 |
} |
267 |
} |
290 |
} |
268 |
if ($comment) { |
291 |
if ($comment) { |
269 |
$actual_changes{comment} = { body => $comment }; |
292 |
$actual_changes{comment} = { body => $comment }; |
270 |
} |
293 |
} |
271 |
|
294 |
|
272 |
if (%actual_changes) { |
295 |
if (%actual_changes) { |
273 |
print "Updating bug $bug_ref\n"; |
296 |
print "Updating bug $bug_ref:\n"; |
274 |
|
297 |
|
275 |
# Show field changes |
298 |
# Show field changes |
276 |
if ($actual_changes{status}) { |
299 |
if ( $actual_changes{status} ) { |
277 |
print "Status changed: $bug->{status} → $actual_changes{status}\n"; |
300 |
print " ✓ Status: " . $bug->status . " → $actual_changes{status}\n"; |
|
|
301 |
} |
302 |
if ( $actual_changes{resolution} ) { |
303 |
my $old = $bug->resolution || 'none'; |
304 |
print " ✓ Resolution: $old → $actual_changes{resolution}\n"; |
278 |
} |
305 |
} |
279 |
if ($actual_changes{resolution}) { |
306 |
if ( $actual_changes{cf_patch_complexity} ) { |
280 |
my $old = $bug->{resolution} || 'none'; |
307 |
my $old = $bug->cf_patch_complexity || 'none'; |
281 |
print "Resolution changed: $old → $actual_changes{resolution}\n"; |
308 |
print " ✓ Patch-complexity: $old → $actual_changes{cf_patch_complexity}\n"; |
282 |
} |
309 |
} |
283 |
if ($actual_changes{cf_patch_complexity}) { |
310 |
if ( $actual_changes{comment} ) { |
284 |
my $old = $bug->{cf_patch_complexity} || 'none'; |
311 |
print " ✓ Added comment\n"; |
285 |
print "Patch-complexity changed: $old → $actual_changes{cf_patch_complexity}\n"; |
|
|
286 |
} |
312 |
} |
287 |
if ($actual_changes{comment}) { |
313 |
if ( $actual_changes{depends_on} ) { |
288 |
print "Added comment\n"; |
314 |
my $depends_change = $actual_changes{depends_on}; |
|
|
315 |
if ( $depends_change->{add} ) { |
316 |
print " ✓ Depends: added " . join( ' ', @{ $depends_change->{add} } ) . "\n"; |
317 |
} |
318 |
if ( $depends_change->{remove} ) { |
319 |
print " ✓ Depends: removed " . join( ' ', @{ $depends_change->{remove} } ) . "\n"; |
320 |
} |
289 |
} |
321 |
} |
290 |
|
322 |
|
291 |
$client->update_bug( $bug_ref, %actual_changes ); |
323 |
$bug->update(%actual_changes); |
292 |
$changed = 1; |
324 |
$changed = 1; |
293 |
} |
325 |
} |
294 |
|
326 |
|
295 |
# Handle obsoleting attachments |
327 |
# Handle obsoleting attachments |
296 |
if (@obsoletes) { |
328 |
if (@obsoletes) { |
297 |
my $attachments = $client->get_attachments($bug_ref); |
329 |
my $attachments = $bug->attachments; |
298 |
my %attach_lookup = map { $_->{id} => $_->{summary} } @$attachments; |
330 |
my %attach_lookup = map { $_->{id} => $_->{summary} } @$attachments; |
299 |
|
331 |
|
300 |
for my $attach_id (@obsoletes) { |
332 |
for my $attach_id (@obsoletes) { |
301 |
$client->obsolete_attachment($attach_id); |
333 |
$bug->obsolete_attachment($attach_id); |
302 |
my $summary = $attach_lookup{$attach_id} || "Unknown"; |
334 |
my $summary = $attach_lookup{$attach_id} || "Unknown"; |
303 |
print "Obsoleted attachment $attach_id - $summary\n"; |
335 |
print " ✓ Obsoleted attachment $attach_id - $summary\n"; |
304 |
$changed = 1; |
336 |
$changed = 1; |
305 |
} |
337 |
} |
306 |
} |
338 |
} |
307 |
|
339 |
|
308 |
return $changed; |
340 |
return $changed; |
309 |
} |
341 |
} |
310 |
|
342 |
|