Source

hghooks / notifyhudson.py

Full commit
"""Notifies Hudson to trigger build

Usage::

    [hooks]
    changegroup.notifyhudson = python:hghooks.notifyhudson.hook

    [hudson]
    baseurl = http://hudson.example.org/
    jobs = foo, bar
"""
from mercurial.i18n import _
from httplib import HTTPConnection
from urlparse import urlparse

def notifyhudson(ui, baseurl, jobs):
    """Notifies repository updates to Hudson"""
    if not baseurl:
        ui.warn(_("notifyhudson: baseurl not specified\n"))
        return

    if not jobs:  # for the case hook globally, config locally
        ui.debug(_("notifyhudson: jobs not specified\n"))
        return

    for e in jobs:
        ui.status(_("notifyjobs: request for hudson to build %s\n") % e)
        url = urlparse(baseurl.rstrip('/') + '/job/%s/build' % e)

        conn = HTTPConnection(url.hostname, url.port or 80)
        try:
            conn.request('GET', url.path)
            resp = conn.getresponse()
            if resp.status >= 400:
                ui.warn(_("notifyjobs: unexpected status %r for %s\n") % (resp.status, url.geturl()))
        finally:
            conn.close()

def hook(ui, repo, hooktype, **kwargs):
    notifyhudson(ui, baseurl=ui.config('hudson', 'baseurl'),
                 jobs=[e.strip() for e in ui.config('hudson', 'jobs', '').split(',')
                       if e.strip()])