blob: 63b8352d9a1fe5dcae4403bbf9e8439a205e6e1c [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
25
26GERRIT_USER = os.environ.get('GERRIT_USER', 'launchpadsync')
27GERRIT_SSH_KEY = os.environ.get('GERRIT_SSH_KEY',
28 '/home/gerrit2/.ssh/launchpadsync_rsa')
29
30logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s', filename='/var/log/gerrit/expire_reviews.log')
31logger= logging.getLogger('expire_reviews')
32logger.setLevel(logging.INFO)
33
34logger.info('Starting expire reviews')
35logger.info('Connecting to Gerrit')
36
37ssh = paramiko.SSHClient()
38ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
39ssh.connect('localhost', username=GERRIT_USER, key_filename=GERRIT_SSH_KEY, port=29418)
40
41def 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
53logger.info('Searching no activity for 2 weeks')
54stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --format JSON status:open age:2w')
55
56for 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
62logger.info('Searching no activity on negative review for 1 week')
63stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --all-approvals --format JSON status:reviewed age:1w')
64
65for 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
74logger.info('End expire review')