Skip to content
Browse files

Rename setgithubproject -> setgithubprojects, implemented setauthfor …

…and setdefaultauth as well as added tests for that.
  • Loading branch information...
1 parent c00634c commit 8f00a84a85681b8184a31bce8d08b27097a9714e @throughnothing throughnothing committed Oct 18, 2011
Showing with 61 additions and 23 deletions.
  1. +49 −22 lib/Bot/BasicBot/Pluggable/Module/GitHub.pm
  2. +12 −1 t/01-parse-config.t
View
71 lib/Bot/BasicBot/Pluggable/Module/GitHub.pm
@@ -91,7 +91,13 @@ sub auth_for_project {
my $auth_for_project =
$self->store->get('GitHub', 'auth_for_project');
- return $auth_for_project->{$project};
+
+ if ($auth_for_project->{$project}) {
+ return $auth_for_project->{$project};
+ } else {
+ # Return the default auth, if set
+ return $self->store->get('GitHub', 'default_auth');
+ }
}
@@ -119,43 +125,64 @@ sub said {
return unless $mess->{address} eq 'msg';
if ($mess->{body} =~ m{
- ^!setgithubproject \s+
+ ^!setgithubprojects \s+
(?<channel> \#\S+ ) \s+
- (?<project> \S+ )
- ( \s+ (?<auth> \S+ ) )?
+ (\S+\/\S+)+
}xi) {
+ my $channel = $+{channel};
+ my $message = "OK, set projects for $channel: ";
my $projects_for_channel =
$self->store->get('GitHub','projects_for_channel') || {};
-
- if($projects_for_channel->{$+{channel}}) {
- push(@{$projects_for_channel->{$+{channel}}}, $+{project});
- } else {
- $projects_for_channel->{$+{channel}} = [ $+{project} ];
+ $projects_for_channel->{$channel} = []; #set to empty
+
+ while($mess->{body} =~ m{
+ \b(?<project> \S+\/\S+ )
+ }gxi)
+ {
+ my $project = $+{project};
+ push(@{$projects_for_channel->{$channel}}, $project);
+ # Invalidate any cached Net::GitHub object we might have,
+ # so the new settings are used
+ delete $net_github{$project};
+ $message .= " $project";
}
$self->store->set(
'GitHub', 'projects_for_channel', $projects_for_channel
);
+ return $message;
+ } elsif ($mess->{body} =~ /^!setgithubproject/i) {
+ return "Invalid usage. Try '!help github'";
+ } elsif ($mess->{body} =~ m{
+ ^!setdefaultauth \s+
+ (?<auth> \S+ )
+ }xi) {
+ $self->store->set('GitHub', 'default_auth', $+{auth});
+ return "Set default auth credentials for all projects";
+ } elsif ($mess->{body} =~ /^!setdefaultauth/i) {
+ return "Invalid usage. Try '!help github'";
+ } elsif ($mess->{body} =~ m{
+ ^!setauthforproject \s+
+ (?<project> \S+\/\S+) \s+
+ (?<auth> \S+ )
+ }xi) {
+ my ($project, $auth) = ($+{project}, $+{auth});
my $auth_for_project =
$self->store->get('GitHub', 'auth_for_project') || {};
- $auth_for_project->{$+{project}} = $+{auth};
+ $auth_for_project->{$project} = $auth;
$self->store->set(
'GitHub', 'auth_for_project', $auth_for_project
);
-
- # Invalidate any cached Net::GitHub object we might have, so the new
- # settings are used
- delete $net_github{$+{project}};
-
- my $message = "OK, project for $+{channel} set to $+{project}";
- if ($+{auth}) {
- $message .= " (using auth details supplied)";
- }
- return $message;
-
- } elsif ($mess->{body} =~ /^!setgithubproject/i) {
+ return "Set auth credentials for $project";
+ } elsif ($mess->{body} =~ /^!setauthforproject/i) {
return "Invalid usage. Try '!help github'";
+ } elsif ($mess->{body} =~ /^!addgithubproject/i) {
+ #TODO:
+ return "Not Implemented!"
+ } elsif ($mess->{body} =~ /^!deletegithubproject/i) {
+ #TODO:
+ return "Not Implemented!"
} elsif ($mess->{body} =~ /^!showgithubprojects/i){
my $message;
my $projects_for_channel =
View
13 t/01-parse-config.t
@@ -19,6 +19,11 @@ sub get {
return unless $namespace eq 'GitHub';
return $self->{settings}{$key};
}
+sub set {
+ my ($self, $namespace, $key, $value) = @_;
+ return unless $namespace eq 'GitHub';
+ $self->{settings}{$key} = $value;
+}
# Subclass to override fetching of config setting from store
@@ -40,7 +45,7 @@ use Bot::BasicBot::Pluggable::Module::GitHub;
package main;
-use Test::More tests => 5;
+use Test::More tests => 6;
my $plugin = MockBot->new;
@@ -77,3 +82,9 @@ is($plugin->auth_for_project('fake/project'), undef,
'Got undef auth info for non-configured project'
);
+# Test default_auth if set
+$plugin->{_store}->set('GitHub', 'default_auth', 'default:auth');
+
+is($plugin->auth_for_project('someuser/foo'), 'default:auth',
+ 'Got expected default auth info for a project'
+);

0 comments on commit 8f00a84

Please sign in to comment.
Something went wrong with that request. Please try again.