GitHub/EasyLinks.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« GitHub/Announce.pm ↑ Summary GitHub/PullRequests.pm »
1 # A quick Bot::BasicBot::Pluggable module to provide easy links when someone
2 # mentions an issue / pull request / commit.
3 #
4 # David Precious <davidp@preshweb.co.uk>
5  
6 package Bot::BasicBot::Pluggable::Module::GitHub::EasyLinks;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Bot::BasicBot::Pluggable::Module::GitHub;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use base 'Bot::BasicBot::Pluggable::Module::GitHub';
10 use URI::Title;
11  
12 sub help {
13 return <<HELPMSG;
14 Provide convenient links to GitHub issues/pull requests/commits etc.
15  
16 If someone says e.g. "Issue 42", the bot will helpfully provide an URL to view
17 that issue directly.
18  
19 The project these relate to must be configured using the vars module to set the
20 'default project' setting (or directly set user_default_project in the bot's
21 store).
22  
23 HELPMSG
24 }
25  
26  
27 sub said {
28 my ($self, $mess, $pri) = @_;
29
30 return unless $pri == 2;
31  
32  
33 # Loop through matching things in the message body, assembling quick links
34 # ready to return.
35 my @return;
36 match:
37 while ($mess->{body} =~ m{
38 (?:
39 \b
40 # "Issue 42", "PR 42" or "Pull Request 42"
41 (?<thing> (?:issue|gh|pr|pull request) )
42 (?:\s+|-)?
43 (?<num> \d+)
44 |
45 # Or a commit SHA
46 (?<sha> [0-9a-f]{6,})
47 )
48 # Possibly with a specific project repo ("user/repo") appeneded
49 (?: \s* \@ \s* (?<project> \S+/\S+) )?
50 }gxi
51 ) {
52  
53 my $project = $+{project} || $self->github_project($mess->{channel});
54 return unless $project;
55  
56 # Get the Net::GitHub::V2 object we'll be using. (If we don't get one,
57 # for some reason, we can't do anything useful.)
58 my $ng = $self->ng($project) or return;
59  
60 # First, extract what kind of thing we're looking at, and normalise it a
61 # little, then go on to handle it.
62 my $thing = $+{thing};
63 my $thingnum = $+{num};
64  
65 if ($+{sha}) {
66 $thing = 'commit';
67 $thingnum = $+{sha};
68 }
69  
70 warn "OK, about to try to handle $thing $thingnum for $project";
71  
72 # Right, handle it in the approriate way
73 if ($thing =~ /Issue|GH/i) {
74 warn "Handling issue $thingnum";
75 my $issue = $ng->issue->view($thingnum);
76 if (exists $issue->{error}) {
77 push @return, $issue->{error};
78 next match;
79 }
80 push @return, sprintf "Issue %d (%s) - %s",
81 $thingnum,
82 $issue->{title},
83 $issue->{html_url};
84 }
85  
86 # Similarly, pull requests:
87 if ($thing =~ /(?:pr|pull request)/i) {
88 warn "Handling pull request $thingnum";
89 # TODO: send a pull request to add support for fetching details of
90 # pull requests to Net::GitHub::V2, so we can handle PRs on private
91 # repos appropriately.
92 my $pull_url = "https://github.com/$project/pull/$thingnum";
93 my $title = URI::Title::title($pull_url);
94 push @return, "Pull request $thingnum ($title) - $pull_url";
95 }
96  
97 # If it was a commit:
98 if ($thing eq 'commit') {
99 warn "Handling commit $thingnum";
100 my $commit = $ng->commit->show($thingnum);
101 if ($commit && !exists $commit->{error}) {
102 my $title = ( split /\n+/, $commit->{message} )[0];
103 my $url = $commit->{url};
104
105 # Currently, the URL given doesn't include the host, but that
106 # might perhaps change in future, so play it safe:
107 $url = "https://github.com$url" unless $url =~ /^http/;
108 push @return, sprintf "Commit $thingnum (%s) - %s",
109 $title,
110 $url;
111 } else {
112 # We purposefully don't show a message on IRC here, as we guess
113 # what might be a SHA, so we could be annoying saying that we
114 # didn't match a commit when someone said a word that just
115 # happened to look like it could be the start of a SHA.
116 warn "No commit details for $thingnum \@ $project/$thingnum";
117 }
118 }
119 }
120
121 return join "\n", @return;
122 }
123  
124  
125  
126  
127  
128 1;
129  
« GitHub/Announce.pm ↑ Summary GitHub/PullRequests.pm »