blob: f9e908a67f70a36effaf6e39680080613f0f7f0f [file] [log] [blame]
Monty Taylorf45f6ca2012-05-01 17:11:48 -04001#!/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 Taylorf45f6ca2012-05-01 17:11:48 -040021import paramiko
22import json
23import logging
Andrew Hutchings6a178932012-05-17 14:53:01 +010024import argparse
Monty Taylorf45f6ca2012-05-01 17:11:48 -040025
Andrew Hutchings6a178932012-05-17 14:53:01 +010026parser = argparse.ArgumentParser()
27parser.add_argument('user', help='The gerrit admin user')
28parser.add_argument('ssh_key', help='The gerrit admin SSH key file')
29options = parser.parse_args()
30
31GERRIT_USER = options.user
32GERRIT_SSH_KEY = options.ssh_key
Monty Taylorf45f6ca2012-05-01 17:11:48 -040033
34logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s', filename='/var/log/gerrit/expire_reviews.log')
35logger= logging.getLogger('expire_reviews')
36logger.setLevel(logging.INFO)
37
38logger.info('Starting expire reviews')
39logger.info('Connecting to Gerrit')
40
41ssh = paramiko.SSHClient()
42ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
43ssh.connect('localhost', username=GERRIT_USER, key_filename=GERRIT_SSH_KEY, port=29418)
44
45def expire_patch_set(patch_id, patch_subject, has_negative):
46 if has_negative:
Andrew Hutchings4b733992012-05-14 17:27:50 +010047 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 Taylorf45f6ca2012-05-01 17:11:48 -040048 else:
Andrew Hutchings4b733992012-05-14 17:27:50 +010049 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 Taylorf45f6ca2012-05-01 17:11:48 -040050 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
57logger.info('Searching no activity for 2 weeks')
58stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --format JSON status:open age:2w')
59
60for 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
66logger.info('Searching no activity on negative review for 1 week')
67stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --all-approvals --format JSON status:reviewed age:1w')
68
69for 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
78logger.info('End expire review')