App-Gitc

 view release on metacpan or  search on metacpan

lib/App/Gitc/Its/Jira.pm  view on Meta::CPAN

package App::Gitc::Its::Jira;
use strict;
use warnings;

# ABSTRACT: Support for Atlassian JIRA ITS (Issue Tracking System)
our $VERSION = '0.60'; # VERSION


use JIRA::Client;
use JIRA::Client::REST;
use Try::Tiny;
use List::MoreUtils qw( any );
use YAML;

use App::Gitc::Util qw(
    project_config
    command_name
    current_branch
);


sub label_service { return 'JIRA'; }
sub label_issue   { return 'Issue'; }

our $default_jira_user     = 'someuser';
our $default_jira_password = 'somepassword';
our $jira_conf;

sub lookup_jira_user {
    my $self = shift;
    if (not $jira_conf and -e "$ENV{HOME}/.gitc/jira.conf") {
        $jira_conf = YAML::LoadFile("$ENV{HOME}/.gitc/jira.conf");
    } else {
        $jira_conf = {};
    }
    my $jira_user     = $jira_conf->{user} || $default_jira_user;
    my $jira_password = $jira_conf->{password} || $default_jira_password;
    return ($jira_user, $jira_password);
}

sub lookup_status_strings
{
    my $self = shift;

    my $jira = $self->get_jira_object or return;

    my $statuses = $jira->getStatuses();
    return unless $statuses;

    return { map { $_->{id} => $_->{name} } @{$statuses} };
}

sub get_jira_object
{
    my $self = shift;

    # to find an JIRA issue, we need a number and URI
    my $uri = project_config()->{'jira_uri'} or return;

    # build and return the Issue object
    our %jira;
    my $jira = $jira{$uri};
    if ( not $jira ) {
        my ($jira_user, $jira_password) = $self->lookup_jira_user;
        $jira{$uri} = $jira
            = JIRA::Client->new( $uri, $jira_user, $jira_password );
        die "Unable to connect to JIRA SOAP API" unless $jira;
    }

    return $jira;
}

sub get_jira_rest_object {
    my $self = shift;

    # to find an JIRA issue, we need a number and URI
    my $uri    = project_config()->{'jira_uri'} or return;

    # build and return the Issue object
    our %jira_rest;
    my $jira = $jira_rest{$uri};
    if ( not $jira ) {
        my ($jira_user, $jira_password) = $self->lookup_jira_user;
        $jira_rest{$uri} = $jira = JIRA::Client::REST->new(
               username => $jira_user,
               password => $jira_password,
               url => $uri,
           );
        die "Unable to connect to JIRA REST API" unless $jira;
    }

    return $jira;
}

sub get_issue {
    my $self = shift;
    my $changeset = shift;
    my %params = @_;
    my $number = $self->issue_number($changeset) or return undef;
    our %jira_issue;
    return $jira_issue{$number}
        if exists $jira_issue{$number} and not $params{reload};

    my $issue = eval {
        my $jira = $self->get_jira_object or return;
        my $issue = $jira->getIssue($number);
        die "Issue $number didn't return an object" unless $issue;
        return $jira_issue{$number} = $issue;
    };

    warn "Error accessing JIRA: $@" if $@;
    return $issue; 
}

sub transition_state {
    my $self = shift;
    my ($args) = @_;
    $args ||= {};
    $args->{with_time} = 1 unless exists $args->{with_time};
    return "Skipping JIRA changes, as requested by GITC_NO_EVENTUM\n"
        if $ENV{GITC_NO_EVENTUM};
    return "Skipping JIRA changes as configured for this project\n"
        if not project_config()->{'jira_uri'};

    my $label = $self->label_issue;

    # validate the arguments
    my $command = $args->{command} || command_name();
    my $state = $self->_states( $command, $args->{target} );
    my $from = $state->{from};
    my $to = $state->{to};
    my $flag = $state->{flag};
    my $message = $args->{message} or die "No message";
    my $reviewer = $args->{reviewer};
    my $issue = exists $args->{issue} ? $args->{issue} :
                                        $self->get_issue(current_branch(), reload => 1);
    return "NOT CHANGING JIRA $label: changeset not in JIRA?\n"
        if not $issue;

    my ($jira_user) = $self->lookup_jira_user;
    $message = (getpwuid $>)[6]   # user's name
                . ": $message\n";

    $message = $flag." ".$message if $flag;

    my ( $rc, $status_exception );



( run in 0.519 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )