Monty Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 1 | #!/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 | |
| 20 | import argparse |
| 21 | import re |
| 22 | import subprocess |
| 23 | import smtplib |
| 24 | |
| 25 | from email.mime.text import MIMEText |
| 26 | |
| 27 | BASE_DIR = '/home/gerrit2/review_site' |
| 28 | EMAIL_TEMPLATE = """ |
| 29 | Hi, I'd like you to take a look at this patch for potential |
Clark Boylan | 5a22b19 | 2012-10-25 14:55:32 -0700 | [diff] [blame] | 30 | %s. |
Monty Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 31 | %s |
| 32 | |
| 33 | Log: |
| 34 | %s |
| 35 | """ |
Monty Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 36 | |
| 37 | def process_impact(git_log, args): |
Clark Boylan | 5a22b19 | 2012-10-25 14:55:32 -0700 | [diff] [blame] | 38 | """Notify mail list of impact""" |
| 39 | email_content = EMAIL_TEMPLATE % (args.impact, args.change_url, git_log) |
Monty Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 40 | msg = MIMEText(email_content) |
Clark Boylan | 5a22b19 | 2012-10-25 14:55:32 -0700 | [diff] [blame] | 41 | msg['Subject'] = '[%s] %s review request change %s' % \ |
| 42 | (args.project, args.impact, args.change) |
Monty Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 43 | msg['From'] = 'gerrit2@review.openstack.org' |
Clark Boylan | 5a22b19 | 2012-10-25 14:55:32 -0700 | [diff] [blame] | 44 | msg['To'] = args.dest_address |
Monty Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 45 | |
| 46 | s = smtplib.SMTP('localhost') |
Clark Boylan | 5a22b19 | 2012-10-25 14:55:32 -0700 | [diff] [blame] | 47 | s.sendmail('gerrit2@review.openstack.org', |
| 48 | args.dest_address, msg.as_string()) |
Monty Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 49 | s.quit() |
| 50 | |
Clark Boylan | 5a22b19 | 2012-10-25 14:55:32 -0700 | [diff] [blame] | 51 | def 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 Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 54 | |
| 55 | def 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 | |
| 63 | def 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 Boylan | 5a22b19 | 2012-10-25 14:55:32 -0700 | [diff] [blame] | 77 | # Not passed by gerrit: |
| 78 | parser.add_argument('--impact', default=None) |
| 79 | parser.add_argument('--dest-address', default=None) |
Monty Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 80 | |
| 81 | args = parser.parse_args() |
| 82 | |
| 83 | # Get git log |
| 84 | git_log = extract_git_log(args) |
| 85 | |
Clark Boylan | 5a22b19 | 2012-10-25 14:55:32 -0700 | [diff] [blame] | 86 | # Process impacts found in git log |
| 87 | if impacted(git_log, args.impact): |
Monty Taylor | 6c9634c | 2012-07-28 11:27:47 -0500 | [diff] [blame] | 88 | process_impact(git_log, args) |
| 89 | |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | main() |