Skip to content

Commit 1d9e44e

Browse files
committed
add milestone display
1 parent bafd6a6 commit 1d9e44e

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

lib/Bot/BasicBot/Pluggable/Module/GitHub.pm

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use base 'Bot::BasicBot::Pluggable::Module';
1010

1111
use strict;
1212
use Net::GitHub::V3;
13+
use Mojo::UserAgent;
1314

1415
our $VERSION = '0.04';
1516

@@ -62,6 +63,60 @@ sub ng {
6263
return $net_github{"$user/$project"} = $api;
6364
}
6465

66+
my $ua;
67+
68+
sub _make_ua {
69+
return $ua if $ua;
70+
$ua = Mojo::UserAgent->new;
71+
$ua->proxy->detect;
72+
return $ua;
73+
}
74+
75+
my %_commit_branch_cache;
76+
my $_commit_branch_cache_timeout = 60 * 60;
77+
78+
sub _commit_branch {
79+
my ($self, $pr, $commit_id) = @_;
80+
_make_ua();
81+
my $base_url = $pr->{html_url};
82+
$base_url =~ s{/(pull|commit)/.*}{};
83+
my $url = "$base_url/branch_commits/$commit_id";
84+
my $tag;
85+
my $time = time;
86+
if ($_commit_branch_cache{$url} && ($_commit_branch_cache{$url}{_time} + $_commit_branch_cache_timeout) > $time) {
87+
$tag = $_commit_branch_cache{$url}{tag};
88+
} else {
89+
warn "getting: $url";
90+
my $res = $ua->get("$url")->res;
91+
return "" unless $res;
92+
my @tags = ((grep !m{[-/]}, $res->dom("ul.branches-list li.branch a")->map("text")->each),
93+
(grep !/-/, $res->dom("ul.branches-tag-list li a")->map("text")->each));
94+
#my @tags = grep !/-/, $res->dom("ul li a")->map("text")->each;
95+
if (@tags) {
96+
$tag = $tags[-1];
97+
$_commit_branch_cache{$url} = { _time => $time, tag => $tag };
98+
}
99+
}
100+
return "T:\cB$tag\cB " if length $tag;
101+
return "";
102+
}
103+
104+
sub _pr_branch {
105+
my ($self, $ng, $pr) = @_;
106+
my $issue_number = $pr->{number};
107+
my $ie = $ng->issue;
108+
my $commit_id;
109+
# while (my $event = $ie->next_event($issue_number))
110+
for my $event ($ie->events($issue_number)) {
111+
if ($event->{event} eq 'merged') {
112+
$commit_id = $event->{commit_id};
113+
last;
114+
}
115+
}
116+
return '' unless length $commit_id;
117+
return $self->_commit_branch($pr, $commit_id);
118+
}
119+
65120

66121
# Find the name of the GitHub project for a given channel
67122
sub project_for_channel {

lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,14 @@ sub said {
147147
push @return, $issue->{error};
148148
next match;
149149
}
150-
push @return, sprintf "%s \cC43%d\cC (\cC59%s\cC) by \cB%s\cB - \cC73%s\cC%s \{%s\cC\}",
150+
push @return, sprintf "%s \cC43%d\cC (\cC59%s\cC) by \cB%s\cB - \cC73%s\cC%s %s\{%s\cC\}",
151151
(exists $issue->{pull_request} ? "\cC29Pull request" : "\cC52Issue"),
152152
$thingnum,
153153
$issue->{title},
154154
_dehih($issue->{user}{login}),
155155
makeashorterlink($issue->{html_url}),
156156
($issue->{labels}&&@{$issue->{labels}}?" [".(join",",map{$_->{name}}@{$issue->{labels}})."]":""),
157+
($issue->{milestone} ? "MS:\cB$issue->{milestone}{title}\cB ": ($pr?$self->_pr_branch($ng, $pr):"")),
157158
$pr&&$pr->{merged_at}?"\cC46merged on ".($pr->{merged_at}=~s/T.*//r):
158159
$issue->{closed_at}?"\cC55closed on ".($issue->{closed_at}=~s/T.*//r):"\cC52".$issue->{state}." since ".($issue->{created_at}=~s/T.*//r);
159160
}
@@ -182,6 +183,7 @@ $issue->{closed_at}?"\cC55closed on ".($issue->{closed_at}=~s/T.*//r):"\cC52".$i
182183
if ($commit->{html_url}) {
183184
$commit->{commit}{html_url} = $commit->{html_url};
184185
}
186+
$commit->{commit}{sha} //= $commit->{sha};
185187
$commit = $commit->{commit};
186188
}
187189
if ($commit && !exists $commit->{error}) {
@@ -192,11 +194,13 @@ $issue->{closed_at}?"\cC55closed on ".($issue->{closed_at}=~s/T.*//r):"\cC52".$i
192194
# might perhaps change in future, so play it safe:
193195
# $url = "https://github.com$url" unless $url =~ /^http/;
194196
# $url =~ s{https://api.github.com/repos/(.*?)/commits/}{https://github.com/$1/commit/};
195-
push @return, sprintf "Commit \cC43$thingnum\cC (\cC59%s\cC) by \cB%s\cB on %s - \cC73%s",
197+
push @return, sprintf "Commit \cC43$thingnum\cC (\cC59%s\cC) by \cB%s\cB on %s - \cC73%s\cC %s",
196198
$title,
197199
_dehih($commit->{author}{login}||$commit->{committer}{login}||$commit->{author}{name}||$commit->{committer}{name}),
198200
($commit->{author}{date}=~s/T.*//r),
199-
makeashorterlink($url);
201+
makeashorterlink($url),
202+
$self->_commit_branch($commit, $commit->{sha}),
203+
;
200204
} else {
201205
# We purposefully don't show a message on IRC here, as we guess
202206
# what might be a SHA, so we could be annoying saying that we
@@ -364,13 +368,14 @@ $issue->{closed_at}?"\cC55closed on ".($issue->{closed_at}=~s/T.*//r):"\cC52".$i
364368
push @return, $issue->{error};
365369
next match;
366370
}
367-
push @return, sprintf "%s \cC43%d\cC (\cC59%s\cC) by \cB%s\cB - \cC73%s\cC%s \{%s\cC\}",
371+
push @return, sprintf "%s \cC43%d\cC (\cC59%s\cC) by \cB%s\cB - \cC73%s\cC%s %s\{%s\cC\}",
368372
(exists $issue->{pull_request} ? "\cC29Pull request" : "\cC52Issue"),
369373
$thingnum,
370374
$issue->{title},
371375
_dehih($issue->{user}{login}),
372376
$project,
373377
($issue->{labels}&&@{$issue->{labels}}?" [".(join",",map{$_->{name}}@{$issue->{labels}})."]":""),
378+
($issue->{milestone} ? "MS:\cB$issue->{milestone}{title}\cB ": ($pr?$self->_pr_branch($ng, $pr):"")),
374379
$pr&&$pr->{merged_at}?"\cC46merged on ".($pr->{merged_at}=~s/T.*//r):
375380
$issue->{closed_at}?"\cC55closed on ".($issue->{closed_at}=~s/T.*//r):"\cC52".$issue->{state}." since ".($issue->{created_at}=~s/T.*//r);
376381
}
@@ -383,11 +388,13 @@ $issue->{closed_at}?"\cC55closed on ".($issue->{closed_at}=~s/T.*//r):"\cC52".$i
383388
my $title = ( split /\n+/, $commit->{message} )[0];
384389
my $url = $commit->{html_url};
385390

386-
push @return, sprintf "Commit \cC43$thingnum\cC (\cC59%s\cC) by \cB%s\cB on %s - \cC73%s",
391+
push @return, sprintf "Commit \cC43$thingnum\cC (\cC59%s\cC) by \cB%s\cB on %s - \cC73%s\cC %s",
387392
$title,
388393
_dehih($commit->{author}{login}||$commit->{committer}{login}||$commit->{author}{name}||$commit->{committer}{name}),
389394
($commit->{author}{date}=~s/T.*//r),
390-
$project;
395+
$project,
396+
$self->_commit_branch($commit, $commit->{sha}),
397+
;
391398
} else {
392399
# We purposefully don't show a message on IRC here, as we guess
393400
# what might be a SHA, so we could be annoying saying that we

lib/Bot/BasicBot/Pluggable/Module/GitHub/IssueSearch.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ sub said {
8888
push @ret, $issue->{error};
8989
next;
9090
}
91-
push @ret, sprintf "%s \cC43%d\cC (\cC59%s\cC) by \cB%s\cB - \cC73%s\cC%s \{%s\cC\}",
91+
push @ret, sprintf "%s \cC43%d\cC (\cC59%s\cC) by \cB%s\cB - \cC73%s\cC%s %s\{%s\cC\}",
9292
(exists $issue->{pull_request} ? "\cC29Pull request" : "\cC52Issue"),
9393
$issue->{number},
9494
$issue->{title},
9595
_dehih($issue->{user}{login}),
9696
makeashorterlink($issue->{html_url}),
9797
($issue->{labels}&&@{$issue->{labels}}?" [".(join",",map{$_->{name}}@{$issue->{labels}})."]":""),
98+
($issue->{milestone} ? "MS:\cB$issue->{milestone}{title}\cB ": ($pr?$self->_pr_branch($ng, $pr):"")),
9899
$pr&&$pr->{merged_at}?"\cC46merged on ".($pr->{merged_at}=~s/T.*//r):
99100
$issue->{closed_at}?"\cC55closed on ".($issue->{closed_at}=~s/T.*//r):"\cC52".$issue->{state}." since ".($issue->{created_at}=~s/T.*//r);
100101
}

0 commit comments

Comments
 (0)