Last active
March 9, 2017 04:34
-
-
Save artifactsauce/870ba1486d0fe2acad80 to your computer and use it in GitHub Desktop.
gitの共有リポジトリにpushしたらSlackとChatworkにメッセージを通知する ref: http://qiita.com/artifactsauce/items/242c71428fa85307c182
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
package MyApp::Notifier::Base; | |
use warnings; | |
use LWP::UserAgent; | |
sub new { | |
my $class = shift; | |
my $self = { @_ }; | |
bless( $self, $class ); | |
return $self; | |
} | |
sub send { | |
my $self = shift; | |
my ( $repos, $branch, $message ) = @_; | |
my ( $url, $post_data, $headers ) = $self->_prepare_request_parameters( $repos, $branch, $message ); | |
my $ua = LWP::UserAgent->new(); | |
my $response = $ua->post($url, $post_data, %$headers); | |
unless ($response->is_success) { | |
warn $response->status_line; | |
return 0; | |
} | |
return 1; | |
} | |
sub _prepare_request_parameters { | |
die "[ERROR] Have to override this method"; | |
} | |
package MyApp::Notifier::Slack; | |
use warnings; | |
use JSON::PP; | |
@MyApp::Notifier::Slack::ISA = qw(MyApp::Notifier::Base); | |
sub _prepare_request_parameters { | |
my $self = shift; | |
my ( $repos, $branch, $message ) = @_; | |
my $endpoint = "https://hooks.slack.com/services"; | |
my $path_token = $self->{path_token}; | |
my $channel = $self->{channel}; | |
my $username = 'Git hooks'; | |
my $icon = ':jack_o_lantern:'; | |
my $text = "(\`${repos}\`) [\`${branch}\`]"; | |
if ( $branch eq 'master' ) { | |
$text = ":bell: PUSHED INTO \`${branch}\` BRANCH!!!\n${text} ${message}"; | |
} | |
elsif ( ! -f "refs/heads/$branch" ) { | |
$text = "${text} Branch was deleted."; | |
} | |
else { | |
$text = "${text} ${message}"; | |
} | |
my $post_data = { | |
payload => encode_json +{ | |
channel => $channel, | |
username => $username, | |
icon_emoji => $icon, | |
text => $text, | |
}, | |
}; | |
return "$endpoint/$path_token", $post_data, {}; | |
} | |
package MyApp::Notifier::Chatwork; | |
use warnings; | |
@MyApp::Notifier::Chatwork::ISA = qw(MyApp::Notifier::Base); | |
sub _prepare_request_parameters { | |
my $self = shift; | |
my ( $repos, $branch, $message ) = @_; | |
my $endpoint = "https://api.chatwork.com/v1/rooms"; | |
my $token = $self->{token}; | |
my $room_id = $self->{room_id}; | |
my $url = "$endpoint/$room_id/messages"; | |
my $text = "(${repos}) [${branch}] ${message}"; | |
if ( $branch eq 'master') { | |
$text = "[info][title]PUSHED INTO \`master\` BRANCH!!![/title]${text}[/info]"; | |
} | |
my $post_data = { | |
body => $text, | |
}; | |
my $headers = { | |
'X-ChatWorkToken' => $token, | |
}; | |
return $url, $post_data, $headers; | |
} | |
package MyApp::Git; | |
use warnings; | |
use FindBin qw/$Bin/; | |
use File::Basename qw/dirname/; | |
use Encode; | |
use Encode::Guess qw/shift-jis euc-jp 7bit-jis/; | |
sub new { | |
my $class = shift; | |
my $self = { @_ }; | |
bless( $self, $class ); | |
return $self; | |
} | |
sub repos_name { | |
my $self = shift; | |
unless ( defined $self->{repos_name} ) { | |
my $dir = (split( /\//, dirname( $Bin ) ))[-1]; | |
$dir =~s/\.git$//; | |
$self->{repos_name} = $dir; | |
} | |
return $self->{repos_name}; | |
} | |
sub branch_name { | |
my $self = shift; | |
unless ( defined $self->{branch_name} ) { | |
$self->{branch_name} = `git rev-parse --symbolic --abbrev-ref $_[0] 2> /dev/null`; | |
chomp $self->{branch_name}; | |
} | |
return $self->{branch_name}; | |
} | |
sub commit_message { | |
my $self = shift; | |
my $branch = shift; | |
unless ( defined $self->{commit_message} ) { | |
$self->{commit_message} = `git log -1 --pretty=format:"%h - %an : %s" $branch 2> /dev/null`; | |
} | |
return unless $self->{commit_message}; | |
my $decoder = Encode::Guess->guess( $self->{commit_message} ); | |
if ( ref $decoder ) { | |
$self->{commit_message} = $decoder->decode( $self->{commit_message} ); | |
} | |
return $self->{commit_message}; | |
} | |
package main; | |
use warnings; | |
use JSON::PP; | |
scalar @ARGV == 0 && die "[ERROR] Arguments not enough"; | |
my $git = MyApp::Git->new(); | |
my $repos = $git->repos_name(); | |
my $branch = $git->branch_name( @ARGV ); | |
my $message = $git->commit_message( $branch ); | |
my %notifiers = ( | |
'slack' => sub { new MyApp::Notifier::Slack(@_) }, | |
# 'chatwork' => sub { new MyApp::Notifier::Chaktwork(@_) }, | |
); | |
my $config = _load_config(); | |
while ( my ( $name, $generator ) = each %notifiers ) { | |
my $notifier = $generator->( %{$config->{$name}} ); | |
$notifier->send( $repos, $branch, $message ) | |
|| warn "[WARN] Faild to send message."; | |
} | |
exit; | |
sub _load_config { | |
return decode_json( join '', <DATA> ); | |
} | |
1; # magic number | |
__DATA__ | |
{ | |
"slack": { | |
"path_token": "<CHANGE_THIS>", | |
"channel": "<CHANGE_THIS>" | |
}, | |
"chatwork": { | |
"token": "<CHANGE_THIS>", | |
"room_id": "<CHANGE_THIS>" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment