Lines 16-26
package GitBz::Commands::Edit;
Link Here
|
16 |
# along with git-bz; if not, see <https://www.gnu.org/licenses>. |
16 |
# along with git-bz; if not, see <https://www.gnu.org/licenses>. |
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
|
|
19 |
|
20 |
use utf8; |
21 |
|
19 |
use Getopt::Long qw(GetOptionsFromArray); |
22 |
use Getopt::Long qw(GetOptionsFromArray); |
20 |
use Try::Tiny qw(catch try); |
23 |
use Try::Tiny qw(catch try); |
21 |
use File::Temp; |
24 |
use File::Temp; |
22 |
use GitBz::Git; |
25 |
use GitBz::Git; |
23 |
use GitBz::Exception; |
26 |
use GitBz::Exception; |
|
|
27 |
use GitBz::StatusWorkflow; |
24 |
|
28 |
|
25 |
sub new { |
29 |
sub new { |
26 |
my ( $class, $commands ) = @_; |
30 |
my ( $class, $commands ) = @_; |
Lines 50-66
sub execute {
Link Here
|
50 |
if ( $arg =~ /^\d+$/ ) { |
54 |
if ( $arg =~ /^\d+$/ ) { |
51 |
|
55 |
|
52 |
# Bug reference |
56 |
# Bug reference |
53 |
$self->edit_bug( $arg, \%opts ); |
57 |
my $changed = $self->edit_bug( $arg, \%opts ); |
|
|
58 |
print "No changes made\n" unless $changed; |
54 |
} else { |
59 |
} else { |
55 |
|
60 |
|
56 |
# Commit or revision range |
61 |
# Commit or revision range |
57 |
my @commits = GitBz::Git->get_commits($arg); |
62 |
my @commits = GitBz::Git->get_commits($arg); |
58 |
GitBz::Exception->throw("No commits found") unless @commits; |
63 |
GitBz::Exception->throw("No commits found") unless @commits; |
59 |
|
64 |
|
60 |
$self->edit_commits( \@commits, \%opts ); |
65 |
my $changed = $self->edit_commits( \@commits, \%opts ); |
|
|
66 |
print "No changes made\n" unless $changed; |
61 |
} |
67 |
} |
62 |
|
68 |
|
63 |
print "Edit completed successfully\n"; |
69 |
# Success message is handled in individual methods |
64 |
} catch { |
70 |
} catch { |
65 |
GitBz::Exception->throw("Edit failed: $_"); |
71 |
GitBz::Exception->throw("Edit failed: $_"); |
66 |
}; |
72 |
}; |
Lines 76-82
sub edit_bug {
Link Here
|
76 |
my $template = $self->create_bug_template( $bug, $opts ); |
82 |
my $template = $self->create_bug_template( $bug, $opts ); |
77 |
my $edited = $self->edit_template($template); |
83 |
my $edited = $self->edit_template($template); |
78 |
|
84 |
|
79 |
$self->update_bug( $client, $bug_ref, $edited, $opts ); |
85 |
return $self->update_bug( $client, $bug_ref, $edited, $opts ); |
80 |
} |
86 |
} |
81 |
|
87 |
|
82 |
sub edit_commits { |
88 |
sub edit_commits { |
Lines 94-103
sub edit_commits {
Link Here
|
94 |
GitBz::Exception->throw("No bug references found in commits") unless %bugs; |
100 |
GitBz::Exception->throw("No bug references found in commits") unless %bugs; |
95 |
|
101 |
|
96 |
# Edit each bug |
102 |
# Edit each bug |
|
|
103 |
my $any_changed = 0; |
97 |
for my $bug_ref ( keys %bugs ) { |
104 |
for my $bug_ref ( keys %bugs ) { |
98 |
print "Editing bug $bug_ref...\n"; |
105 |
print "Editing bug $bug_ref...\n"; |
99 |
$self->edit_bug_with_commits( $bug_ref, $bugs{$bug_ref}, $opts ); |
106 |
my $changed = $self->edit_bug_with_commits( $bug_ref, $bugs{$bug_ref}, $opts ); |
|
|
107 |
$any_changed ||= $changed; |
100 |
} |
108 |
} |
|
|
109 |
|
110 |
return $any_changed; |
101 |
} |
111 |
} |
102 |
|
112 |
|
103 |
sub edit_bug_with_commits { |
113 |
sub edit_bug_with_commits { |
Lines 110-136
sub edit_bug_with_commits {
Link Here
|
110 |
my $template = $self->create_bug_template_with_commits( $bug, $commits, $opts ); |
120 |
my $template = $self->create_bug_template_with_commits( $bug, $commits, $opts ); |
111 |
my $edited = $self->edit_template($template); |
121 |
my $edited = $self->edit_template($template); |
112 |
|
122 |
|
113 |
$self->update_bug( $client, $bug_ref, $edited, $opts ); |
123 |
return $self->update_bug( $client, $bug_ref, $edited, $opts ); |
114 |
} |
124 |
} |
115 |
|
125 |
|
116 |
sub create_bug_template { |
126 |
sub create_bug_template { |
117 |
my ( $self, $bug, $opts ) = @_; |
127 |
my ( $self, $bug, $opts ) = @_; |
|
|
128 |
my $client = $self->{commands}->{client}; |
118 |
|
129 |
|
119 |
my $template = ""; |
130 |
my $template = ""; |
120 |
|
131 |
$template .= "# Bug $bug->{id} - $bug->{summary}\n\n"; |
121 |
if ( $opts->{fix} ) { |
132 |
|
122 |
$template .= "# Closing bug as FIXED\n"; |
133 |
# Show existing patches for obsoleting |
123 |
$template .= "Status: RESOLVED\n"; |
134 |
my $attachments = $client->get_attachments($bug->{id}); |
124 |
$template .= "Resolution: FIXED\n\n"; |
135 |
if ( $attachments && @$attachments ) { |
|
|
136 |
for my $patch ( @$attachments ) { |
137 |
next unless $patch->{is_patch} && !$patch->{is_obsolete}; |
138 |
$template .= "#Obsoletes: $patch->{id} - $patch->{summary}\n"; |
139 |
} |
140 |
$template .= "\n"; |
125 |
} |
141 |
} |
126 |
|
142 |
|
127 |
$template .= "# Bug $bug->{id}: $bug->{summary}\n"; |
143 |
# Add status options |
128 |
$template .= "# Product: $bug->{product}\n"; |
144 |
my $workflow = GitBz::StatusWorkflow->new($client); |
129 |
$template .= "# Component: $bug->{component}\n"; |
145 |
$template .= "# Current status: $bug->{status}\n"; |
130 |
$template .= "# Status: $bug->{status}\n\n"; |
146 |
my $status_values = $workflow->get_next_status_values($bug->{status}); |
131 |
|
147 |
for my $status (@$status_values) { |
132 |
$template .= "Comment:\n"; |
148 |
$template .= "# Status: $status\n"; |
133 |
$template .= "# Enter your comment above. Lines starting with # are ignored.\n"; |
149 |
} |
|
|
150 |
$template .= "\n"; |
151 |
|
152 |
# Add patch complexity options |
153 |
my $complexity = $bug->{cf_patch_complexity} || ""; |
154 |
$template .= "# Current patch-complexity: $complexity\n"; |
155 |
my $complexity_values = $client->get_field_values('cf_patch_complexity'); |
156 |
if ($complexity_values) { |
157 |
for my $comp (@$complexity_values) { |
158 |
$template .= "# Patch-complexity: $comp\n"; |
159 |
} |
160 |
} |
161 |
$template .= "\n"; |
162 |
|
163 |
# Add depends options |
164 |
my $depends = $bug->{depends_on} || []; |
165 |
my $depends_str = ref($depends) eq 'ARRAY' ? join(' ', @$depends) : $depends; |
166 |
$template .= "# Current depends: $depends_str\n"; |
167 |
$template .= "# Depends: bug xxxx\n"; |
168 |
$template .= "# Depends: bug yyyy\n"; |
169 |
$template .= "\n"; |
170 |
|
171 |
$template .= "# Enter comment below. Lines starting with '#' will be ignored.\n"; |
172 |
$template .= "# To obsolete patches, uncomment the appropriate Obsoletes lines.\n"; |
134 |
|
173 |
|
135 |
return $template; |
174 |
return $template; |
136 |
} |
175 |
} |
Lines 159-209
sub edit_template {
Link Here
|
159 |
my ( $self, $template ) = @_; |
198 |
my ( $self, $template ) = @_; |
160 |
|
199 |
|
161 |
my $temp = File::Temp->new( SUFFIX => '.txt' ); |
200 |
my $temp = File::Temp->new( SUFFIX => '.txt' ); |
|
|
201 |
binmode $temp, ':utf8'; |
162 |
print $temp $template; |
202 |
print $temp $template; |
163 |
close $temp; |
203 |
close $temp; |
164 |
|
204 |
|
165 |
my $editor = $ENV{EDITOR} || 'vi'; |
205 |
my $editor = $ENV{EDITOR} || $ENV{GIT_EDITOR} || 'vi'; |
166 |
system( $editor, $temp->filename ); |
206 |
system( $editor, $temp->filename ); |
167 |
|
207 |
|
168 |
open my $fh, '<', $temp->filename or die "Cannot read temp file: $!"; |
208 |
open my $fh, '<:utf8', $temp->filename or die "Cannot read temp file: $!"; |
169 |
my $content = do { local $/; <$fh> }; |
209 |
my $content = do { local $/; <$fh> }; |
170 |
close $fh; |
210 |
close $fh; |
171 |
|
211 |
|
172 |
# Remove comment lines |
212 |
return $content; |
173 |
my @lines = grep { !/^#/ } split /\n/, $content; |
|
|
174 |
return join( "\n", @lines ); |
175 |
} |
213 |
} |
176 |
|
214 |
|
177 |
sub update_bug { |
215 |
sub update_bug { |
178 |
my ( $self, $client, $bug_ref, $edited, $opts ) = @_; |
216 |
my ( $self, $client, $bug_ref, $edited, $opts ) = @_; |
179 |
|
217 |
|
|
|
218 |
my @lines = split /\n/, $edited; |
219 |
my @non_comment_lines = grep { !/^#/ && /\S/ } @lines; |
220 |
|
221 |
# Early return if no non-comment lines - no API calls needed |
222 |
return 0 unless @non_comment_lines; |
223 |
|
224 |
my @obsoletes; |
225 |
my @comment_lines; |
180 |
my %update_params; |
226 |
my %update_params; |
181 |
|
227 |
|
182 |
# Parse edited content |
228 |
for my $line (@non_comment_lines) { |
183 |
my @lines = split /\n/, $edited; |
229 |
if ( $line =~ /^\s*Status\s*:\s*(.+)/ ) { |
184 |
my $comment = ''; |
|
|
185 |
my $in_comment = 0; |
186 |
|
187 |
for my $line (@lines) { |
188 |
if ( $line =~ /^Status:\s*(.+)/ ) { |
189 |
$update_params{status} = $1; |
230 |
$update_params{status} = $1; |
190 |
} elsif ( $line =~ /^Resolution:\s*(.+)/ ) { |
231 |
} elsif ( $line =~ /^\s*Resolution\s*:\s*(.+)/ ) { |
191 |
$update_params{resolution} = $1; |
232 |
$update_params{resolution} = $1; |
192 |
} elsif ( $line =~ /^Comment:/ ) { |
233 |
} elsif ( $line =~ /^\s*Patch-complexity\s*:\s*(.+)/ ) { |
193 |
$in_comment = 1; |
234 |
$update_params{cf_patch_complexity} = $1; |
194 |
} elsif ( $in_comment && $line =~ /\S/ ) { |
235 |
} elsif ( $line =~ /^\s*Depends\s*:\s*([Bb][Uu][Gg])?\s*(\d+)/ ) { |
195 |
$comment .= $line . "\n"; |
236 |
push @{ $update_params{depends_on} }, $2; |
|
|
237 |
} elsif ( $line =~ /^\s*Obsoletes\s*:\s*(\d+)/ ) { |
238 |
push @obsoletes, $1; |
239 |
} else { |
240 |
push @comment_lines, $line; |
196 |
} |
241 |
} |
197 |
} |
242 |
} |
198 |
|
243 |
|
|
|
244 |
my $comment = join( "\n", @comment_lines ); |
245 |
$comment =~ s/^\s+|\s+$//g; |
246 |
|
247 |
# Early return if no changes |
248 |
return 0 unless %update_params || $comment || @obsoletes; |
249 |
|
250 |
# Only fetch bug data if we have changes to process |
251 |
my $bug = $client->get_bug($bug_ref); |
252 |
my $changed = 0; |
253 |
|
254 |
# Check if there are actual changes before making API call |
255 |
my %actual_changes; |
256 |
if ($update_params{status} && $update_params{status} ne $bug->{status}) { |
257 |
$actual_changes{status} = $update_params{status}; |
258 |
} |
259 |
if ($update_params{resolution} && $update_params{resolution} ne ($bug->{resolution} || '')) { |
260 |
$actual_changes{resolution} = $update_params{resolution}; |
261 |
} |
262 |
if ($update_params{cf_patch_complexity} && $update_params{cf_patch_complexity} ne ($bug->{cf_patch_complexity} || '')) { |
263 |
$actual_changes{cf_patch_complexity} = $update_params{cf_patch_complexity}; |
264 |
} |
265 |
if ($update_params{depends_on}) { |
266 |
$actual_changes{depends_on} = $update_params{depends_on}; |
267 |
} |
199 |
if ($comment) { |
268 |
if ($comment) { |
200 |
$update_params{comment} = { body => $comment }; |
269 |
$actual_changes{comment} = { body => $comment }; |
201 |
} |
270 |
} |
202 |
|
271 |
|
203 |
if (%update_params) { |
272 |
if (%actual_changes) { |
204 |
$client->update_bug( $bug_ref, %update_params ); |
273 |
print "Updating bug $bug_ref\n"; |
205 |
print "Updated bug $bug_ref\n"; |
274 |
|
|
|
275 |
# Show field changes |
276 |
if ($actual_changes{status}) { |
277 |
print "Status changed: $bug->{status} → $actual_changes{status}\n"; |
278 |
} |
279 |
if ($actual_changes{resolution}) { |
280 |
my $old = $bug->{resolution} || 'none'; |
281 |
print "Resolution changed: $old → $actual_changes{resolution}\n"; |
282 |
} |
283 |
if ($actual_changes{cf_patch_complexity}) { |
284 |
my $old = $bug->{cf_patch_complexity} || 'none'; |
285 |
print "Patch-complexity changed: $old → $actual_changes{cf_patch_complexity}\n"; |
286 |
} |
287 |
if ($actual_changes{comment}) { |
288 |
print "Added comment\n"; |
289 |
} |
290 |
|
291 |
$client->update_bug( $bug_ref, %actual_changes ); |
292 |
$changed = 1; |
293 |
} |
294 |
|
295 |
# Handle obsoleting attachments |
296 |
if (@obsoletes) { |
297 |
my $attachments = $client->get_attachments($bug_ref); |
298 |
my %attach_lookup = map { $_->{id} => $_->{summary} } @$attachments; |
299 |
|
300 |
for my $attach_id (@obsoletes) { |
301 |
$client->obsolete_attachment($attach_id); |
302 |
my $summary = $attach_lookup{$attach_id} || "Unknown"; |
303 |
print "Obsoleted attachment $attach_id - $summary\n"; |
304 |
$changed = 1; |
305 |
} |
206 |
} |
306 |
} |
|
|
307 |
|
308 |
return $changed; |
207 |
} |
309 |
} |
208 |
|
310 |
|
209 |
sub extract_bug_ref { |
311 |
sub extract_bug_ref { |