Home > googlecode, subversion, thrudb, twitter > Twittering subversion commits

Twittering subversion commits

January 31st, 2009

Since I’ve been working on Thrudb I’ve become a big twitter user. So naturally I’ve created a Thrudb twitter account for people who are interested in tracking progress on the project.  I also wanted to have the changeset tweeted whenever I commit code into subversion.

Subversion has a post-commit hook that will run a script when you commit something so I ended up finding twitvn which does what I wanted but only for trac based projects. Thrudb uses googlecode so it gets more complicated since we can’t install scripts on google hardware :) but they do support a nifty url callback that will post the commit info to you.

So off I went and whipped up a script just for googlecode projects that want to tweet their commits. Just put this in as a cgi on your webserver and tell googlecode the location (under Administration->Source tab). Here is an example of the output.

#!/usr/bin/perl

use strict;
use warnings;

use Net::Twitter;
use JSON::Any;
use Digest::HMAC_MD5 qw(hmac_md5_hex);
use LWP::Simple;

#Just update these
use constant SECRET_KEY   => "SECRET_KEY_FROM_GOOGLE";
use constant TWITTER_USER => "TWITTER_USER";
use constant TWITTER_PASS => "TWITTER_PASS";

#check for defined digest from google
my $remote_digest = $ENV{HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC};
die("missing hmac digest") unless defined $remote_digest;

#fetch json message upto 100k
die("message too long") if $ENV{CONTENT_LENGTH} > 100000;
my $json_str;
read(STDIN, $json_str, $ENV{CONTENT_LENGTH});

#calc local digest and verify
my $digest = hmac_md5_hex($json_str, SECRET_KEY);
die("digests don't match") unless $digest eq $remote_digest;

#construct tweet and shorten changeset url
my $obj     = JSON::Any->jsonToObj($json_str);
my $comment = $obj->{revisions}[0]->{message};
my $url     = get "http://is.gd/api.php?longurl=".
    "http://code.google.com/p/".$obj->{project_name}.
    "/source/detail?r=".$obj->{revisions}[0]{revision};

die("problem shortening $url") unless defined $url && $url !~ "Error";

my $tweet = "svn: ".$comment." ".$url;

#shorten?
if(length($tweet) > 140){
    $tweet = substr($comment,0,140 - 5 - (length($tweet) - 140))."... ".$url;
}

#tweet!
Net::Twitter->new(username=>TWITTER_USER(), password=>TWITTER_PASS() )
    ->update($tweet);

print "content-type:plain/textnn";
print "OKn";

jake googlecode, subversion, thrudb, twitter

Viewing 3 Comments

 
close Reblog this comment
blog comments powered by Disqus