blob: 5c8ba8c76e20a593ba253aace7dc93d7e9920e53 [file] [log] [blame]
Monty Taylor6c9634c2012-07-28 11:27:47 -05001#!/usr/bin/env python
2# Copyright (c) 2012 OpenStack, LLC.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16# This is designed to be called by a gerrit hook. It searched new
17# patchsets for strings like "bug FOO" and updates corresponding Launchpad
18# bugs status.
19
20import argparse
21import re
22import subprocess
23import smtplib
24
25from email.mime.text import MIMEText
26
27BASE_DIR = '/home/gerrit2/review_site'
28EMAIL_TEMPLATE = """
29Hi, I'd like you to take a look at this patch for potential
Clark Boylan5a22b192012-10-25 14:55:32 -070030%s.
Monty Taylor6c9634c2012-07-28 11:27:47 -050031%s
32
33Log:
34%s
35"""
Monty Taylor6c9634c2012-07-28 11:27:47 -050036
37def process_impact(git_log, args):
Clark Boylan5a22b192012-10-25 14:55:32 -070038 """Notify mail list of impact"""
39 email_content = EMAIL_TEMPLATE % (args.impact, args.change_url, git_log)
Monty Taylor6c9634c2012-07-28 11:27:47 -050040 msg = MIMEText(email_content)
Clark Boylan5a22b192012-10-25 14:55:32 -070041 msg['Subject'] = '[%s] %s review request change %s' % \
42 (args.project, args.impact, args.change)
Monty Taylor6c9634c2012-07-28 11:27:47 -050043 msg['From'] = 'gerrit2@review.openstack.org'
Clark Boylan5a22b192012-10-25 14:55:32 -070044 msg['To'] = args.dest_address
Monty Taylor6c9634c2012-07-28 11:27:47 -050045
46 s = smtplib.SMTP('localhost')
Clark Boylan5a22b192012-10-25 14:55:32 -070047 s.sendmail('gerrit2@review.openstack.org',
48 args.dest_address, msg.as_string())
Monty Taylor6c9634c2012-07-28 11:27:47 -050049 s.quit()
50
Clark Boylan5a22b192012-10-25 14:55:32 -070051def impacted(git_log, impact_string):
52 """Determine if a changes log indicates there is an impact"""
53 return re.search(impact_string, git_log, re.IGNORECASE)
Monty Taylor6c9634c2012-07-28 11:27:47 -050054
55def extract_git_log(args):
56 """Extract git log of all merged commits"""
57 cmd = ['git',
58 '--git-dir=' + BASE_DIR + '/git/' + args.project + '.git',
59 'log', '--no-merges', args.commit + '^1..' + args.commit]
60 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
61
62
63def main():
64 parser = argparse.ArgumentParser()
65 parser.add_argument('hook')
66 #common
67 parser.add_argument('--change', default=None)
68 parser.add_argument('--change-url', default=None)
69 parser.add_argument('--project', default=None)
70 parser.add_argument('--branch', default=None)
71 parser.add_argument('--commit', default=None)
72 #change-merged
73 parser.add_argument('--submitter', default=None)
74 #patchset-created
75 parser.add_argument('--uploader', default=None)
76 parser.add_argument('--patchset', default=None)
Clark Boylan5a22b192012-10-25 14:55:32 -070077 # Not passed by gerrit:
78 parser.add_argument('--impact', default=None)
79 parser.add_argument('--dest-address', default=None)
Monty Taylor6c9634c2012-07-28 11:27:47 -050080
81 args = parser.parse_args()
82
83 # Get git log
84 git_log = extract_git_log(args)
85
Clark Boylan5a22b192012-10-25 14:55:32 -070086 # Process impacts found in git log
87 if impacted(git_log, args.impact):
Monty Taylor6c9634c2012-07-28 11:27:47 -050088 process_impact(git_log, args)
89
90
91if __name__ == '__main__':
92 main()