| 1 | package App::Gitc::Its::Eventum; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # ABSTRACT: Support for Eventum ITS (Issue Tracking System) | ||
| 6 | our $VERSION = '0.60'; # VERSION | ||
| 7 | |||
| 8 | |||
| 9 | use POSIX qw( strftime ); | ||
| 10 | |||
| 11 | use App::Gitc::Util qw( | ||
| 12 | project_config | ||
| 13 | command_name | ||
| 14 | current_branch | ||
| 15 | ); | ||
| 16 | |||
| 17 | use List::MoreUtils qw( any ); | ||
| 18 | |||
| 19 | sub label_service { return 'Eventum'; } | ||
| 20 | sub label_issue { return 'Eventum'; } | ||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | sub get_issue { | ||
| 25 | my $self = shift; | ||
| 26 | my $changeset = shift; | ||
| 27 | my %params = @_; | ||
| 28 | my $number = $self->issue_number($changeset) or return undef; | ||
| 29 | our %eventum_issue; | ||
| 30 | return $eventum_issue{$number} | ||
| 31 | if exists $eventum_issue{$number} and not $params{reload}; | ||
| 32 | |||
| 33 | # to find an Eventum issue, we need a number and URI | ||
| 34 | my $uri = project_config()->{'eventum_uri'} or return; | ||
| 35 | |||
| 36 | # build and return the Issue object | ||
| 37 | our %eventum; | ||
| 38 | my $eventum = $eventum{$uri}; | ||
| 39 | if ( not $eventum ) { | ||
| 40 | require GSG::Eventum; | ||
| 41 | # TODO GSG::Eventum has not been publicly released | ||
| 42 | $eventum{$uri} = $eventum = GSG::Eventum->new({ | ||
| 43 | uri => $uri, | ||
| 44 | }); | ||
| 45 | } | ||
| 46 | |||
| 47 | # GSG::Eventum::Issue is lazy, force an action to verify the issue | ||
| 48 | my $issue = $eventum->issue($number); | ||
| 49 | $issue = undef if not eval { $issue->summary }; | ||
| 50 | return $eventum_issue{$number} = $issue; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | sub transition_state { | ||
| 55 | my $self = shift; | ||
| 56 | my ($args) = @_; | ||
| 57 | $args ||= {}; | ||
| 58 | $args->{with_time} = 1 unless exists $args->{with_time}; | ||
| 59 | return "Skipping Eventum changes, as requested by GITC_NO_EVENTUM\n" | ||
| 60 | if $ENV{GITC_NO_EVENTUM}; | ||
| 61 | return "Skipping Eventum changes as configured for this project\n" | ||
| 62 | if not project_config()->{'eventum_uri'}; | ||
| 63 | |||
| 64 | # validate the arguments | ||
| 65 | my $command = $args->{command} || command_name(); | ||
| 66 | my ($from, $to) = $self->_states( $command, $args->{target} ); | ||
| 67 | my $message = $args->{message} or die "No eventum message"; | ||
| 68 | my $issue = exists $args->{issue} ? $args->{issue} : | ||
| 69 | $self->get_issue(current_branch(), reload => 1); | ||
| 70 | return "NOT CHANGING Eventum status: changeset not in Eventum?\n" | ||
| 71 | if not $issue; | ||
| 72 | |||
| 73 | # update the Eventum issue | ||
| 74 | my $time_format = '%m/%d/%Y'; | ||
| 75 | $time_format .= ' %I:%M %p' if $args->{with_time}; | ||
| 76 | $message = (getpwuid $>)[6] # user's name | ||
| 77 | . strftime( " $time_format: $message\n", localtime ); | ||
| 78 | my ( $rc, $status_exception ); | ||
| 79 | eval { | ||
| 80 | return $rc = $issue->close($message) if $to and $to eq 'CLOSE'; | ||
| 81 | $issue->postpone_updates; | ||
| 82 | $issue->append_internal_comments($message); | ||
| 83 | $rc = eval { $issue->transition_status($from, $to) } if $to; | ||
| 84 | $status_exception = $@ if $@; | ||
| 85 | $issue->update; | ||
| 86 | $issue->live_updates; | ||
| 87 | }; | ||
| 88 | die $@ if $@; # rethrow unexpected exceptions | ||
| 89 | if ( $status_exception ) { | ||
| 90 | die $status_exception | ||
| 91 | if $status_exception !~ m/('[^']+' is not a valid status name)/; | ||
| 92 | return "NOT CHANGING Eventum status: $1\n"; | ||
| 93 | } | ||
| 94 | elsif ($rc) { # success | ||
| 95 | return "Changed Eventum status to '$to'\n"; | ||
| 96 | } | ||
| 97 | else { | ||
| 98 | return sprintf "NOT CHANGING Eventum status: currently '%s'\n", | ||
| 99 | $issue->status; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | sub issue_state { | ||
| 105 | my $self = shift; | ||
| 106 | my $issue = shift; | ||
| 107 | |||
| 108 | return unless $issue; | ||
| 109 | |||
| 110 | return $issue->status; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub issue_id { | ||
| 114 | my $self = shift; | ||
| 115 | my $issue = shift; | ||
| 116 | |||
| 117 | return $issue->number; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub issue_changeset_uri { | ||
| 121 | my $self = shift; | ||
| 122 | my $issue = shift; | ||
| 123 | return unless $issue; | ||
| 124 | |||
| 125 | my $uri = eval { | ||
| 126 | sprintf('%s/view.php?id=%d', $issue->eventum->uri, $issue->number); | ||
| 127 | } || ''; | ||
| 128 | return $uri; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub issue_summary { | ||
| 132 | my $self = shift; | ||
| 133 | my $issue = shift; | ||
| 134 | return unless $issue; | ||
| 135 | |||
| 136 | return $issue->summary; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub issue_promotion_notes { | ||
| 140 | my $self = shift; | ||
| 141 | my $issue = shift; | ||
| 142 | return unless $issue; | ||
| 143 | |||
| 144 | return $issue->promotion_notes; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub issue_project { | ||
| 148 | my $self = shift; | ||
| 149 | my $issue = shift; | ||
| 150 | return unless $issue; | ||
| 151 | |||
| 152 | return $issue->project; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub issue_scheduled_release { | ||
| 156 | my $self = shift; | ||
| 157 | my $issue = shift; | ||
| 158 | return unless $issue; | ||
| 159 | |||
| 160 | return $issue->scheduled_release; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | sub issue_number { | ||
| 165 | my $self = shift; | ||
| 166 | my ($changeset_or_issue) = @_; | ||
| 167 | |||
| 168 | return $changeset_or_issue->number if ref $changeset_or_issue; | ||
| 169 | |||
| 170 | return $1 if $changeset_or_issue =~ m/\A e (\d+) \w? \z/xms; | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; | ||
| 175 | |||
| 176 | __END__ |