Skip to content

simple twitter mercurial hook

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

  1. CharlieNo Gravatar wrote:

    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

    Saturday, April 25, 2009 at 2:48 pm | Permalink
  2. tonymagroNo Gravatar wrote:

    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:

    def __init__(self, ui, repo, patch, diffopts, mapfile, buffered):

    while the 1.1 definition is missing the ‘diffopts’ argument:

    def __init__(self, ui, repo, patch, mapfile, buffered):

    Passing ‘diffopts=None’ as an argument to changeset_templater should solve the issue.

    So the mercurial 1.2 version would look like:

    t = cmdutil.changeset_templater(ui=ui, repo=repo,
                                       patch=False, diffopts=None,
                                       mapfile=None,  buffered=False)

    While the mercurial 1.1 version would look like:

    t = cmdutil.changeset_templater(ui=ui, repo=repo,
                                       patch=False, mapfile=None,
                                       buffered=False)

    Thanks for letting me know. I think I will update the main article with the 1.2 version.

    Sunday, April 26, 2009 at 2:36 am | Permalink
  3. CharlieNo Gravatar wrote:

    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!

    Sunday, April 26, 2009 at 7:09 am | Permalink
  4. tonymagroNo Gravatar wrote:

    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 .

    Tuesday, May 19, 2009 at 6:57 am | Permalink
  5. StodgeNo Gravatar wrote:

    Where do you save this file? In ~/.hg?

    Thanks

    Friday, November 13, 2009 at 6:09 am | Permalink
  6. RezaNo Gravatar wrote:

    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.

    Thursday, December 10, 2009 at 11:55 pm | Permalink
  7. 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

    Wednesday, January 20, 2010 at 6:34 pm | Permalink
  8. http://www.No Gravatar wrote:

    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!

    Tuesday, September 23, 2014 at 4:10 am | Permalink

One Trackback/Pingback

  1. Tony's Blog › simple twitter oauth mercurial hook on Friday, December 24, 2010 at 12:17 pm

    […] older twitter mercurial hook relied on twitter’s basic authentication which, as of August 31, 2010, is no longer […]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

Tony's Blog is using WP-Gravatar