blib/lib/Git/Hooks/CheckYoutrack.pm untested

filter: Statementstmt 0.0% 0 / 177 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 33 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 283 CC 22 SCAR 30.0
CC
22
Cov
0%
_get_ticket
42.0
_process_ticket
42.0
check_commit_msg
30.0
CRAP 20.0
1 # ========================================================================== #
2 # lib/Git/Hooks/Youtrack.pm - Github Hooks for youtrack
3 # ========================================================================== #
4  
5 package Git::Hooks::CheckYoutrack;
6  
7 use strict;
8 use warnings;
9 use utf8;
10 use Log::Any '$log';
11 use Path::Tiny;
12 use Git::Hooks;
13 use LWP::UserAgent;
14 use URI::Builder;
15 use JSON::XS;
16  
17 $Git::Hooks::CheckYoutrack::VERSION = '1.0.1';
18  
19 =head1 NAME
20  
21 Git::Hooks::CheckYoutrack - Git::Hooks plugin which requires youtrack ticket number on each commit message
22  
23 =head1 SYNOPSIS
24  
25 As a C<Git::Hooks> plugin you don't use this Perl module directly. Instead, you
26 may configure it in a Git configuration file like this:
27  
28 [githooks]
29
30 # Enable the plugin
31 plugin = CheckYoutrack
32  
33 [githooks "checkyoutrack"]
34  
35 # '/youtrack' will be appended to this host
36 youtrack-host = "https://example.myjetbrains.com"
37  
38 # Refer: https://www.jetbrains.com/help/youtrack/standalone/Manage-Permanent-Token.html
39 # to create a Bearer token
40 youtrack-token = "<your-token>"
41  
42 # Regular expression to match for Youtrack ticket id
43 matchkey = '^((?:P|M)(?:AY|\d+)-\d+)'
44  
45 # Setting this flag will aborts the commit if valid Youtrack number not found
46 # Shows a warning message otherwise - default false
47 required = true
48  
49 # Print the fetched youtrack ticket details like Assignee, State etc..,
50 # default false
51 print-info = true
52  
53  
54 =head1 DESCRIPTION
55  
56 This plugin hooks the following git hooks to guarantee that every commit message
57 cites a valid Youtrack Id in the log message, so that you can be certain that
58 every commit message has a valid link to the Youtrack ticket. Refer L<Git::Hooks Usage|https://metacpan.org/pod/Git::Hooks#USAGE>
59 for steps to install and use Git::Hooks
60  
61 This plugin also hooks prepare-commit-msg to pre-populate youtrack ticket sumary on the
62 commit message if the current working branch name is starting with the valid ticket number
63  
64 =head1 METHODS
65  
66 =cut
67  
68 my $PKG = __PACKAGE__;
69 (my $CFG = __PACKAGE__) =~ s/.*::/githooks./;
70  
71 # =========================================================================== #
72  
73 =head2 B<commit-msg>, B<applypatch-msg>
74
75 These hooks are invoked during the commit, to check if the commit message
76 starts with a valid Youtrack ticket Id.
77  
78 =cut
79  
80 sub check_commit_msg {
81 my ($git, $message) = @_;
82  
83 # Skip for empty message
84 return 'no_check' if (!$message || $message =~ /^[\n\r]$/g);
85  
86 my $yt_id = _get_youtrack_id($git, $message);
87  
88 if (!$yt_id) {
89 return _show_error($git, "Missing youtrack ticket id in your message: $message");
90 }
91  
92 $log->debug("Found Youtrack ticket id from message as: $yt_id");
93  
94 my $yt_ticket = _get_ticket($git, $yt_id);
95  
96 if (!$yt_ticket) {
97 return _show_error($git, "Youtrack ticket not found with ID: $yt_id");
98 }
99  
100 if ($yt_ticket && $git->get_config_boolean($CFG => 'print-info')) {
101 print '-' x 80 . "\n";
102 print "Youtrack ticket: $yt_ticket->{ticket_id}\n";
103 print "Summary: $yt_ticket->{summary}\n";
104 print "Current status: $yt_ticket->{State}\n";
105 print "Assigned to: $yt_ticket->{Assignee}\n";
106 print "Ticket Link: $yt_ticket->{WebLink}\n";
107 print '-' x 80 . "\n";
108 }
109  
110 return 0;
111 }
112  
113 # =========================================================================== #
114  
115 sub check_message_file {
116 my ($git, $commit_msg_file) = @_;
117  
118 $log->debug(__PACKAGE__ . "::check_message_file($commit_msg_file)");
119  
120 _setup_config($git);
121  
122 my $msg = _get_message_from_file($git, $commit_msg_file);
123  
124 # Remove comment lines from the message file contents.
125 $msg =~ s/^#[^\n]*\n//mgs;
126  
127 return check_commit_msg($git, $msg);
128 }
129  
130 # =========================================================================== #
131  
132 sub _show_error {
133 my ($git, $msg) = @_;
134 if ($git->get_config_boolean($CFG => 'required')) {
135 $git->fault("ERROR: $msg");
136 return 1;
137 }
138 else {
139 print "WARNING: $msg\n";
140 return 0;
141 }
142 }
143  
144 # =========================================================================== #
145  
146 =head2 B<update>
147  
148 This hook is for remote repository and should be installed and configured at the remote git server.
149 Checks for youtrack ticket on each commit message pushed to the remote repository and deny push
150 if its not found and its required = true in the config, shows a warning message on client side
151 if config required = false but accepts the push.
152  
153 =cut
154  
155 sub check_affected_refs {
156 my ($git) = @_;
157  
158 $log->debug(__PACKAGE__ . "::check_affected_refs");
159  
160 _setup_config($git);
161  
162 my $errors = 0;
163  
164 foreach my $ref ($git->get_affected_refs()) {
165 next unless $git->is_reference_enabled($ref);
166 check_ref($git, $ref)
167 or ++$errors;
168 }
169  
170 if($errors) {
171 return _show_error($git, "Some of your commit message missing a valid youtrack ticket");
172 }
173  
174 return 0;
175 }
176  
177 # =========================================================================== #
178  
179 sub check_ref {
180 my ($git, $ref) = @_;
181  
182 my $errors = 0;
183  
184 foreach my $commit ($git->get_affected_ref_commits($ref)) {
185 check_commit_msg($git, $commit->message) or ++$errors;
186 }
187  
188 return $errors == 0;
189 }
190  
191 # =========================================================================== #
192  
193 =head2 B<prepare-commit-msg>
194
195 This hook is invoked before a commit, to check if the current branch name start with
196 a valid youtrack ticket id and pre-populates the commit message with youtrack ticket: summary
197  
198 =cut
199  
200 sub add_youtrack_summary {
201 my ($git, $commit_msg_file) = @_;
202  
203 $log->debug(__PACKAGE__ . "::add_youtrack_summary($commit_msg_file)");
204  
205 _setup_config($git);
206  
207 my $msg = _get_message_from_file($git, $commit_msg_file);
208 my $msg_copy = $msg;
209  
210 # Remove comment lines and empty lines from the message file contents.
211 $msg =~ s/^#[^\n]*\n//mgs;
212 $msg =~ s/^\n*\n$//msg;
213  
214 # Don't do anything if message already exist (user used -m option)
215 if ($msg) {
216 $log->debug("Message exist: $msg");
217 return 0;
218 }
219  
220 my $current_branch = $git->get_current_branch();
221  
222 # Extract current branch name
223 $current_branch =~ s/.+\/(.+)/$1/;
224  
225 my $yt_id = _get_youtrack_id($git, $current_branch);
226  
227 if (!$yt_id) {
228 $log->warn("No youtrack id in your working branch");
229 return 0;
230 }
231  
232 my $task = _get_ticket($git, $yt_id);
233  
234 if (!$task) {
235 $log->warn("Your branch name does not match with youtrack ticket");
236 return 0;
237 }
238  
239 my $ticket_msg = "$yt_id: $task->{summary}\n";
240  
241 $log->debug("Pre-populating commit message as: $ticket_msg");
242  
243 open my $out, '>', path($commit_msg_file) or die "Can't write new file: $!";
244 print $out $ticket_msg;
245 print $out $msg_copy;
246 close $out;
247 }
248  
249 # =========================================================================== #
250  
251 sub _get_message_from_file {
252 my ($git, $file) = @_;
253  
254 my $msg = eval { path($file)->slurp };
255 defined $msg
256 or $git->fault("Cannot open file '$file' for reading:", {details => $@})
257 and return 0;
258  
259 return $msg;
260 }
261  
262 # =========================================================================== #
263  
264 # Setup default configs
265 sub _setup_config {
266 my ($git) = @_;
267  
268 my $config = $git->get_config();
269  
270 $config->{lc $CFG} //= {};
271  
272 my $default = $config->{lc $CFG};
273  
274 # Default matchkey for matching Youtrack ids (P3-1234 || PAY-1234) keys.
275 $default->{matchkey} //= ['^((?:P|M)(?:AY|\d+)-\d+)'];
276  
277 $default->{required} //= ['false'];
278  
279 $default->{'print-info'} //= ['false'];
280  
281 return;
282 }
283  
284 # =========================================================================== #
285  
286 # Tries to get a valid youtrack ticket and return a HashRef with ticket details if found success
287 sub _get_ticket {
288 my ($git, $ticket_id) = @_;
289  
290 my $yt_token = $git->get_config($CFG => 'youtrack-token');
291  
292 $yt_token = $ENV{YoutrackToken} if (!$yt_token);
293  
294 if (!$yt_token) {
295 my $error = "Please set Youtrack permanent token in ENV YoutrackToken\n";
296 $error .= "Refer: https://www.jetbrains.com/help/youtrack/standalone/Manage-Permanent-Token.html\n";
297 $error .= "to generate a token\n";
298 $git->fault($error);
299 return;
300 }
301  
302 my $yt_host = $git->get_config($CFG => 'youtrack-host');
303 my $yt = URI::Builder->new(uri => $yt_host);
304  
305 my $ua = $git->{yt_ua} //= LWP::UserAgent->new();
306 $ua->default_header('Authorization' => "Bearer $yt_token");
307 $ua->default_header('Accept' => 'application/json');
308 $ua->default_header('Content-Type' => 'application/json');
309  
310 my $ticket_fields = 'fields=numberInProject,project(shortName),summary,customFields(name,value(name))';
311  
312 $yt->path_segments("youtrack", "api", "issues", $ticket_id);
313  
314 my $url = $yt->as_string . "?$ticket_fields";
315  
316 my $ticket = $ua->get($url);
317  
318 if (!$ticket->is_success) {
319 $log->debug("Youtrack fetch failed");
320 return;
321 }
322  
323 my $json = decode_json($ticket->decoded_content);
324 my $ticket_details = _process_ticket($json);
325  
326 if (!$ticket_details->{ticket_id}) {
327 $log->debug("No valid youtrack ticket found");
328 return;
329 }
330  
331 $ticket_details->{Assignee} = 'Unassigned' if (!$ticket_details->{Assignee});
332  
333 $yt->path_segments('youtrack', 'issue', $ticket_id);
334 $ticket_details->{WebLink} = $yt->as_string;
335  
336 return $ticket_details;
337 }
338  
339 # =========================================================================== #
340  
341 # Helper method to process the response from Youtrack API
342 sub _process_ticket {
343 my $json = shift;
344  
345 return if (!$json);
346 my $ticket;
347  
348 $ticket->{summary} = $json->{summary};
349 $ticket->{type} = $json->{'$' . 'type'};
350 $ticket->{ticket_id} = $json->{numberInProject};
351  
352 if ($json->{project} && $json->{project}->{shortName}) {
353 $ticket->{ticket_id} = $json->{project}->{shortName} . '-' . $ticket->{ticket_id};
354 }
355  
356 if ($json->{customFields}) {
357 foreach my $field (@{$json->{customFields}}) {
358  
359 if (ref $field->{value} eq 'HASH') {
360 $ticket->{$field->{name}} = $field->{value}->{name};
361 }
362 elsif (ref $field->{value} eq 'ARRAY') {
363 foreach my $val (@{$field->{value}}) {
364 $ticket->{$field->{name}} = join(',', $val->{name});
365 }
366 }
367 else {
368 $ticket->{$field->{name}} = $field->{value};
369 }
370 }
371 }
372  
373 return $ticket;
374 }
375  
376 # =========================================================================== #
377  
378 # Check and return a youtrack Id in the given string based on the matchkey regex
379 sub _get_youtrack_id {
380 my ($git, $message) = @_;
381  
382 my $matchkey = $git->get_config($CFG => 'matchkey');
383  
384 if ($message =~ /$matchkey/i) {
385 return uc($1);
386 }
387  
388 return;
389 }
390  
391 # =========================================================================== #
392  
393 =head1 USAGE INSTRUCTION
394  
395 Create a generic script that will be invoked by Git for every hook. Go to hooks directory of your repository,
396 for local repository it is .git/hooks/ and for remote server it is ./hooks/ and create a simple executable perl script
397  
398 $ cd /path/to/repo/.git/hooks
399
400 $ cat >git-hooks.pl <<'EOT'
401 #!/usr/bin/env perl
402 use Git::Hooks;
403 run_hook($0, @ARGV);
404 EOT
405
406 $ chmod +x git-hooks.pl
407  
408 Now you should create symbolic links pointing to this perl script for each hook you are interested in
409  
410 For local repository
411  
412 $ cd /path/to/repo/.git/hooks
413  
414 $ ln -s git-hooks.pl commit-msg
415 $ ln -s git-hooks.pl applypatch-msg
416 $ ln -s git-hooks.pl prepare-commit-msg
417  
418 For remote repository
419  
420 $ cd /path/to/repo/hooks
421  
422 $ ln -s git-hooks.pl update
423  
424 =cut
425  
426 # Install hooks via Git::Hooks
427 APPLYPATCH_MSG \&check_message_file;
428 COMMIT_MSG \&check_message_file;
429 PREPARE_COMMIT_MSG \&add_youtrack_summary;
430 UPDATE \&check_affected_refs;
431  
432 # =========================================================================== #
433  
434 1;
435  
436 __END__