| 1 | package App::Gitc::Its::Jira; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # ABSTRACT: Support for Atlassian JIRA ITS (Issue Tracking System) | ||
| 6 | our $VERSION = '0.60'; # VERSION | ||
| 7 | |||
| 8 | |||
| 9 | use JIRA::Client; | ||
| 10 | use JIRA::Client::REST; | ||
| 11 | use Try::Tiny; | ||
| 12 | use List::MoreUtils qw( any ); | ||
| 13 | use YAML; | ||
| 14 | |||
| 15 | use App::Gitc::Util qw( | ||
| 16 | project_config | ||
| 17 | command_name | ||
| 18 | current_branch | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | sub label_service { return 'JIRA'; } | ||
| 23 | sub label_issue { return 'Issue'; } | ||
| 24 | |||
| 25 | our $default_jira_user = 'someuser'; | ||
| 26 | our $default_jira_password = 'somepassword'; | ||
| 27 | our $jira_conf; | ||
| 28 | |||
| 29 | sub lookup_jira_user { | ||
| 30 | my $self = shift; | ||
| 31 | if (not $jira_conf and -e "$ENV{HOME}/.gitc/jira.conf") { | ||
| 32 | $jira_conf = YAML::LoadFile("$ENV{HOME}/.gitc/jira.conf"); | ||
| 33 | } else { | ||
| 34 | $jira_conf = {}; | ||
| 35 | } | ||
| 36 | my $jira_user = $jira_conf->{user} || $default_jira_user; | ||
| 37 | my $jira_password = $jira_conf->{password} || $default_jira_password; | ||
| 38 | return ($jira_user, $jira_password); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub lookup_status_strings | ||
| 42 | { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | my $jira = $self->get_jira_object or return; | ||
| 46 | |||
| 47 | my $statuses = $jira->getStatuses(); | ||
| 48 | return unless $statuses; | ||
| 49 | |||
| 50 | return { map { $_->{id} => $_->{name} } @{$statuses} }; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub get_jira_object | ||
| 54 | { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | # to find an JIRA issue, we need a number and URI | ||
| 58 | my $uri = project_config()->{'jira_uri'} or return; | ||
| 59 | |||
| 60 | # build and return the Issue object | ||
| 61 | our %jira; | ||
| 62 | my $jira = $jira{$uri}; | ||
| 63 | if ( not $jira ) { | ||
| 64 | my ($jira_user, $jira_password) = $self->lookup_jira_user; | ||
| 65 | $jira{$uri} = $jira | ||
| 66 | = JIRA::Client->new( $uri, $jira_user, $jira_password ); | ||
| 67 | die "Unable to connect to JIRA SOAP API" unless $jira; | ||
| 68 | } | ||
| 69 | |||
| 70 | return $jira; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub get_jira_rest_object { | ||
| 74 | my $self = shift; | ||
| 75 | |||
| 76 | # to find an JIRA issue, we need a number and URI | ||
| 77 | my $uri = project_config()->{'jira_uri'} or return; | ||
| 78 | |||
| 79 | # build and return the Issue object | ||
| 80 | our %jira_rest; | ||
| 81 | my $jira = $jira_rest{$uri}; | ||
| 82 | if ( not $jira ) { | ||
| 83 | my ($jira_user, $jira_password) = $self->lookup_jira_user; | ||
| 84 | $jira_rest{$uri} = $jira = JIRA::Client::REST->new( | ||
| 85 | username => $jira_user, | ||
| 86 | password => $jira_password, | ||
| 87 | url => $uri, | ||
| 88 | ); | ||
| 89 | die "Unable to connect to JIRA REST API" unless $jira; | ||
| 90 | } | ||
| 91 | |||
| 92 | return $jira; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub get_issue { | ||
| 96 | my $self = shift; | ||
| 97 | my $changeset = shift; | ||
| 98 | my %params = @_; | ||
| 99 | my $number = $self->issue_number($changeset) or return undef; | ||
| 100 | our %jira_issue; | ||
| 101 | return $jira_issue{$number} | ||
| 102 | if exists $jira_issue{$number} and not $params{reload}; | ||
| 103 | |||
| 104 | my $issue = eval { | ||
| 105 | my $jira = $self->get_jira_object or return; | ||
| 106 | my $issue = $jira->getIssue($number); | ||
| 107 | die "Issue $number didn't return an object" unless $issue; | ||
| 108 | return $jira_issue{$number} = $issue; | ||
| 109 | }; | ||
| 110 | |||
| 111 | warn "Error accessing JIRA: $@" if $@; | ||
| 112 | return $issue; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub transition_state { | ||
| 116 | my $self = shift; | ||
| 117 | my ($args) = @_; | ||
| 118 | $args ||= {}; | ||
| 119 | $args->{with_time} = 1 unless exists $args->{with_time}; | ||
| 120 | return "Skipping JIRA changes, as requested by GITC_NO_EVENTUM\n" | ||
| 121 | if $ENV{GITC_NO_EVENTUM}; | ||
| 122 | return "Skipping JIRA changes as configured for this project\n" | ||
| 123 | if not project_config()->{'jira_uri'}; | ||
| 124 | |||
| 125 | my $label = $self->label_issue; | ||
| 126 | |||
| 127 | # validate the arguments | ||
| 128 | my $command = $args->{command} || command_name(); | ||
| 129 | my $state = $self->_states( $command, $args->{target} ); | ||
| 130 | my $from = $state->{from}; | ||
| 131 | my $to = $state->{to}; | ||
| 132 | my $flag = $state->{flag}; | ||
| 133 | my $message = $args->{message} or die "No message"; | ||
| 134 | my $reviewer = $args->{reviewer}; | ||
| 135 | my $issue = exists $args->{issue} ? $args->{issue} : | ||
| 136 | $self->get_issue(current_branch(), reload => 1); | ||
| 137 | return "NOT CHANGING JIRA $label: changeset not in JIRA?\n" | ||
| 138 | if not $issue; | ||
| 139 | |||
| 140 | my ($jira_user) = $self->lookup_jira_user; | ||
| 141 | $message = (getpwuid $>)[6] # user's name | ||
| 142 | . ": $message\n"; | ||
| 143 | |||
| 144 | $message = $flag." ".$message if $flag; | ||
| 145 | |||
| 146 | my ( $rc, $status_exception ); | ||
| 147 | eval { | ||
| 148 | my $jira = $self->get_jira_object or return; | ||
| 149 | $jira->addComment( $issue, $message ); | ||
| 150 | my $updated_issue = $jira->progress_workflow_action_safely( $issue, $to ); | ||
| 151 | my $jira_rest = $self->get_jira_rest_object or return; | ||
| 152 | $jira_rest->unwatch_issue( $issue->{id}, $jira_user ) | ||
| 153 | if $jira_user eq $default_jira_user; | ||
| 154 | $rc = ( $issue->{id} == $updated_issue->{id} ); | ||
| 155 | }; | ||
| 156 | die $@ if $@; # rethrow unexpected exceptions | ||
| 157 | |||
| 158 | if ($reviewer) { | ||
| 159 | eval { | ||
| 160 | my $jira = $self->get_jira_object or return; | ||
| 161 | $jira->update_issue( $issue, | ||
| 162 | { custom_fields => { customfield_10401 => $reviewer, } } ); | ||
| 163 | }; | ||
| 164 | warn "Unable to set reviewer: $@" if $@; | ||
| 165 | } | ||
| 166 | |||
| 167 | if ( $status_exception ) { | ||
| 168 | die $status_exception | ||
| 169 | if $status_exception !~ m/('[^']+' is not a valid status name)/; | ||
| 170 | return "NOT CHANGING JIRA $label: $1\n"; | ||
| 171 | } | ||
| 172 | elsif ($rc) { # success | ||
| 173 | return "Changed JIRA $label to '$to'\n"; | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | return sprintf "NOT CHANGING JIRA $label: currently '%s'\n", | ||
| 177 | $self->issue_state($issue); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub issue_state { | ||
| 183 | my $self = shift; | ||
| 184 | my $issue = shift; | ||
| 185 | |||
| 186 | return unless $issue; | ||
| 187 | my $status_lookup = $self->lookup_status_strings; | ||
| 188 | |||
| 189 | return $status_lookup->{$issue->{status}} if $status_lookup && exists $status_lookup->{$issue->{status}}; | ||
| 190 | return $issue->{status}; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub issue_number { | ||
| 194 | my $self = shift; | ||
| 195 | my ($changeset_or_issue) = @_; | ||
| 196 | |||
| 197 | return $changeset_or_issue->{key} if ref $changeset_or_issue; | ||
| 198 | |||
| 199 | $changeset_or_issue =~ s/[a-z]+$//; | ||
| 200 | |||
| 201 | return $changeset_or_issue; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub issue_id { | ||
| 205 | my $self = shift; | ||
| 206 | my $issue = shift; | ||
| 207 | |||
| 208 | return $issue->{key}; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub issue_changeset_uri { | ||
| 212 | my $self = shift; | ||
| 213 | my $issue = shift; | ||
| 214 | return unless $issue; | ||
| 215 | |||
| 216 | # to find an JIRA issue, we need a number and URI | ||
| 217 | my $its_uri = project_config()->{'jira_uri'}; | ||
| 218 | return unless $its_uri; | ||
| 219 | |||
| 220 | my $uri = eval { sprintf( '%s/browse/%s', $its_uri, $issue->key ); } || ''; | ||
| 221 | return $uri; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub issue_summary { | ||
| 225 | my $self = shift; | ||
| 226 | my $issue = shift; | ||
| 227 | return unless $issue; | ||
| 228 | |||
| 229 | return $issue->{summary}; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub issue_promotion_notes { | ||
| 233 | my $self = shift; | ||
| 234 | my $issue = shift; | ||
| 235 | return unless $issue; | ||
| 236 | |||
| 237 | return unless $issue->{customFieldValues}; | ||
| 238 | my ($val_obj) | ||
| 239 | = grep { $_->{'customfieldId'} eq 'customfield_10302' } # ID for 'Promotion Related Notes' in TE Project | ||
| 240 | @{ $issue->{customFieldValues} }; | ||
| 241 | return unless $val_obj; | ||
| 242 | return join( "\n", @{ $val_obj->{values} } ); | ||
| 243 | } | ||
| 244 | |||
| 245 | sub issue_project { | ||
| 246 | my $self = shift; | ||
| 247 | my $issue = shift; | ||
| 248 | return unless $issue; | ||
| 249 | |||
| 250 | return $issue->{project}; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub issue_scheduled_release { | ||
| 254 | my $self = shift; | ||
| 255 | my $issue = shift; | ||
| 256 | return unless $issue; | ||
| 257 | |||
| 258 | return unless $issue->{fixVersions} && @{$issue->{fixVersions}}; | ||
| 259 | |||
| 260 | return $issue->{fixVersions}->[-1]->{name}; #.' ('.$issue->{fixVersions}->[-1]->{releaseDate}.')'; | ||
| 261 | } | ||
| 262 | |||
| 263 | 1; | ||
| 264 | |||
| 265 | __END__ |