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

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

Bug 1266630
parent ce39f746
#!/usr/bin/python
#
# Gerrit Launchpad and CIA Hook, inspired by:
# https://github.com/hobbs/jirret
# http://cia.vc/clients/git/ciabot.bash
# http://cia.vc/clients/bzr/cia_bzr.py
#
# Copyright (C) 2011 - 2013 Catalyst IT (http://www.catalyst.net.nz)
#
# 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# 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 <http://www.gnu.org/licenses/>.
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
FROM_ADDRESS = 'dev@mahara.org'
TO_ADDRESS_SUFFIX = '@bugs.launchpad.net'
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):
bugs.append(token)
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 = 'http://gitorious.org/mahara/%s/commit/%s' % (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.connect()
s.sendmail(FROM_ADDRESS, [to_address], msg.as_string())
s.quit()
def main():
# See https://git.eclipse.org/r/Documentation/config-hooks.html#_patchset_created
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)
else:
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__':
sys.exit(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