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 | |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 21 | import paramiko |
| 22 | import json |
| 23 | import logging |
Andrew Hutchings | 6a17893 | 2012-05-17 14:53:01 +0100 | [diff] [blame] | 24 | import argparse |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 25 | |
Andrew Hutchings | 6a17893 | 2012-05-17 14:53:01 +0100 | [diff] [blame] | 26 | parser = argparse.ArgumentParser() |
| 27 | parser.add_argument('user', help='The gerrit admin user') |
| 28 | parser.add_argument('ssh_key', help='The gerrit admin SSH key file') |
| 29 | options = parser.parse_args() |
| 30 | |
| 31 | GERRIT_USER = options.user |
| 32 | GERRIT_SSH_KEY = options.ssh_key |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 33 | |
| 34 | logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s', filename='/var/log/gerrit/expire_reviews.log') |
| 35 | logger= logging.getLogger('expire_reviews') |
| 36 | logger.setLevel(logging.INFO) |
| 37 | |
| 38 | logger.info('Starting expire reviews') |
| 39 | logger.info('Connecting to Gerrit') |
| 40 | |
| 41 | ssh = paramiko.SSHClient() |
| 42 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 43 | ssh.connect('localhost', username=GERRIT_USER, key_filename=GERRIT_SSH_KEY, port=29418) |
| 44 | |
| 45 | def expire_patch_set(patch_id, patch_subject, has_negative): |
| 46 | if has_negative: |
Andrew Hutchings | 4b73399 | 2012-05-14 17:27:50 +0100 | [diff] [blame] | 47 | message= 'code review expired after 1 week of no activity after a negative review, it can be restored using the \`Restore Change\` button under the Patch Set on the web interface' |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 48 | else: |
Andrew Hutchings | 4b73399 | 2012-05-14 17:27:50 +0100 | [diff] [blame] | 49 | message= 'code review expired after 2 weeks of no activity, it can be restored using the \`Restore Change\` button under the Patch Set on the web interface' |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 50 | command='gerrit review --abandon --message="{0}" {1}'.format(message, patch_id) |
| 51 | logger.info('Expiring: %s - %s: %s', patch_id, patch_subject, message) |
| 52 | stdin, stdout, stderr = ssh.exec_command(command) |
| 53 | if stdout.channel.recv_exit_status() != 0: |
| 54 | logger.error(stderr.read()) |
| 55 | |
| 56 | # Query all open with no activity for 2 weeks |
| 57 | logger.info('Searching no activity for 2 weeks') |
| 58 | stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --format JSON status:open age:2w') |
| 59 | |
| 60 | for line in stdout: |
| 61 | row= json.loads(line) |
| 62 | if not row.has_key('rowCount'): |
| 63 | expire_patch_set(row['currentPatchSet']['revision'], row['subject'], False) |
| 64 | |
| 65 | # Query all reviewed with no activity for 1 week |
| 66 | logger.info('Searching no activity on negative review for 1 week') |
| 67 | stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --all-approvals --format JSON status:reviewed age:1w') |
| 68 | |
| 69 | for line in stdout: |
| 70 | row= json.loads(line) |
| 71 | if not row.has_key('rowCount'): |
| 72 | # Search for negative approvals |
| 73 | for approval in row['currentPatchSet']['approvals']: |
| 74 | if approval['value'] == '-1': |
| 75 | expire_patch_set(row['currentPatchSet']['revision'], row['subject'], True) |
| 76 | break |
| 77 | |
| 78 | logger.info('End expire review') |