Gerrit Project Builder Tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
12 KiB

#!/usr/bin/env python
# Copyright (c) 2011 OpenStack Foundation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# This is designed to be called by a gerrit hook. It searched new
# patchsets for strings like "bug FOO" and updates corresponding Launchpad
# bugs status.
import argparse
import os
import re
import subprocess
from launchpadlib import launchpad
from launchpadlib import uris
from jeepyb import projects as p
from jeepyb import utils as u
GERRIT_GIT_DIR = os.environ.get(
'GERRIT_GIT_DIR', '/home/gerrit2/review_site/git')
GERRIT_CACHE_DIR = os.path.expanduser(
GERRIT_CREDENTIALS = os.path.expanduser(
def fix_or_related_fix(related):
if related:
return "Related fix"
return "Fix"
def add_change_abandoned_message(bugtask, change_url, project,
branch, abandoner, reason):
subject = ('Change abandoned on %s (%s)'
% (u.short_project_name(project), branch))
body = ('Change abandoned by %s on branch: %s\nReview: %s'
% (abandoner, branch, change_url))
if reason:
body += ('\nReason: %s' % (reason))
bugtask.bug.newMessage(subject=subject, content=body)
def add_change_proposed_message(bugtask, change_url, project, branch,
fix = fix_or_related_fix(related)
subject = ('%s proposed to %s (%s)'
% (fix, u.short_project_name(project), branch))
body = '%s proposed to branch: %s\nReview: %s' % (fix, branch, change_url)
bugtask.bug.newMessage(subject=subject, content=body)
def add_change_merged_message(bugtask, change_url, project, commit,
submitter, branch, git_log, related=False):
subject = '%s merged to %s (%s)' % (fix_or_related_fix(related),
u.short_project_name(project), branch)
git_url = '' % (project, commit)
body = '''Reviewed: %s
Committed: %s
Submitter: %s
Branch: %s\n''' % (change_url, git_url, submitter, branch)
body = body + '\n' + git_log
bugtask.bug.newMessage(subject=subject, content=body)
def set_in_progress(bugtask, launchpad, change_url):
"""Set bug In progress"""
bugtask.status = "In Progress"
def set_fix_committed(bugtask):
"""Set bug fix committed."""
bugtask.status = "Fix Committed"
def set_fix_released(bugtask):
"""Set bug fix released."""
bugtask.status = "Fix Released"
def release_fixcommitted(bugtask):
"""Set bug FixReleased if it was FixCommitted."""
if bugtask.status == u'Fix Committed':
def tag_in_branchname(bugtask, branch):
"""Tag bug with in-branch-name tag (if name is appropriate)."""
lp_bug = bugtask.bug
branch_name = branch.replace('/', '-')
if branch_name.replace('-', '').isalnum():
lp_bug.tags = lp_bug.tags + ["in-%s" % branch_name]
lp_bug.tags.append("in-%s" % branch_name)
class Task:
def __init__(self, lp_task, prefix):
'''Prefixes associated with bug references will allow for certain
changes to be made to the bug's launchpad (lp) page. The following
tokens represent the automation currently taking place.
add_comment -> Adds a comment to the bug's lp page.
sidenote -> Adds a 'related' comment to the bug's lp page.
set_in_progress -> Sets the bug's lp status to 'In Progress'.
set_fix_released -> Sets the bug's lp status to 'Fix Released'.
set_fix_committed -> Sets the bug's lp status to 'Fix Committed'.
changes_needed, when populated, simply indicates the actions that are
available to be taken based on the value of 'prefix'.
self.lp_task = lp_task
self.changes_needed = []
# If no prefix was matched, default to 'closes'.
prefix = prefix.split('-')[0].lower() if prefix else 'closes'
if prefix in ('closes', 'fixes', 'resolves'):
elif prefix in ('partial',):
self.changes_needed.extend(('add_comment', 'set_in_progress'))
elif prefix in ('related', 'impacts', 'affects'):
# prefix is not recognized.
def needs_change(self, change):
'''Return a boolean indicating if given 'change' needs to be made.'''
if change in self.changes_needed:
return True
return False
def process_bugtask(launchpad, task, git_log, args):
"""Apply changes to lp bug tasks, based on hook / branch."""
bugtask = task.lp_task
series = None
if args.hook == "change-abandoned":
add_change_abandoned_message(bugtask, args.change_url,
args.project, args.branch,
args.abandoner, args.reason)
if args.hook == "change-merged":
if args.branch == 'master':
if (not p.is_delay_release(args.project) and
if (bugtask.status != u'Fix Released' and
elif args.branch.startswith('proposed/'):
series = args.branch.rsplit('/', 1)[-1]
if series:
# Look for a related task matching the series.
for reltask in bugtask.related_tasks:
if (reltask.bug_target_name.endswith(series) and
reltask.status != u'Fix Released' and
# Use tag_in_branchname if there isn't any.
tag_in_branchname(bugtask, args.branch)
if task.needs_change('add_comment') or task.needs_change('sidenote'):
add_change_merged_message(bugtask, args.change_url, args.project,
args.commit, args.submitter, args.branch,
if args.hook == "patchset-created":
if args.branch == 'master':
if (bugtask.status not in [u'Fix Committed', u'Fix Released'] and
set_in_progress(bugtask, launchpad, args.change_url)
series = args.branch.rsplit('/', 1)[-1]
if series:
# Look for a related task matching the series.
for reltask in bugtask.related_tasks:
if (reltask.bug_target_name.endswith(series) and
task.needs_change('set_in_progress') and
reltask.status not in [u'Fix Committed',
u'Fix Released']):
set_in_progress(reltask, launchpad, args.change_url)
if args.patchset == '1' and (task.needs_change('add_comment') or
add_change_proposed_message(bugtask, args.change_url,
args.project, args.branch,
def find_bugs(launchpad, git_log, args):
'''Find bugs referenced in the git log and return related tasks.
Our regular expression is composed of three major parts:
part1: Matches only at start-of-line (required). Optionally matches any
word or hyphen separated words.
part2: Matches the words 'bug' or 'lp' on a word boundary (required).
part3: Matches a whole number (required).
The following patterns will be matched properly:
bug # 555555
Closes-Bug: 555555
Fixes: bug # 555555
Resolves: bug 555555
Partial-Bug: lp bug # 555555
:returns: an iterable containing Task objects.
project = args.project
if p.is_no_launchpad_bugs(project):
return []
projects = p.project_to_groups(project)
part1 = r'^[\t ]*(?P<prefix>[-\w]+)?[\s:]*'
part2 = r'(?:\b(?:bug|lp)\b[\s#:]*)+'
part3 = r'(?P<bug_number>\d+)\s*?$'
regexp = part1 + part2 + part3
matches = re.finditer(regexp, git_log, flags=re.I | re.M)
# Extract unique bug tasks and associated prefixes.
bugtasks = {}
for match in matches:
prefix ='prefix')
bug_num ='bug_number')
if bug_num not in bugtasks:
lp_bug = launchpad.bugs[bug_num]
for lp_task in lp_bug.bug_tasks:
if lp_task.bug_target_name in projects:
bugtasks[bug_num] = Task(lp_task, prefix)
except KeyError:
# Unknown bug.
return bugtasks.values()
def extract_git_log(args):
"""Extract git log of all merged commits."""
cmd = ['git',
'--git-dir=' + GERRIT_GIT_DIR + '/' + args.project + '.git',
'log', '--no-merges', args.commit + '^1..' + args.commit]
return subprocess.Popen(
cmd, stdout=subprocess.PIPE).communicate()[0].decode('utf-8')
def main():
parser = argparse.ArgumentParser()
# common
parser.add_argument('--change', default=None)
parser.add_argument('--change-url', default=None)
parser.add_argument('--project', default=None)
parser.add_argument('--branch', default=None)
parser.add_argument('--commit', default=None)
parser.add_argument('--topic', default=None)
parser.add_argument('--change-owner', default=None)
parser.add_argument('--change-owner-username', default=None)
# change-abandoned
parser.add_argument('--abandoner', default=None)
parser.add_argument('--abandoner-username', default=None)
parser.add_argument('--reason', default=None)
# change-merged
parser.add_argument('--submitter', default=None)
parser.add_argument('--submitter-username', default=None)
parser.add_argument('--newrev', default=None)
# patchset-created
parser.add_argument('--uploader', default=None)
parser.add_argument('--uploader-username', default=None)
parser.add_argument('--patchset', default=None)
parser.add_argument('--kind', default=None)
args = parser.parse_args()
# Connect to Launchpad.
lpconn = launchpad.Launchpad.login_with(
credentials_file=GERRIT_CREDENTIALS, version='devel')
# Get git log.
git_log = extract_git_log(args)
# Process tasks found in git log.
for task in find_bugs(lpconn, git_log, args):
process_bugtask(lpconn, task, git_log, args)
if __name__ == "__main__":