From c0d4d75e0da437ad52d0fffa67107867294c6d4e Mon Sep 17 00:00:00 2001 From: David Precious Date: Thu, 13 Mar 2014 23:12:04 +0000 Subject: [PATCH 01/10] pass, not token. Use a password. These days, that needn't be the user's GH password, though; they can create as many tokens with limited privileges as they want. --- lib/Bot/BasicBot/Pluggable/Module/GitHub.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm index 3d1faf0..41cf2a0 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm @@ -50,7 +50,7 @@ sub ng { if (my $auth = $self->auth_for_project("$user/$project")) { my ($auth_user, $token) = split /:/, $auth, 2; $ngparams{login} = $auth_user; - $ngparams{token} = $token; + $ngparams{pass} = $token; $ngparams{always_Authorization} = 1; } From 36a99631d7767f27a666b96d93bd3787326ecf2e Mon Sep 17 00:00:00 2001 From: David Precious Date: Thu, 13 Mar 2014 23:14:02 +0000 Subject: [PATCH 02/10] Get a Net::GitHub::V3 object. --- lib/Bot/BasicBot/Pluggable/Module/GitHub.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm index 41cf2a0..6de0640 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm @@ -9,7 +9,7 @@ use base 'Bot::BasicBot::Pluggable::Module'; # want. use strict; -use Net::GitHub::V2; +use Net::GitHub::V3; our $VERSION = '0.04'; @@ -54,7 +54,7 @@ sub ng { $ngparams{always_Authorization} = 1; } - return $net_github{"$user/$project"} = Net::GitHub::V2->new(%ngparams); + return $net_github{"$user/$project"} = Net::GitHub::V3->new(%ngparams); } From 3c63b85332950e94d70a688d6e335c301515467e Mon Sep 17 00:00:00 2001 From: David Precious Date: Thu, 13 Mar 2014 23:17:51 +0000 Subject: [PATCH 03/10] Set default user & repo for each N::G object. When getting a Net::GitHub::V3 object for a given owner/repo, call set_default_user_repo() on it before returning it. --- lib/Bot/BasicBot/Pluggable/Module/GitHub.pm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm index 6de0640..1732daa 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm @@ -53,8 +53,10 @@ sub ng { $ngparams{pass} = $token; $ngparams{always_Authorization} = 1; } - - return $net_github{"$user/$project"} = Net::GitHub::V3->new(%ngparams); + + my $ng = Net::GitHub::V3->new(%ngparams); + $ng->set_default_user_repo($user, $project); + return $net_github{"$user/$project"} = $ng; } From 73c427d367671cdd03a96e18a3b9002e78cbfc54 Mon Sep 17 00:00:00 2001 From: David Precious Date: Thu, 13 Mar 2014 23:18:57 +0000 Subject: [PATCH 04/10] Don't pass owner/repo params. Net::GitHub::V3 doesn't use them. --- lib/Bot/BasicBot/Pluggable/Module/GitHub.pm | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm index 1732daa..fe7e669 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub.pm @@ -42,10 +42,7 @@ sub ng { return $ng; } - my %ngparams = ( - owner => $user, - repo => $project, - ); + my %ngparams; # If authentication is needed, add that in too: if (my $auth = $self->auth_for_project("$user/$project")) { my ($auth_user, $token) = split /:/, $auth, 2; From fa4b3c3c5bdcb126c359ed9daa0ea0f89b785d5d Mon Sep 17 00:00:00 2001 From: David Precious Date: Thu, 13 Mar 2014 23:35:03 +0000 Subject: [PATCH 05/10] Make commit details announcements work for V3 API. --- .../Pluggable/Module/GitHub/EasyLinks.pm | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm index 3b48c4a..4e8e12b 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm @@ -54,15 +54,15 @@ sub said { my $project = $+{project} || $self->github_project($mess->{channel}); return unless $project; - + # Search through all the projects to see if the search word matches $project = $self->search_projects($mess->{channel}, $+{search}) || $project; - # Get the Net::GitHub::V2 object we'll be using. (If we don't get one, + # Get the Net::GitHub::V3 object we'll be using. (If we don't get one, # for some reason, we can't do anything useful.) my $ng = $self->ng($project) or return; - + # First, extract what kind of thing we're looking at, and normalise it a # little, then go on to handle it. my $thing = $+{thing}; @@ -103,17 +103,18 @@ sub said { # If it was a commit: if ($thing eq 'commit') { warn "Handling commit $thingnum"; - my $commit = $ng->commit->show($thingnum); + warn "Got an NG object: $ng"; + my $commit = $ng->repos->commit($thingnum); if ($commit && !exists $commit->{error}) { - my $title = ( split /\n+/, $commit->{message} )[0]; - my $url = $commit->{url}; + my $title = ( split /\n+/, $commit->{commit}{message} )[0]; + my $url = $commit->{html_url}; + my $author = $commit->{commit}{author}{name}; # Currently, the URL given doesn't include the host, but that # might perhaps change in future, so play it safe: $url = "https://github.com$url" unless $url =~ /^http/; - push @return, sprintf "Commit $thingnum (%s) - %s", - $title, - $url; + push @return, sprintf "Commit $thingnum (%s by %s) - %s", + $title, $author, $url; } else { # We purposefully don't show a message on IRC here, as we guess # what might be a SHA, so we could be annoying saying that we From 1fccac0be2f826afab4b62e941baac4de5e3ba1a Mon Sep 17 00:00:00 2001 From: David Precious Date: Thu, 13 Mar 2014 23:36:43 +0000 Subject: [PATCH 06/10] Ignore messages from ourselves. Amusing problem I encountered - bot was running this module, plus the normal Title module (which announces the title of links). It would see a SHA, fetch the details, and announce the details - including the link to the commit. The Title module would see that link, and announce the title of the link - which includes the SHA. This module would see the SHA, and announce the details... turtles all the way down! (TODO: submit a patch so Title ignores stuff the bot itself says.) --- lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm index 4e8e12b..b4b94a5 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm @@ -29,6 +29,9 @@ sub said { return unless $pri == 2; + # If the message was from the bot (for e.g. another module announcing the + # title of an URL we just said, etc), go no further, to avoid loops + return if $mess->{who} eq $self->nick; # Loop through matching things in the message body, assembling quick links # ready to return. From 8aaf0949a67067581e67bea1bb4fe3655954627b Mon Sep 17 00:00:00 2001 From: David Precious Date: Thu, 13 Mar 2014 23:54:09 +0000 Subject: [PATCH 07/10] $self->bot->nick, not $self->nick. --- lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm index b4b94a5..e6280b6 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm @@ -31,7 +31,7 @@ sub said { # If the message was from the bot (for e.g. another module announcing the # title of an URL we just said, etc), go no further, to avoid loops - return if $mess->{who} eq $self->nick; + return if $mess->{who} eq $self->bot->nick; # Loop through matching things in the message body, assembling quick links # ready to return. From cd745d4085f45b093901b058b9252781a0eae910 Mon Sep 17 00:00:00 2001 From: David Precious Date: Thu, 13 Mar 2014 23:54:23 +0000 Subject: [PATCH 08/10] Make issues work with V3 API. Wow, that was easy :) --- lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm index e6280b6..407b1f1 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm @@ -81,7 +81,7 @@ sub said { # Right, handle it in the approriate way if ($thing =~ /Issue|GH/i) { warn "Handling issue $thingnum"; - my $issue = $ng->issue->view($thingnum); + my $issue = $ng->issue->issue($thingnum); if (exists $issue->{error}) { push @return, $issue->{error}; next match; From 1cc9963942e627d23c629b1d974f714598238c31 Mon Sep 17 00:00:00 2001 From: David Precious Date: Fri, 14 Mar 2014 00:00:37 +0000 Subject: [PATCH 09/10] Get PR details via V3 API. Use the V3 API to get pull request details, rather than just URI::Title. Finally, we can handle PRs for private repos! --- lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm index 407b1f1..0efe5c0 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm @@ -98,9 +98,10 @@ sub said { # TODO: send a pull request to add support for fetching details of # pull requests to Net::GitHub::V2, so we can handle PRs on private # repos appropriately. - my $pull_url = "https://github.com/$project/pull/$thingnum"; - my $title = URI::Title::title($pull_url); - push @return, "Pull request $thingnum ($title) - $pull_url"; + my $pull = $ng->pull_request->pull($thingnum); + + push @return, sprintf "Pull request $thingnum (%s by %s) - %s"; + $pull->{title}, $pull->{user}{login}, $pull->{html_url}; } # If it was a commit: From ffb03d1cb3357587e760b09b8be1b602c0f4420b Mon Sep 17 00:00:00 2001 From: David Precious Date: Fri, 14 Mar 2014 00:03:50 +0000 Subject: [PATCH 10/10] Include PR status in details. --- lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm index 0efe5c0..aa4742b 100644 --- a/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm +++ b/lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm @@ -99,9 +99,13 @@ sub said { # pull requests to Net::GitHub::V2, so we can handle PRs on private # repos appropriately. my $pull = $ng->pull_request->pull($thingnum); - - push @return, sprintf "Pull request $thingnum (%s by %s) - %s"; - $pull->{title}, $pull->{user}{login}, $pull->{html_url}; + my $status = $pull->{merged} ? 'merged' : 'open'; + push @return, + sprintf "Pull request $thingnum is %s (%s by %s) - %s", + $status, + $pull->{title}, + $pull->{user}{login}, + $pull->{html_url}; } # If it was a commit: