Tuesday, October 26, 2010

Workaround for BASH twitter script with OAuth and SuperTweet.net

When twitter disabled basic authentication for OAuth, my BASH post-commit script for posting SVN commits to twitter was broken. I didn't feel like writing an OAuth bash client, and didn't want to install any 3rd party libraries, so I found this solution: http://www.supertweet.net/

Using supertweet, you create a password for supertweet and connect it to your twitter account. You can then perform command line status updates using curl without using OAuth. The best thing is that you never actually give supertweet your twitter password, and can disable it at any time. Here's the curl command (via http://mrblog.org/2010/05/20/an-answer-for-twitter-oauth-pacalypse/):

curl -u user:password -d "status=playing with cURL and the SuperTweet.net API" http://api.supertweet.net/1/statuses/update.xml

So with that in mind, I updated the SVN post commit script. I hope it helps!

Here is the new version:

#!/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'
TRAC_URL="https%3A%2F%2Fopensha.usc.edu%2Ftrac%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!"
# password for supertweet.net
SUPERTWEET_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 --user ${TWITTER_USERNAME}:${SUPERTWEET_PASSWORD} --data status="${tweet}" http://api.supertweet.net/1/statuses/update.xml