Saturday, August 7, 2010

BASH post-commit script for posting SVN commits to twitter (with Trac /bit.ly link)

EDIT: Changes to the twitter API broke this script. See workaround and new script here: Workaround for BASH twitter script with OAuth and SuperTweet.net

Recently I got the idea of using twitter to follow commits to various SVN repositories. I thought this might be of interest to someone out there. It's free to use (under the Apache 2.0 license).

We have Trac pages set up for our projects, so I use bit.ly to shorten URLs to the changeset. If this isn't applicable to your project, you can remove it from the script. To get it working, simply place this script in the "hooks" directory of your SVN repository, and name it "post-commit". It must also be executable. I would recommend restricting read access to your user, and the apache user (or whatever SVN runs under).

Here's the format of the tweets: [revision number] [username] [branch] ([num files updated, added, delated]): [comment] [trac changeset url]

Tweets longer than 140 characters will be automatically shortened (without ruining the trac URL)

Here's an example:

opensha_svn 6920 kmilner releases (6U, 0A, 0D): merged from trunk: updated build version to 1.0.0 http://bit.ly/cVkq3W

Here's the code (don't forget to fill in the blanks with your information!):

#!/bin/bash

REPOS="$1"
REV="$2"

### USER SPECIFIC PROPERTIES...CHAGE THESE! ###
# this is the path to your SVN binaries, you'll probably have to change this
SVNDIR='/usr/local/subversion/default/bin/'
# this is the trac URL. be sure to excape special characters like ':' with '%3A' and '/' with '%2F'
# I left an example to help with formatting, you should fill it in!
TRAC_URL="https%3A%2F%2Fintensity.usc.edu%2Ftrac%2Fopensha%2Fchangeset%2F${REV}"
# this is your bit.ly login name
BITLY_LOGIN="FILLMEIN!"
# this is your bit.ly API key
BITLY_API="FILLMEIN!"
# twitter usename
TWITTER_USERNAME="FILLMEIN!"
# twitter password
TWITTER_PASSWORD="FILLMEIN!"

# ---------------------------------
# This gathers some info on the commit, and "tweets" it to the twitter account
# "opensha_svn" at: http://twitter.com/opensha_svn .
# This is half for fun, and half to make it easier for Kevin to track commits.
# ---------------------------------
COMMENT=`$SVNDIR/svnlook log -r${REV} ${REPOS}`
AUTHOR=`$SVNDIR/svnlook author -r${REV} ${REPOS}`
UP=`$SVNDIR/svnlook changed -r${REV} ${REPOS} | awk '{ print $1 }' | grep -c U`
ADD=`$SVNDIR/svnlook changed -r${REV} ${REPOS} | awk '{ print $1 }' | grep -c A`
DEL=`$SVNDIR/svnlook changed -r${REV} ${REPOS} | awk '{ print $1 }' | grep -c D`

numTrunk=`$SVNDIR/svnlook dirs-changed -r${REV} ${REPOS} | grep -c trunk`

if [[ $numTrunk -gt 0 ]];then
codebase="trunk"
else
codebase=`$SVNDIR/svnlook dirs-changed -r${REV} ${REPOS} | cut -f2 --delimiter="/" | head -n 1`
fi

tweet="$REV $AUTHOR $codebase (${UP}U, ${ADD}A, ${DEL}D): $COMMENT"

if [[ $TRAC_URL ]];then
bitly=`curl "http://api.bit.ly/v3/shorten?login=${BITLY_LOGIN}&apiKey=${BITLY_API}&uri=${TRAC_URL}&format=txt"`

bitlychars=`echo -n $bitly | wc --chars`
if [[ $bitlychars -gt 25 ]];then
bitlychars=0
bitly=""
fi
fi

if [[ `echo -n "$tweet $bitly" | wc -c` -gt 140 ]];then
let chars=136-$bitlychars
tweet="`echo -n $tweet | head -c $chars`... $bitly"
else
tweet="$tweet $bitly"
fi

echo "tweet of len `echo -n "$tweet" | wc -c`: $tweet"

curl --basic --user ${TWITTER_USERNAME}:${TWITTER_PASSWORD} --data status="${tweet}" https://twitter.com/statuses/update.xml

2 comments:

  1. I just read your first post and didn't understand a thing. I hope you post something, someday that us "normal" folk can follow!

    ReplyDelete
  2. Thank you for this. I found it extremely helpful. It's amazing what a search for "BASH commit script" can bring up!

    ReplyDelete