Commit 2947ceb1 authored by Aaron Wells's avatar Aaron Wells
Browse files

A patchset-created hook, to send a message to Launchpad when a new change is uploaded

Bug 1266630
parent ce39f746
# Gerrit Launchpad and CIA Hook, inspired by:
# Copyright (C) 2011 - 2013 Catalyst IT (
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <>.
from email.mime.text import MIMEText
from getopt import getopt
import re
import smtplib
import subprocess
import sys
from xml.dom.minidom import Document
import xmlrpclib
BASE_DIR = '/home/gerrit/mahara_reviews'
def send_notifications(change_url, project, branch, uploader, commit):
# Extract git log of all merged commits
git_log = subprocess.Popen(['git', '--git-dir=' + BASE_DIR + '/git/' + project + '.git', 'log', '--no-merges', commit + '^1..' + commit], stdout=subprocess.PIPE).communicate()[0]
# In launchpad's email api, leading whitespace followed by a keyword is how you send a command in your email.
# So, we need to remove the leading spaces from every line
unindent = re.compile('^[ ]+', re.MULTILINE);
git_log = re.sub(unindent, '', git_log);
# Find bug numbers referenced in the git log
bug_regexp = '[Bb]ug:? *#?([0-9]+)'
tokens = re.split(bug_regexp, git_log)
# Extract unique bug numbers
bugs = []
for token in tokens:
if re.match('^\d+$', token) and (token not in bugs):
send_bug_mail(token, change_url, project, commit, uploader, branch, git_log)
def send_bug_mail(bug_number, change_url, project, commit, uploader, branch, git_log):
to_address = bug_number + TO_ADDRESS_SUFFIX
gitorious_url = '' % (project, commit)
body = 'Patch for "%s" branch: %s\n' % (branch, change_url)
msg = MIMEText(body)
msg['Subject'] = 'A patch has been submitted for review'
msg['From'] = FROM_ADDRESS
msg['To'] = to_address
s = smtplib.SMTP()
s.sendmail(FROM_ADDRESS, [to_address], msg.as_string())
def main():
# See
gerrit_args = ['change=', 'is-draft=', 'change-url=', 'project=', 'branch=', 'topic=', 'uploader=', 'commit=', 'patchset=']
args, unused = getopt(sys.argv[1:], '', gerrit_args)
change_url = project = branch = uploader = commit = None
for argname, argv in args:
if argname == '--change-url':
change_url = argv
elif argname == '--project':
project = argv
elif argname == '--branch':
branch = argv
elif argname == '--uploader':
uploader = argv
elif argname == '--commit':
commit = argv
elif argname == '--is-draft':
is_draft = argv
elif argname == '--patchset':
patchset = argv
if change_url and project and branch and uploader and commit and patchset and is_draft:
# Comment on Launchpad only on first patchset for each change, and no notification for drafts.
if is_draft != 'true' and patchset == '1':
send_notifications(change_url, project, branch, uploader, commit)
print 'Missing arguments: '
if not change_url:
print 'change_url '
if not project:
print 'project '
if not branch:
print 'branch '
if not uploader:
print 'uploader '
if not commit:
print 'commit '
if not patchset:
print 'patchset '
if not is_draft:
print 'is_draft '
return 1
return 0;
if __name__ == '__main__':
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment