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