App-Git-Workflow
view release on metacpan or search on metacpan
lib/App/Git/Workflow/Command/Jira.pm view on Meta::CPAN
package App::Git::Workflow::Command::Jira;
# Created on: 2014-03-11 21:06:01
# Create by: Ivan Wills
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$
use strict;
use warnings;
use version;
use English qw/ -no_match_vars /;
use App::Git::Workflow::Pom;
use App::Git::Workflow::Command qw/get_options/;
our $VERSION = version->new(1.1.20);
our $workflow = App::Git::Workflow::Pom->new;
our ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
our %option;
sub run {
if (!@ARGV) {
warn "No JIRA specified!\n";
@ARGV = qw/--help/;
}
get_options(
\%option,
'all|a',
'remote|r',
'list|l',
'quiet|q!',
'url|u=s',
'user|U=s',
'pass|password|P=s',
) or return;
my $jira_re = my $jira = shift @ARGV;
$jira_re =~ s/[-_]/[-_]/;
$jira_re = lc $jira_re;
# check local branches first
my $type = $option{all} ? 'both' : $option{remote} ? 'remote' : 'local';
my $prefix = $option{all} || $option{remote} ? '(?:\w+/)?' : '';
my @branch = grep {/^$prefix(?:[a-z]+\/)?(\w+_)?$jira_re(?:\D|$)/i} $workflow->branches($type);
if (@branch) {
my $branch = which_branch(@branch);
return if !defined $branch;
$workflow->git->checkout($branch);
}
else {
# check if there is a remote branch
my (@remote_branch) = grep {/^origin\/(\w+_)?$jira_re/} $workflow->branches('remote');
if (@remote_branch) {
my $remote_branch = which_branch(@remote_branch);
return if !defined $remote_branch;
my $branch = $remote_branch;
$branch =~ s{^origin/}{};
$workflow->git->checkout('-b', $branch, '--track', $remote_branch);
print "Switched to branch '$branch'\n" if !$option{quiet};
}
elsif (!$option{quiet}) {
if ( $option{url} && eval { require JIRA::REST } ) {
$jira =~ s/_/-/;
$jira = uc $jira;
my $jira_rest = JIRA::REST->new($option{url}, $option{user}, $option{pass});
my $issue = eval { $jira_rest->GET("/issue/$jira") };
my $branch = lc "$jira $issue->{fields}{summary}";
$branch =~ s/[ !?-]+/_/gxms;
warn "No branch found for $jira!\n";
warn "Create with one of the following:\n";
warn "git feature $branch\n";
}
else {
# suggest how to construct the branch
warn "No branch for jira $jira exists!\n";
warn "Create with one of the following:\n";
warn "git feature $jira\n";
}
}
}
return;
}
sub which_branch {
my @branches = map {/(.*)$/} @_;
if ($option{list}) {
print +( join "\n", map {label($_)} @branches ), "\n";
return;
}
return $branches[0] if @branches == 1;
( run in 0.530 second using v1.01-cache-2.11-cpan-39bf76dae61 )