blob: 64160ed893ebc2283cb0cc8bfd32e3db1f8d3e6b [file] [log] [blame]
Monty Taylorf45f6ca2012-05-01 17:11:48 -04001#! /usr/bin/env python
2# Copyright (C) 2011 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# Close Github pull requests with instructions to use Gerrit for
17# code review. The list of projects is found in github.config
18# and should look like:
19
20# [project "GITHUB_PROJECT"]
21# close_pull = true
22
23# Github authentication information is read from github.secure.config,
24# which should look like:
25
26# [github]
27# username = GITHUB_USERNAME
28# api_token = GITHUB_API_TOKEN
29
30import github2.client
31import os
Monty Taylorf45f6ca2012-05-01 17:11:48 -040032import ConfigParser
33import logging
34import re
35
36logging.basicConfig(level=logging.ERROR)
37
38GITHUB_CONFIG = os.environ.get('GITHUB_CONFIG',
39 '/home/gerrit2/github.config')
40GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG',
41 '/home/gerrit2/github.secure.config')
42
43MESSAGE = """Thank you for contributing to OpenStack!
44
45%(project)s uses Gerrit for code review.
46
47Please visit http://wiki.openstack.org/GerritWorkflow and follow the instructions there to upload your change to Gerrit.
48"""
49
50PROJECT_RE = re.compile(r'^project\s+"(.*)"$')
51
52secure_config = ConfigParser.ConfigParser()
53secure_config.read(GITHUB_SECURE_CONFIG)
54config = ConfigParser.ConfigParser()
55config.read(GITHUB_CONFIG)
56
57github = github2.client.Github(requests_per_second=1.0,
58 username=secure_config.get("github", "username"),
59 api_token=secure_config.get("github", "api_token"))
60
61for section in config.sections():
62 # Each section looks like [project "openstack/project"]
63 m = PROJECT_RE.match(section)
64 if not m: continue
65 project = m.group(1)
66
67 # Make sure we're supposed to close pull requests for this project:
68 if not (config.has_option(section, "close_pull") and
69 config.get(section, "close_pull").lower() == 'true'):
70 continue
71
72 # Close each pull request
73 pull_requests = github.pull_requests.list(project)
74 for req in pull_requests:
75 vars = dict(project=project)
76 github.issues.comment(project, req.number, MESSAGE%vars)
77 github.issues.close(project, req.number)