From 2b323a99f83530342a0ad0a0c30da24a79c06223 Mon Sep 17 00:00:00 2001 From: Andrew Hutchings Date: Mon, 9 Jan 2012 13:03:41 +0000 Subject: [PATCH] Adds a reviews expire cron script This script: 1. abandon all open reviews with no activity for 2 weeks 2. abandon all negative reviews with no activity for 1 week Fixes bug 907842 Change-Id: I2cccab46e0f6ffd8b1cc5e8227556f31dc963705 --- gerrit/expire_old_reviews.py | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 gerrit/expire_old_reviews.py diff --git a/gerrit/expire_old_reviews.py b/gerrit/expire_old_reviews.py new file mode 100644 index 00000000..461af711 --- /dev/null +++ b/gerrit/expire_old_reviews.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# Copyright (c) 2012 OpenStack, LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# This script is designed to expire old code reviews that have not been touched +# using the following rules: +# 1. if open and no activity in 2 weeks, expire +# 2. if negative comment and no activity in 1 week, expire + +import os +import paramiko +import json +import logging + +GERRIT_USER = os.environ.get('GERRIT_USER', 'launchpadsync') +GERRIT_SSH_KEY = os.environ.get('GERRIT_SSH_KEY', + '/home/gerrit2/.ssh/launchpadsync_rsa') + +logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s', filename='/var/log/gerrit/expire_reviews.log') +logger= logging.getLogger('expire_reviews') +logger.setLevel(logging.INFO) + +logger.info('Starting expire reviews') +logger.info('Connecting to Gerrit') + +ssh = paramiko.SSHClient() +ssh.load_system_host_keys(GERRIT_SSH_KEY) +ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) +ssh.connect('localhost', username=GERRIT_USER, port=29418) + +def expire_patch_set(patch_id, patch_subject, has_negative): + if has_negative: + message= 'code review expired after 1 week of no activity after a negative review' + else: + message= 'code review expired after 2 weeks of no activity' + command='gerrit review --abandon --message="{0}" {1}'.format(message, patch_id) + logger.info('Expiring: %s - %s: %s', patch_id, patch_subject, message) + stdin, stdout, stderr = ssh.exec_command(command) + if stdout.channel.recv_exit_status() != 0: + logger.error(stderr.read()) + +# Query all open with no activity for 2 weeks +logger.info('Searching no activity for 2 weeks') +stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --format JSON status:open age:2w') + +for line in stdout: + row= json.loads(line) + if not row.has_key('rowCount'): + expire_patch_set(row['currentPatchSet']['revision'], row['subject'], False) + +# Query all reviewed with no activity for 1 week +logger.info('Searching no activity on negative review for 1 week') +stdin, stdout, stderr = ssh.exec_command('gerrit query --current-patch-set --all-approvals --format JSON status:reviewed age:1w') + +for line in stdout: + row= json.loads(line) + if not row.has_key('rowCount'): + # Search for negative approvals + for approval in row['currentPatchSet']['approvals']: + if approval['value'] == '-1': + expire_patch_set(row['currentPatchSet']['revision'], row['subject'], True) + break + +logger.info('End expire review')