| 1 | package App::Gitc::Its::Github; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # ABSTRACT: Support for Github Issue Tracking | ||
| 6 | our $VERSION = '0.60'; # VERSION | ||
| 7 | |||
| 8 | |||
| 9 | use YAML::Syck; | ||
| 10 | use Pithub::Issues; | ||
| 11 | |||
| 12 | use App::Gitc::Util qw( | ||
| 13 | get_user_name | ||
| 14 | get_user_email | ||
| 15 | git | ||
| 16 | project_config | ||
| 17 | project_name | ||
| 18 | command_name | ||
| 19 | current_branch | ||
| 20 | ); | ||
| 21 | use List::MoreUtils qw(any); | ||
| 22 | |||
| 23 | sub label_service { return 'Github'; } | ||
| 24 | sub label_issue { return 'Issue'; } | ||
| 25 | |||
| 26 | sub get_issue { | ||
| 27 | my ($self, $changeset, %params) = @_; | ||
| 28 | |||
| 29 | my $number = $self->issue_number($changeset) or return undef; | ||
| 30 | |||
| 31 | our %github_issue; | ||
| 32 | return $github_issue{$number} | ||
| 33 | if exists $github_issue{$number} and not $params{reload}; | ||
| 34 | |||
| 35 | my $issue = eval { | ||
| 36 | my $github = $self->get_github_object or return; | ||
| 37 | my $issue = $self->_get_issue($number); | ||
| 38 | die "Issue $number didn't return an object" unless $issue; | ||
| 39 | return $github_issue{$number} = $issue; | ||
| 40 | }; | ||
| 41 | |||
| 42 | warn "Error accessing Github: $@" if $@; | ||
| 43 | return $issue; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub get_github_object { | ||
| 47 | my ($self) = @_; | ||
| 48 | |||
| 49 | our $github; | ||
| 50 | if (!$github) { | ||
| 51 | my %opts = $self->get_github_opts; | ||
| 52 | $github = Pithub::Issues->new(%opts); | ||
| 53 | } | ||
| 54 | |||
| 55 | return $github; | ||
| 56 | } | ||
| 57 | |||
| 58 | our $github_conf; | ||
| 59 | |||
| 60 | sub get_github_opts { | ||
| 61 | if (not $github_conf and -e "$ENV{HOME}/.gitc/github.conf") { | ||
| 62 | $github_conf = LoadFile("$ENV{HOME}/.gitc/github.conf"); | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | $github_conf ||= {}; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $project = project_name(); | ||
| 69 | |||
| 70 | my ($owner, $repo) = @{$github_conf->{$project}}{qw/Owner Repo/}; | ||
| 71 | unless ($owner and $repo) { # default to using the repo they cloned | ||
| 72 | my $url = git "config --get remote.origin.url"; | ||
| 73 | my ($o, $r) = $url =~ m|/([^/]+?)/([^/]+?)(?:\.git)?$|; | ||
| 74 | $owner ||= $o; | ||
| 75 | $repo ||= $r; | ||
| 76 | } | ||
| 77 | |||
| 78 | return ( | ||
| 79 | user => $owner, | ||
| 80 | repo => $repo, | ||
| 81 | prepare_request => sub { | ||
| 82 | return shift->authorization_basic(@{$github_conf}{qw(Username Password)}); | ||
| 83 | }, | ||
| 84 | ); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _get_issue { | ||
| 88 | my ($self, $number) = @_; | ||
| 89 | |||
| 90 | our $github; | ||
| 91 | $github ||= $self->get_github_object; | ||
| 92 | |||
| 93 | my $r = $github->get(issue_id => $number); | ||
| 94 | die "Issue $number didn't return an object" unless $r->response->code == 200; | ||
| 95 | return $r->content; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub issue_number { | ||
| 99 | my ($self, $changeset_or_number) = @_; | ||
| 100 | |||
| 101 | $changeset_or_number =~ s/.*?(\d+)[a-z]?$/$1/; | ||
| 102 | |||
| 103 | return $changeset_or_number; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub issue_summary { | ||
| 107 | my ($self, $issue) = @_; | ||
| 108 | return unless $issue; | ||
| 109 | |||
| 110 | return $issue->{title}; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub last_status { | ||
| 114 | my ($self, $branch) = @_; | ||
| 115 | return unless $branch; | ||
| 116 | |||
| 117 | my $meta_data = App::Gitc::Util::view_blob("meta/$branch") or die "No meta data found for $branch"; | ||
| 118 | return unless @$meta_data > 1; | ||
| 119 | |||
| 120 | my $to; | ||
| 121 | for (my $i = @$meta_data - 2; $i >= 0; $i--) { | ||
| 122 | my $command = $meta_data->[$i]{action}; | ||
| 123 | my $status; | ||
| 124 | if (my $target = $meta_data->[$i]{target}) { | ||
| 125 | $status = project_config()->{github_statuses}{$command}{$target} or next; | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | $status = project_config()->{github_statuses}{$command} or next; | ||
| 129 | } | ||
| 130 | $to = $status->{to} and last; | ||
| 131 | } | ||
| 132 | |||
| 133 | return $to; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub transition_state { | ||
| 137 | my ($self, $args) = @_; | ||
| 138 | $args ||= {}; | ||
| 139 | $args->{with_time} = 1 unless exists $args->{with_time}; | ||
| 140 | return "Skipping Github changes, as requested by GITC_NO_GITHUB\n" | ||
| 141 | if $ENV{GITC_NO_GITHUB}; | ||
| 142 | |||
| 143 | my $label = $self->label_issue; | ||
| 144 | # validate the arguments | ||
| 145 | my ($command, $message, $reviewer, $issue) = @{$args}{qw/command message reviewer issue/}; | ||
| 146 | die "No message" unless $message; | ||
| 147 | $issue = $self->get_issue(current_branch(), reload => 1) unless defined $issue; | ||
| 148 | return "NOT CHANGING Github $label: changeset not in Github?\n" | ||
| 149 | if not $issue; | ||
| 150 | my $state = $self->_states( $command, $args->{target} ); | ||
| 151 | my $to = $state->{to}; | ||
| 152 | my $from = $self->last_status($args->{changeset}); | ||
| 153 | |||
| 154 | $message = get_user_name() # user's name | ||
| 155 | . ": $message\n"; | ||
| 156 | |||
| 157 | my ( $rc ); | ||
| 158 | eval { | ||
| 159 | my $github = $self->get_github_object or return; | ||
| 160 | my $r = $github->comments->create(issue_id => $issue->{number}, data => {body => $message}); | ||
| 161 | die "Could not comment on issue $issue->{number}" unless $r->response->code == 201; | ||
| 162 | $r = $github->labels->remove(issue_id => $issue->{number}, label => $from) if $from; | ||
| 163 | $r = $github->labels->add(issue_id => $issue->{number}, data => [$to]); | ||
| 164 | die "Could not update issue $issue->{number}" unless $r->response->code == 200; | ||
| 165 | $rc = ($r->content->[0]{name} eq $to); | ||
| 166 | }; | ||
| 167 | die $@ if $@; | ||
| 168 | |||
| 169 | if ($rc) { | ||
| 170 | return "Changed Github $label to '$to'\n"; | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | return "NOT CHANGING GITHUB $label: currently '%s'\n", | ||
| 174 | $self->issue_state($issue); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | sub issue_state { | ||
| 179 | my ($self, $issue) = @_; | ||
| 180 | return unless $issue; | ||
| 181 | |||
| 182 | return $issue->{labels}[0]{name}; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub issue_changeset_uri { | ||
| 186 | my ($self, $issue) = @_; | ||
| 187 | |||
| 188 | return $issue->{html_url}; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub issue_project { | ||
| 192 | my ($self, $issue) = @_; | ||
| 193 | return unless $issue; | ||
| 194 | |||
| 195 | my ($project) = $issue->{url} =~ m|([^/]+)/issues/\d+$|; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub issue_promotion_notes { | ||
| 199 | return; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub issue_scheduled_release { | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; | ||
| 207 | |||
| 208 | __END__ |