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: |
Jonathan Harker | 450b1a1 | 2015-01-06 15:38:19 -0500 | [diff] [blame] | 18 | # 1. if negative comment and no recent activity, expire |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 19 | |
Monty Taylor | 061919f | 2013-06-02 11:35:42 -0400 | [diff] [blame] | 20 | import argparse |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 21 | import json |
| 22 | import logging |
Monty Taylor | 061919f | 2013-06-02 11:35:42 -0400 | [diff] [blame] | 23 | import paramiko |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 24 | |
Monty Taylor | 96ca93b | 2020-03-24 13:00:16 -0500 | [diff] [blame^] | 25 | import jeepyb.log |
Steve Kowalik | 5c5a9e1 | 2015-08-14 13:56:53 +1000 | [diff] [blame] | 26 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 27 | logger = logging.getLogger('expire_reviews') |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 28 | |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 29 | |
Russell Bryant | f98a3a4 | 2013-06-25 15:45:52 -0400 | [diff] [blame] | 30 | def expire_patch_set(ssh, patch_id, patch_subject): |
Monty Taylor | 96ca93b | 2020-03-24 13:00:16 -0500 | [diff] [blame^] | 31 | message = ("Code review expired due to no recent activity" |
| 32 | " after a negative review. It can be restored using" |
| 33 | " the 'Restore Change' button under the Patch Set" |
| 34 | " on the web interface.") |
Jeremy Stanley | 4e5ca4c | 2012-12-21 17:18:34 +0000 | [diff] [blame] | 35 | command = ('gerrit review --abandon ' |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 36 | '--message="{message}" {patch_id}').format( |
| 37 | message=message, |
| 38 | patch_id=patch_id) |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 39 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 40 | logger.info('Expiring: %s - %s: %s', patch_id, patch_subject, message) |
| 41 | stdin, stdout, stderr = ssh.exec_command(command) |
| 42 | if stdout.channel.recv_exit_status() != 0: |
| 43 | logger.error(stderr.read()) |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 44 | |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 45 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 46 | def main(): |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 47 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 48 | parser = argparse.ArgumentParser() |
| 49 | parser.add_argument('user', help='The gerrit admin user') |
| 50 | parser.add_argument('ssh_key', help='The gerrit admin SSH key file') |
Jonathan Harker | 450b1a1 | 2015-01-06 15:38:19 -0500 | [diff] [blame] | 51 | parser.add_argument('--age', dest='age', default='1w', |
| 52 | help='The minimum age of a review to expire') |
Monty Taylor | 96ca93b | 2020-03-24 13:00:16 -0500 | [diff] [blame^] | 53 | jeepyb.log.setup_logging_arguments(parser) |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 54 | options = parser.parse_args() |
Monty Taylor | 96ca93b | 2020-03-24 13:00:16 -0500 | [diff] [blame^] | 55 | jeepyb.log.configure_logging(options) |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 56 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 57 | GERRIT_USER = options.user |
| 58 | GERRIT_SSH_KEY = options.ssh_key |
Jonathan Harker | 450b1a1 | 2015-01-06 15:38:19 -0500 | [diff] [blame] | 59 | EXPIRY_AGE = options.age |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 60 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 61 | logger.info('Starting expire reviews') |
| 62 | logger.info('Connecting to Gerrit') |
| 63 | |
| 64 | ssh = paramiko.SSHClient() |
| 65 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 66 | ssh.connect('localhost', username=GERRIT_USER, |
| 67 | key_filename=GERRIT_SSH_KEY, port=29418) |
| 68 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 69 | # Query all reviewed with no activity for 1 week |
| 70 | logger.info('Searching no activity on negative review for 1 week') |
| 71 | stdin, stdout, stderr = ssh.exec_command( |
| 72 | 'gerrit query --current-patch-set --all-approvals' |
Jonathan Harker | 450b1a1 | 2015-01-06 15:38:19 -0500 | [diff] [blame] | 73 | ' --format JSON status:reviewed age:' + EXPIRY_AGE) |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 74 | |
| 75 | for line in stdout: |
| 76 | row = json.loads(line) |
Ramy Asselin | c3a6219 | 2016-01-13 16:49:53 -0800 | [diff] [blame] | 77 | if 'rowCount' not in row and 'open' in row and row['open']: |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 78 | # Search for negative approvals |
| 79 | for approval in row['currentPatchSet']['approvals']: |
Jeremy Stanley | d8c51c6 | 2013-07-15 14:51:56 +0000 | [diff] [blame] | 80 | if approval['value'] in ('-1', '-2'): |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 81 | expire_patch_set(ssh, |
| 82 | row['currentPatchSet']['revision'], |
Russell Bryant | f98a3a4 | 2013-06-25 15:45:52 -0400 | [diff] [blame] | 83 | row['subject']) |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 84 | break |
| 85 | |
| 86 | logger.info('End expire review') |
Jeremy Stanley | 06efb06 | 2013-04-15 20:18:16 +0000 | [diff] [blame] | 87 | |
Monty Taylor | 96ca93b | 2020-03-24 13:00:16 -0500 | [diff] [blame^] | 88 | |
Jeremy Stanley | 06efb06 | 2013-04-15 20:18:16 +0000 | [diff] [blame] | 89 | if __name__ == "__main__": |
| 90 | main() |