blob: 984bc5571fbd6239b65e064afbe780a6a4a6a260 [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
Clark Boylancb3b4762012-06-13 10:10:30 -070028# password = GITHUB_PASSWORD
29#
30# or
31#
32# [github]
33# oauth_token = GITHUB_OAUTH_TOKEN
Monty Taylorf45f6ca2012-05-01 17:11:48 -040034
Clark Boylancb3b4762012-06-13 10:10:30 -070035import github
Monty Taylorf45f6ca2012-05-01 17:11:48 -040036import os
Monty Taylorf45f6ca2012-05-01 17:11:48 -040037import ConfigParser
38import logging
39import re
40
41logging.basicConfig(level=logging.ERROR)
42
43GITHUB_CONFIG = os.environ.get('GITHUB_CONFIG',
44 '/home/gerrit2/github.config')
45GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG',
46 '/home/gerrit2/github.secure.config')
47
48MESSAGE = """Thank you for contributing to OpenStack!
49
50%(project)s uses Gerrit for code review.
51
52Please visit http://wiki.openstack.org/GerritWorkflow and follow the instructions there to upload your change to Gerrit.
53"""
54
55PROJECT_RE = re.compile(r'^project\s+"(.*)"$')
56
57secure_config = ConfigParser.ConfigParser()
58secure_config.read(GITHUB_SECURE_CONFIG)
59config = ConfigParser.ConfigParser()
60config.read(GITHUB_CONFIG)
61
Clark Boylancb3b4762012-06-13 10:10:30 -070062if secure_config.has_option("github", "oauth_token"):
63 ghub = github.Github(secure_config.get("github", "oauth_token"))
64else:
65 ghub = github.Github(secure_config.get("github", "username"),
66 secure_config.get("github", "password"))
Monty Taylorf45f6ca2012-05-01 17:11:48 -040067
68for section in config.sections():
69 # Each section looks like [project "openstack/project"]
70 m = PROJECT_RE.match(section)
71 if not m: continue
72 project = m.group(1)
73
74 # Make sure we're supposed to close pull requests for this project:
75 if not (config.has_option(section, "close_pull") and
76 config.get(section, "close_pull").lower() == 'true'):
77 continue
78
79 # Close each pull request
Clark Boylancb3b4762012-06-13 10:10:30 -070080 repo = ghub.get_user().get_repo(project)
81 pull_requests = repo.get_pulls("open")
Monty Taylorf45f6ca2012-05-01 17:11:48 -040082 for req in pull_requests:
83 vars = dict(project=project)
Clark Boylancb3b4762012-06-13 10:10:30 -070084 issue = repo.get_issue(req.number)
85 issue.create_comment(MESSAGE % vars)
86 req.edit(state = "closed")