Skip to content

Commit

Permalink
Rename setgithubproject -> setgithubprojects, implemented setauthfor …
Browse files Browse the repository at this point in the history
…and setdefaultauth as well as added tests for that.
  • Loading branch information
throughnothing committed Oct 18, 2011
1 parent c00634c commit 8f00a84
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
71 changes: 49 additions & 22 deletions lib/Bot/BasicBot/Pluggable/Module/GitHub.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}


Expand Down Expand Up @@ -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 =
Expand Down
13 changes: 12 additions & 1 deletion t/01-parse-config.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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.