Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [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 script is designed to expire old code reviews that have not been touched |
| 17 | # using the following rules: |
| 18 | # 1. if open and no activity in 2 weeks, expire |
| 19 | # 2. if negative comment and no activity in 1 week, expire |
| 20 | |
| 21 | import os |
| 22 | import paramiko |
| 23 | import json |
| 24 | import logging |
| 25 | |
| 26 | GERRIT_USER = os.environ.get('GERRIT_USER', 'launchpadsync') |
| 27 | GERRIT_SSH_KEY = os.environ.get('GERRIT_SSH_KEY', |
| 28 | '/home/gerrit2/.ssh/launchpadsync_rsa') |
| 29 | |
| 30 | logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s', filename='/var/log/gerrit/expire_reviews.log') |
| 31 | logger= logging.getLogger('expire_reviews') |
| 32 | logger.setLevel(logging.INFO) |
| 33 | |
| 34 | logger.info('Starting expire reviews') |
| 35 | logger.info('Connecting to Gerrit') |
| 36 | |
| 37 | ssh = paramiko.SSHClient() |
| 38 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 39 | ssh.connect('localhost', username=GERRIT_USER, key_filename=GERRIT_SSH_KEY, port=29418) |
| 40 | |
| 41 | def expire_patch_set(patch_id, patch_subject, has_negative): |
| 42 | if has_negative: |
| 43 | message= 'code review expired after 1 week of no activity after a negative review' |
| 44 | else: |
| 45 | message= 'code review expired after 2 weeks of no activity' |
| 46 | command='gerrit review --abandon --message="{0}" {1}'.format(message, patch_id) |
| 47 | logger.info('Expiring: %s - %s: %s', patch_id, patch_subject, message) |
| 48 | stdin, stdout, stderr = ssh.exec_command(command) |
| 49 | if stdout.channel.recv_exit_status() != 0: |
| 50 | logger.error(stderr.read()) |
| 51 | |
| 52 | # Query all open with no activity for 2 weeks |
| 53 | logger.info('Searching no activity for 2 weeks') |
| 54 | stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --format JSON status:open age:2w') |
| 55 | |
| 56 | for line in stdout: |
| 57 | row= json.loads(line) |
| 58 | if not row.has_key('rowCount'): |
| 59 | expire_patch_set(row['currentPatchSet']['revision'], row['subject'], False) |
| 60 | |
| 61 | # Query all reviewed with no activity for 1 week |
| 62 | logger.info('Searching no activity on negative review for 1 week') |
| 63 | stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --all-approvals --format JSON status:reviewed age:1w') |
| 64 | |
| 65 | for line in stdout: |
| 66 | row= json.loads(line) |
| 67 | if not row.has_key('rowCount'): |
| 68 | # Search for negative approvals |
| 69 | for approval in row['currentPatchSet']['approvals']: |
| 70 | if approval['value'] == '-1': |
| 71 | expire_patch_set(row['currentPatchSet']['revision'], row['subject'], True) |
| 72 | break |
| 73 | |
| 74 | logger.info('End expire review') |