UPDATE: This script uses basic auth and no longer works as of Aug 31, 2010. Please use the updated version with OAuth support
This is a really simple mercurial 1.2 hook for posting incoming changesets to twitter. It requires python-twitter.
File: hgtwitter.py
''' [extensions] hgext.hgtwitter= [hooks] incoming.notify = python:hgext.hgtwitter.hook [twitter] username = twitter_username password = twitter_password ''' from mercurial import cmdutil, templater import twitter tweet_template = ''' |{root|basename}:{rev}| {desc|strip} '''.strip() def hook(ui, repo, hooktype, node=None, source=None, **kwargs): ctx = repo[node] tuser = ui.config('twitter', 'username') tpass = ui.config('twitter', 'password') t = cmdutil.changeset_templater(ui=ui, repo=repo, patch=False, diffopts=None, mapfile=None, buffered=False) t.use_template(templater.parsestring(tweet_template, quoted=False)) ui.pushbuffer() t.show(ctx, changes=ctx.changeset(), root=repo.root) tweet = ui.popbuffer() if len(tweet) > 140: tweet = tweet[:139] + u"\u2026" api = twitter.Api(username=tuser, password=tpass) status = api.PostUpdate(tweet) |
If you have problems with mercurial 1.1 and lower, try removing the diffopts argument from changeset_templater() so it looks like:
t = cmdutil.changeset_templater(ui=ui, repo=repo, patch=False, mapfile=None, buffered=False) |
8 Comments
Thanks for the script, it looks good, but I’m having a problem. I got the following message back:
error: incoming.notify hook raised an exception: __init__() takes exactly 7 non-keyword arguments (4 given)
I’m really not sure where the issue would be as I’m not given any trace (do you know how to get a trace?) and I don’t know how to debug without doing another commit (which is fine…). Anyway, I thought you might have some ideas?
My setup is as follows:
Python 2.4.4
Mercurial – 1.2.1
SimpleJSON 2.0.9
Python Twitter 0.5
Thanks for your help,
Charlie
I recall having a similar issue. I think the changeset_templater init function definition changed between mercurial 1.1 and 1.2. The 1.2 definition takes an extra ‘diffopts’ argument:
while the 1.1 definition is missing the ‘diffopts’ argument:
Passing ‘diffopts=None’ as an argument to changeset_templater should solve the issue.
So the mercurial 1.2 version would look like:
While the mercurial 1.1 version would look like:
Thanks for letting me know. I think I will update the main article with the 1.2 version.
Thanks! It’s working great now! I’m seeing one error when I do a push still, but it doesn’t seem to hurt anything (I get my tweets). However, I thought I’d send it along in case you had any ideas:
error: incoming.notify hook raised an exception: object is not callable
Anyway, thank you again for this great little hook. With that (and Trac), I’ve got everything I need!
Sorry it’s taken me so long to respond. I finally got around to upgrading my mercurial to 1.2.1. The extension seems to be working and doesn’t produce any errors. One thing that I can think of that might cause this issue would be version 0.5 of python-twitter . The latest released version is 0.5 but I think it’s from around 2007 and it seems they have been working on it without an official release for 2 years. I use the latest svn version which contains some important bug fixes. You can find the latest svn version of python-twitter here http://code.google.com/p/python-twitter/source/checkout .
Where do you save this file? In ~/.hg?
Thanks
Hello, I tried to incorporate your hgtwitter script on my mercurial server but failed to work.
Here’s what I did:
– I put hgtwitter.py in the same folder as mercurial (In my Ubuntu, it is in /usr/local/lib/python2.5/site-packages/hgext
– I added these lines in /etc/mercurial/hgrc (is it okay if I put it here, since I want it to update status on Twitter for all my repos)
[extensions]
hgext.hgrc=
[hooks]
incoming.notify = python:hgext.hgtwitter.hook
[twitter]
username = my_user_name (should it be a string?)
password = my_password (should this also be a string?)
But when I try to push changes to the server, I got:
abort: incoming.notify hook is invalid (import of “hgext.hgtwitter” failed)
It confuses me since in python shell, import hgext.hgtwitter seems to work.
Can you help me out?
Thanks.
When you’re using:
incoming.notify = python:hgext.hgtwitter.hook
You should place the hgtwitter.py file in your site-packages/hgext/ folder (in $PYHTONPATH)
See how modules work here:
http://docs.python.org/tutorial/modules.html
Cheers,
~ Dani
Thanks for the marvelous posting! I certainly enjoyed reading it,
you happen to be a great author.I will remember to bookmark your blog
and definitely will come back down the road. I want to
encourage one to continue your great posts, have a nice day!
One Trackback/Pingback
[…] older twitter mercurial hook relied on twitter’s basic authentication which, as of August 31, 2010, is no longer […]
Post a Comment