blob: 1ba936e9d6eaf092bd7a5678e8bf622cdeec37de [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
21import os
22import paramiko
23import json
24import logging
Andrew Hutchings6a178932012-05-17 14:53:01 +010025import argparse
Monty Taylorf45f6ca2012-05-01 17:11:48 -040026
Andrew Hutchings6a178932012-05-17 14:53:01 +010027parser = argparse.ArgumentParser()
28parser.add_argument('user', help='The gerrit admin user')
29parser.add_argument('ssh_key', help='The gerrit admin SSH key file')
30options = parser.parse_args()
31
32GERRIT_USER = options.user
33GERRIT_SSH_KEY = options.ssh_key
Monty Taylorf45f6ca2012-05-01 17:11:48 -040034
35logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s', filename='/var/log/gerrit/expire_reviews.log')
36logger= logging.getLogger('expire_reviews')
37logger.setLevel(logging.INFO)
38
39logger.info('Starting expire reviews')
40logger.info('Connecting to Gerrit')
41
42ssh = paramiko.SSHClient()
43ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
44ssh.connect('localhost', username=GERRIT_USER, key_filename=GERRIT_SSH_KEY, port=29418)
45
46def expire_patch_set(patch_id, patch_subject, has_negative):
47 if has_negative:
Andrew Hutchings4b733992012-05-14 17:27:50 +010048 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 -040049 else:
Andrew Hutchings4b733992012-05-14 17:27:50 +010050 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 -040051 command='gerrit review --abandon --message="{0}" {1}'.format(message, patch_id)
52 logger.info('Expiring: %s - %s: %s', patch_id, patch_subject, message)
53 stdin, stdout, stderr = ssh.exec_command(command)
54 if stdout.channel.recv_exit_status() != 0:
55 logger.error(stderr.read())
56
57# Query all open with no activity for 2 weeks
58logger.info('Searching no activity for 2 weeks')
59stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --format JSON status:open age:2w')
60
61for line in stdout:
62 row= json.loads(line)
63 if not row.has_key('rowCount'):
64 expire_patch_set(row['currentPatchSet']['revision'], row['subject'], False)
65
66# Query all reviewed with no activity for 1 week
67logger.info('Searching no activity on negative review for 1 week')
68stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --all-approvals --format JSON status:reviewed age:1w')
69
70for line in stdout:
71 row= json.loads(line)
72 if not row.has_key('rowCount'):
73 # Search for negative approvals
74 for approval in row['currentPatchSet']['approvals']:
75 if approval['value'] == '-1':
76 expire_patch_set(row['currentPatchSet']['revision'], row['subject'], True)
77 break
78
79logger.info('End expire review')