blob: 202d2eb7a7c28fff19631f936024405d99b9c3bc [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
Clark Boylan4d5e6542012-06-13 12:00:10 -070068orgs = ghub.get_user().get_orgs()
Clark Boylan99999ff2012-06-13 14:48:28 -070069orgs_dict = dict(zip([o.login.lower() for o in orgs], orgs))
Monty Taylorf45f6ca2012-05-01 17:11:48 -040070for section in config.sections():
71 # Each section looks like [project "openstack/project"]
72 m = PROJECT_RE.match(section)
73 if not m: continue
74 project = m.group(1)
75
76 # Make sure we're supposed to close pull requests for this project:
77 if not (config.has_option(section, "close_pull") and
78 config.get(section, "close_pull").lower() == 'true'):
79 continue
80
Clark Boylan4d5e6542012-06-13 12:00:10 -070081 # Find the project's repo
82 project_split = project.split('/', 1)
83 if len(project_split) > 1:
84 repo = orgs_dict[project_split[0].lower()].get_repo(project_split[1])
85 else:
86 repo = ghub.get_user().get_repo(project)
87
Monty Taylorf45f6ca2012-05-01 17:11:48 -040088 # Close each pull request
Clark Boylancb3b4762012-06-13 10:10:30 -070089 pull_requests = repo.get_pulls("open")
Monty Taylorf45f6ca2012-05-01 17:11:48 -040090 for req in pull_requests:
91 vars = dict(project=project)
Clark Boylan692ca392012-06-13 16:58:40 -070092 issue_data = {"url": repo.url + "/issues/" + str(req.number)}
93 issue = github.Issue.Issue(req._requester, issue_data, completed = True)
Clark Boylancb3b4762012-06-13 10:10:30 -070094 issue.create_comment(MESSAGE % vars)
95 req.edit(state = "closed")