blob: 0355e90d480728885e94382a8756ff9434611784 [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
32import StringIO
33import ConfigParser
34import logging
35import re
36
37logging.basicConfig(level=logging.ERROR)
38
39GITHUB_CONFIG = os.environ.get('GITHUB_CONFIG',
40 '/home/gerrit2/github.config')
41GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG',
42 '/home/gerrit2/github.secure.config')
43
44MESSAGE = """Thank you for contributing to OpenStack!
45
46%(project)s uses Gerrit for code review.
47
48Please visit http://wiki.openstack.org/GerritWorkflow and follow the instructions there to upload your change to Gerrit.
49"""
50
51PROJECT_RE = re.compile(r'^project\s+"(.*)"$')
52
53secure_config = ConfigParser.ConfigParser()
54secure_config.read(GITHUB_SECURE_CONFIG)
55config = ConfigParser.ConfigParser()
56config.read(GITHUB_CONFIG)
57
58github = github2.client.Github(requests_per_second=1.0,
59 username=secure_config.get("github", "username"),
60 api_token=secure_config.get("github", "api_token"))
61
62for section in config.sections():
63 # Each section looks like [project "openstack/project"]
64 m = PROJECT_RE.match(section)
65 if not m: continue
66 project = m.group(1)
67
68 # Make sure we're supposed to close pull requests for this project:
69 if not (config.has_option(section, "close_pull") and
70 config.get(section, "close_pull").lower() == 'true'):
71 continue
72
73 # Close each pull request
74 pull_requests = github.pull_requests.list(project)
75 for req in pull_requests:
76 vars = dict(project=project)
77 github.issues.comment(project, req.number, MESSAGE%vars)
78 github.issues.close(project, req.number)