File Coverage

blib/lib/App/Git/Workflow/Command/Jira.pm
Criterion Covered Total %
statement 74 85 87.0
branch 31 32 96.8
condition 4 6 66.6
subroutine 9 9 100.0
pod 3 3 100.0
total 121 135 89.6


line stmt bran cond sub pod time code
1             package App::Git::Workflow::Command::Jira;
2              
3             # Created on: 2014-03-11 21:06:01
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   85279 use strict;
  2         13  
  2         55  
10 2     2   11 use warnings;
  2         4  
  2         55  
11 2     2   378 use version;
  2         1624  
  2         10  
12 2     2   527 use English qw/ -no_match_vars /;
  2         3004  
  2         12  
13 2     2   1086 use App::Git::Workflow::Pom;
  2         6  
  2         99  
14 2     2   473 use App::Git::Workflow::Command qw/get_options/;
  2         5  
  2         1919  
15              
16             our $VERSION = version->new(1.1.16);
17             our $workflow = App::Git::Workflow::Pom->new;
18             our ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
19             our %option;
20              
21             sub run {
22 15 100   15 1 49 if (!@ARGV) {
23 1         55 warn "No JIRA specified!\n";
24 1         6 @ARGV = qw/--help/;
25             }
26             get_options(
27 15 100       73 \%option,
28             'all|a',
29             'remote|r',
30             'list|l',
31             'quiet|q!',
32             'url|u=s',
33             'user|U=s',
34             'pass|password|P=s',
35             ) or return;
36              
37 14         38 my $jira_re = my $jira = shift @ARGV;
38 14         63 $jira_re =~ s/[-_]/[-_]/;
39 14         40 $jira_re = lc $jira_re;
40              
41             # check local branches first
42 14 100       50 my $type = $option{all} ? 'both' : $option{remote} ? 'remote' : 'local';
    100          
43 14 100 100     74 my $prefix = $option{all} || $option{remote} ? '(?:\w+/)?' : '';
44 14         96 my @branch = grep {/^$prefix(?:[a-z]+\/)?(\w+_)?$jira_re(?:\D|$)/i} $workflow->branches($type);
  26         592  
45              
46 14 100       40 if (@branch) {
47 9         23 my $branch = which_branch(@branch);
48 9 100       37 return if !defined $branch;
49 4         15 $workflow->git->checkout($branch);
50             }
51             else {
52             # check if there is a remote branch
53 5         14 my (@remote_branch) = grep {/^origin\/(\w+_)?$jira_re/} $workflow->branches('remote');
  6         46  
54 5 100       19 if (@remote_branch) {
    100          
55 3         10 my $remote_branch = which_branch(@remote_branch);
56 3 100       11 return if !defined $remote_branch;
57 2         4 my $branch = $remote_branch;
58 2         9 $branch =~ s{^origin/}{};
59 2         6 $workflow->git->checkout('-b', $branch, '--track', $remote_branch);
60 2 100       42 print "Switched to branch '$branch'\n" if !$option{quiet};
61             }
62             elsif (!$option{quiet}) {
63 1 50 33     6 if ( $option{url} && eval { require JIRA::REST } ) {
  0         0  
64 0         0 $jira =~ s/_/-/;
65 0         0 $jira = uc $jira;
66 0         0 my $jira_rest = JIRA::REST->new($option{url}, $option{user}, $option{pass});
67 0         0 my $issue = eval { $jira_rest->GET("/issue/$jira") };
  0         0  
68 0         0 my $branch = lc "$jira $issue->{fields}{summary}";
69 0         0 $branch =~ s/[ !?-]+/_/gxms;
70              
71 0         0 warn "No branch found for $jira!\n";
72 0         0 warn "Create with one of the following:\n";
73 0         0 warn "git feature $branch\n";
74             }
75             else {
76             # suggest how to construct the branch
77 1         47 warn "No branch for jira $jira exists!\n";
78 1         16 warn "Create with one of the following:\n";
79 1         13 warn "git feature $jira\n";
80             }
81             }
82             }
83              
84 8         29 return;
85             }
86              
87             sub which_branch {
88 12     12 1 28 my @branches = map {/(.*)$/} @_;
  16         55  
89              
90 12 100       38 if ($option{list}) {
91 5         11 print +( join "\n", map {label($_)} @branches ), "\n";
  6         20  
92 5         34 return;
93             }
94 7 100       20 return $branches[0] if @branches == 1;
95              
96 3         5 my $count = 0;
97 3         5 print {*STDERR} "Which branch:\n\t";
  3         100  
98 3         57 print {*STDERR} join "", map { ++$count . ". $_\n\t" } map {label($_)} @branches;
  3         11  
  6         60  
  6         14  
99 3         13 print {*STDERR} "\n[1..$count] : ";
  3         58  
100 3         17 my $ans = <STDIN>;
101 3         5 chomp $ans;
102 3         8 $ans--;
103 3 100       10 if (!$branches[$ans]) {
104 1         11 warn "\nUnknown branch!\n";
105 1         6 return;
106             }
107              
108 2         7 return $branches[$ans];
109             }
110              
111             sub label {
112 12     12 1 27 my ($branch) = @_;
113 12 100       68 return $branch if $option{quiet};
114              
115 11         43 my $details = $workflow->commit_details($branch, user => 1);
116              
117 11         596 return "$branch ($details->{user} at " . localtime($details->{time}) . ')';
118             }
119              
120             1;
121              
122             __DATA__
123              
124             =head1 NAME
125              
126             git-jira - Checkout any branch mentioning the passed Jira
127              
128             =head1 VERSION
129              
130             This documentation refers to git-jira version 1.1.16
131              
132             =head1 SYNOPSIS
133              
134             git-jira [option] JIRAID
135              
136             OPTIONS:
137             JIRAID A Jira format id
138             -u --url[=]URL
139             Use URL as the JIRA instance for looking up summaries.
140             -l --list Just list found branch(es) don't checkout
141             -q --quiet Don't inform how to create missing branch
142              
143             -v --verbose Show more detailed option
144             --version Prints the version information
145             --help Prints this help information
146             --man Prints the full documentation for git-Jira
147              
148             =head1 DESCRIPTION
149              
150             Finds any branch containing the passed Jira issue id and switches to that
151             branch. If none is found then it suggests creating the branch using
152             L<git-feature>. If L<JIRA::REST> is installed the suggestion will use the
153             JIRA summary as part of the name.
154              
155             =head1 SUBROUTINES/METHODS
156              
157             =head2 C<run ()>
158              
159             Executes the git workflow command
160              
161             =head2 C<which_branch (@branches)>
162              
163             Ask the user which branch to switch to
164              
165             =head2 C<label ($branch)>
166              
167             Adds user and time to a branch unless --quiet used
168              
169             =head1 DIAGNOSTICS
170              
171             =head1 CONFIGURATION AND ENVIRONMENT
172              
173             Defaults for this script can be set through C<git config>
174              
175             jira.url Specifies the URL for the JIRA instance being used.
176              
177             You can set these values either by editing the repository local C<.git/config>
178             file or C<~/.gitconfig> or use the C<git config> command
179              
180             # eg Setting the local value (ie only the current repository)
181             git config jira.url https://jira.example.com/
182              
183             # eg Setting the global value
184             git config --global jira.url https://jira.example.com/
185              
186             =head1 DEPENDENCIES
187              
188             =head1 INCOMPATIBILITIES
189              
190             =head1 BUGS AND LIMITATIONS
191              
192             There are no known bugs in this module.
193              
194             Please report problems to Ivan Wills (ivan.wills@gmail.com).
195              
196             Patches are welcome.
197              
198             =head1 AUTHOR
199              
200             Ivan Wills - (ivan.wills@gmail.com)
201              
202             =head1 LICENSE AND COPYRIGHT
203              
204             Copyright (c) 2014 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
205             All rights reserved.
206              
207             This module is free software; you can redistribute it and/or modify it under
208             the same terms as Perl itself. See L<perlartistic>. This program is
209             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
210             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
211             PARTICULAR PURPOSE.
212              
213             =cut