| 1 | package Bot::BasicBot::Pluggable::Module::GitHub; | ||
| 2 | 2 | ▶ | use base 'Bot::BasicBot::Pluggable::Module'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | # This module is intended to be used as a base by the B::B::P::M::GitHub::* | ||
| 5 | # modules, and provides some shared functionality (reading the default project | ||
| 6 | # for a channel from the bot's store, etc). | ||
| 7 | # | ||
| 8 | # It should not be loaded directly by the bot; load the desired modules you | ||
| 9 | # want. | ||
| 10 | |||
| 11 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use Net::GitHub::V2; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | our $VERSION = '0.04'; | ||
| 15 | |||
| 16 | # We'll cache suitably-configured Net::GitHub objects for each channel. | ||
| 17 | my %net_github; | ||
| 18 | |||
| 19 | # Given a channel name or a user/project, return a suitably-configured | ||
| 20 | # Net::GitHub::V2 object. | ||
| 21 | # If a channel name is given, we'll look up the default project stored for that | ||
| 22 | # channel; if a user/project is given, we'll just use that. | ||
| 23 | # We'll then see if we have authentication details stored to use, and use them | ||
| 24 | # if appropriate. | ||
| 25 | sub ng { | ||
| 26 | my ($self, $channelorproject) = @_; | ||
| 27 | |||
| 28 | |||
| 29 | # Work out the repo we're using - we may have been given "user/repo", or, if | ||
| 30 | # we were given a channel name, look up the default repo for that channel | ||
| 31 | my ($user,$project); | ||
| 32 | if ($channelorproject =~ m{/}) { | ||
| 33 | ($user, $project) = split '/', $channelorproject, 2; | ||
| 34 | } else { | ||
| 35 | my $chanproject = $self->project_for_channel($channelorproject); | ||
| 36 | ($user, $project) = split '/', $chanproject, 2; | ||
| 37 | } | ||
| 38 | |||
| 39 | return unless $user && $project; | ||
| 40 | |||
| 41 | # If we've already got a suitable Net::GitHub::V2 object, use it: | ||
| 42 | if (my $ng = $net_github{"$user/$project"}) { | ||
| 43 | return $ng; | ||
| 44 | } | ||
| 45 | |||
| 46 | # Right - assemble the params we need to give to Net::GitHub::V2 | ||
| 47 | my %ngparams = ( | ||
| 48 | owner => $user, | ||
| 49 | repo => $project, | ||
| 50 | ); | ||
| 51 | |||
| 52 | # If authentication is needed, add that in too: | ||
| 53 | if (my $auth = $self->auth_for_project("$user/$project")) { | ||
| 54 | my ($user, $token) = split /:/, $auth, 2; | ||
| 55 | $ngparams{login} = $user; | ||
| 56 | $ngparams{token} = $token; | ||
| 57 | $ngparams{always_Authorization} = 1; | ||
| 58 | } | ||
| 59 | return $net_github{"$user/$project"} = Net::GitHub::V2->new(%ngparams); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | # Find the name of the GitHub project for a given channel | ||
| 64 | sub project_for_channel { | ||
| 65 | my ($self, $channel) = @_; | ||
| 66 | |||
| 67 | my $project_for_channel = | ||
| 68 | $self->store->get('GitHub', 'project_for_channel'); | ||
| 69 | return $project_for_channel->{$channel}; | ||
| 70 | } | ||
| 71 | # Alias for backwards compatibility | ||
| 72 | sub github_project { | ||
| 73 | my $self = shift; | ||
| 74 | $self->project_for_channel(@_); | ||
| 75 | } | ||
| 76 | |||
| 77 | # Find auth details to use to access a channel's project | ||
| 78 | sub auth_for_project { | ||
| 79 | my ($self, $project) = @_; | ||
| 80 | |||
| 81 | my $auth_for_project = | ||
| 82 | $self->store->get('GitHub', 'auth_for_project'); | ||
| 83 | return $auth_for_project->{$project}; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | # For each channel the bot is in, call project_for_channel() to find out what | ||
| 88 | # project is appropriate for that channel, and return a hashref of | ||
| 89 | # channel => project (leaving out channels for which no project is defined) | ||
| 90 | sub channels_and_projects { | ||
| 91 | my $self = shift; | ||
| 92 | my %project_for_channel; | ||
| 93 | for my $channel ($self->bot->channels) { | ||
| 94 | if (my $project = $self->project_for_channel($channel)) { | ||
| 95 | $project_for_channel{$channel} = $project; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | return \%project_for_channel; | ||
| 99 | } | ||
| 100 | |||
| 101 | # Support configuring project details for a channel (potentially with auth | ||
| 102 | # details) via a msg. This is a bit too tricky to just leave the Vars module to | ||
| 103 | # handle, I think. (Note that each of the modules which inherit from us will | ||
| 104 | # get this method; one of them will catch it and handle it.) | ||
| 105 | sub said { | ||
| 106 | my ($self, $mess, $pri) = @_; | ||
| 107 | return unless $pri == 2; | ||
| 108 | return unless $mess->{address} eq 'msg'; | ||
| 109 | |||
| 110 | if ($mess->{body} =~ m{ | ||
| 111 | ^!setgithubproject \s+ | ||
| 112 | (?<channel> \#\S+ ) \s+ | ||
| 113 | (?<project> \S+ ) | ||
| 114 | ( \s+ (?<auth> \S+ ) )? | ||
| 115 | }xi) { | ||
| 116 | my $project_for_channel = | ||
| 117 | $self->store->get('GitHub','project_for_channel') || {}; | ||
| 118 | $project_for_channel->{$+{channel}} = $+{project}; | ||
| 119 | $self->store->set( | ||
| 120 | 'GitHub', 'project_for_channel', $project_for_channel | ||
| 121 | ); | ||
| 122 | |||
| 123 | my $auth_for_project = | ||
| 124 | $self->store->get('GitHub', 'auth_for_project') || {}; | ||
| 125 | $auth_for_project->{$+{project}} = $+{auth}; | ||
| 126 | $self->store->set( | ||
| 127 | 'GitHub', 'auth_for_project', $auth_for_project | ||
| 128 | ); | ||
| 129 | |||
| 130 | # Invalidate any cached Net::GitHub object we might have, so the new | ||
| 131 | # settings are used | ||
| 132 | delete $net_github{$+{project}}; | ||
| 133 | |||
| 134 | my $message = "OK, project for $+{channel} set to $+{project}"; | ||
| 135 | if ($+{auth}) { | ||
| 136 | $message .= " (using auth details supplied)"; | ||
| 137 | } | ||
| 138 | return $message; | ||
| 139 | |||
| 140 | } elsif ($mess->{body} =~ /^!setgithubproject/i) { | ||
| 141 | return "Invalid usage. Try '!help github'"; | ||
| 142 | } | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | =head1 NAME | ||
| 148 | |||
| 149 | Bot::BasicBot::Pluggable::Module::GitHub - GitHub-related modules for IRC bots running Bot::BasicBot::Pluggable | ||
| 150 | |||
| 151 | =head1 DESCRIPTION | ||
| 152 | |||
| 153 | A set of modules for L<Bot::BasicBot::Pluggable> bots, providing useful | ||
| 154 | GitHub-related functionality. | ||
| 155 | |||
| 156 | If your team use GitHub and colloborate on IRC, these may well be of interest to | ||
| 157 | you. They're already in use on the L<Dancer> project's IRC channel, and | ||
| 158 | internally at my workplace, UK2. | ||
| 159 | |||
| 160 | Most communication with GitHub uses L<Net::GitHub::V2>, and can use | ||
| 161 | authentication with an API key for private repositories. | ||
| 162 | |||
| 163 | |||
| 164 | =head1 MODULES | ||
| 165 | |||
| 166 | The following modules are included - see the documentation for each for more | ||
| 167 | details on how to use them. | ||
| 168 | |||
| 169 | =over 4 | ||
| 170 | |||
| 171 | =item L<Bot::BasicBot::Pluggable::Module::GitHub::EasyLinks> | ||
| 172 | |||
| 173 | Provide quick URLs to view issues/pull requests etc when someone mentions one - | ||
| 174 | for example: | ||
| 175 | |||
| 176 | <user> Go have a look at Issue 42 | ||
| 177 | <bot1> Issue 42 (It doesn't work) https://github.com/.... | ||
| 178 | <user> I fixed that in 5fcbb01 | ||
| 179 | <bot1> Commit 5fcbb01 (Retarded logic fail.) - https://github.com/.... | ||
| 180 | |||
| 181 | You can set a default project per-channel, so the above examples will look at | ||
| 182 | whatever project is set as default for the channel the message was in. | ||
| 183 | |||
| 184 | You can also explicitly tell it to look at any other GitHub project: | ||
| 185 | |||
| 186 | <user> 5fcbb01 @ bigpresh/Bot-BasicBot-Pluggable-Module-GitHub | ||
| 187 | <bot1> Commit 5fcbb01 (Retarded logic fail.) - https://github.com/... | ||
| 188 | |||
| 189 | |||
| 190 | =item L<Bot::BasicBot::Pluggable::Module::GitHub::PullRequests> | ||
| 191 | |||
| 192 | Monitor pull requests for GitHub projects. | ||
| 193 | |||
| 194 | Example: | ||
| 195 | |||
| 196 | <@bigpresh> !pr | ||
| 197 | < sophie> Open pull requests for sukria/Dancer : 8 pull requests open | ||
| 198 | (felixdo:3, perlpilot:1, jamhed:1, dams:1, ambs:1, JTimothyKing:1) | ||
| 199 | |||
| 200 | =item L<Bot::BasicBot::Pluggable::Module::GitHub::Announce> | ||
| 201 | |||
| 202 | Announces issues raised/closed for each channel's default project. | ||
| 203 | |||
| 204 | Periodically checks on issues for each project, and reports changes, for | ||
| 205 | example: | ||
| 206 | |||
| 207 | < sophie> Issues closed : 667 (YAML::XS for Session::YAML) by jamhed : | ||
| 208 | https://github.com/sukria/Dancer/issues/667 | ||
| 209 | |||
| 210 | |||
| 211 | =back | ||
| 212 | |||
| 213 | =head1 Loading modules | ||
| 214 | |||
| 215 | See the L<Bot::BasicBot::Pluggable> documentation for how to load these modules | ||
| 216 | into your bot. | ||
| 217 | |||
| 218 | Do not load this module directly; load the modules named above individually, to | ||
| 219 | get just the functionality you require. | ||
| 220 | |||
| 221 | This module is intended only to provide a base for the other modules, including | ||
| 222 | shared functionality and common documentation. | ||
| 223 | |||
| 224 | |||
| 225 | =head1 Configuring the default project repo | ||
| 226 | |||
| 227 | The modules above need to know what GitHub project repository they should refer | ||
| 228 | to. | ||
| 229 | |||
| 230 | The project (and, optionally, authentication details, if it's not a public | ||
| 231 | project) are configured with the C<!setgithubproject> command in a private | ||
| 232 | message to the bot. | ||
| 233 | |||
| 234 | You'll need to be authenticated to the bot in order to set the project | ||
| 235 | (see L<the auth module|Bot::BasicBot::Pluggable::Module::Auth>). | ||
| 236 | |||
| 237 | You set the project with: | ||
| 238 | |||
| 239 | !setgithubproject #channel user/projectname | ||
| 240 | |||
| 241 | That sets the default project repo to C<projectname> owned by C<user> on GitHub | ||
| 242 | (in other words, <https://github.com/user/projectname>). | ||
| 243 | |||
| 244 | If the project is a private repository which requires authentication, you can | ||
| 245 | also tell the bot what user and token it should use to authenticate: | ||
| 246 | |||
| 247 | !setgithubproject #channel user/privateproject someuser:githubtokenhere | ||
| 248 | |||
| 249 | You can generate/find an API token for your GitHub account at | ||
| 250 | L<https://github.com/account/admin> | ||
| 251 | |||
| 252 | |||
| 253 | =head1 AUTHOR | ||
| 254 | |||
| 255 | David Precious C<<davidp@preshweb.co.uk>> | ||
| 256 | |||
| 257 | =head1 ACKNOWLEDGEMENTS | ||
| 258 | |||
| 259 | James Ronan C<<james@ronanweb.co.uk>> | ||
| 260 | |||
| 261 | =head1 CONTRIBUTING | ||
| 262 | |||
| 263 | Contributions are very welcome. | ||
| 264 | |||
| 265 | |||
| 266 | =head1 LICENSE AND COPYRIGHT | ||
| 267 | |||
| 268 | Copyright 2011 David Precious. | ||
| 269 | |||
| 270 | This program is free software; you can redistribute it and/or modify it | ||
| 271 | under the terms of either: the GNU General Public License as published | ||
| 272 | by the Free Software Foundation; or the Artistic License. | ||
| 273 | |||
| 274 | See http://dev.perl.org/licenses/ for more information. | ||
| 275 | |||
| 276 | |||
| 277 | =cut | ||
| 278 | |||
| 279 | |||
| 280 | 1; # End of Bot::BasicBot::Pluggable::Module::GitHub | ||