Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 1 | #! /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 Boylan | cb3b476 | 2012-06-13 10:10:30 -0700 | [diff] [blame] | 28 | # password = GITHUB_PASSWORD |
| 29 | # |
| 30 | # or |
| 31 | # |
| 32 | # [github] |
| 33 | # oauth_token = GITHUB_OAUTH_TOKEN |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 34 | |
Clark Boylan | cb3b476 | 2012-06-13 10:10:30 -0700 | [diff] [blame] | 35 | import github |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 36 | import os |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 37 | import ConfigParser |
| 38 | import logging |
| 39 | import re |
| 40 | |
| 41 | logging.basicConfig(level=logging.ERROR) |
| 42 | |
| 43 | GITHUB_CONFIG = os.environ.get('GITHUB_CONFIG', |
| 44 | '/home/gerrit2/github.config') |
| 45 | GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG', |
| 46 | '/home/gerrit2/github.secure.config') |
| 47 | |
| 48 | MESSAGE = """Thank you for contributing to OpenStack! |
| 49 | |
| 50 | %(project)s uses Gerrit for code review. |
| 51 | |
| 52 | Please visit http://wiki.openstack.org/GerritWorkflow and follow the instructions there to upload your change to Gerrit. |
| 53 | """ |
| 54 | |
| 55 | PROJECT_RE = re.compile(r'^project\s+"(.*)"$') |
| 56 | |
| 57 | secure_config = ConfigParser.ConfigParser() |
| 58 | secure_config.read(GITHUB_SECURE_CONFIG) |
| 59 | config = ConfigParser.ConfigParser() |
| 60 | config.read(GITHUB_CONFIG) |
| 61 | |
Clark Boylan | cb3b476 | 2012-06-13 10:10:30 -0700 | [diff] [blame] | 62 | if secure_config.has_option("github", "oauth_token"): |
| 63 | ghub = github.Github(secure_config.get("github", "oauth_token")) |
| 64 | else: |
| 65 | ghub = github.Github(secure_config.get("github", "username"), |
| 66 | secure_config.get("github", "password")) |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 67 | |
Clark Boylan | 4d5e654 | 2012-06-13 12:00:10 -0700 | [diff] [blame] | 68 | orgs = ghub.get_user().get_orgs() |
Clark Boylan | 99999ff | 2012-06-13 14:48:28 -0700 | [diff] [blame] | 69 | orgs_dict = dict(zip([o.login.lower() for o in orgs], orgs)) |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 70 | for 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 Boylan | 4d5e654 | 2012-06-13 12:00:10 -0700 | [diff] [blame] | 81 | # 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 Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 88 | # Close each pull request |
Clark Boylan | cb3b476 | 2012-06-13 10:10:30 -0700 | [diff] [blame] | 89 | pull_requests = repo.get_pulls("open") |
Monty Taylor | f45f6ca | 2012-05-01 17:11:48 -0400 | [diff] [blame] | 90 | for req in pull_requests: |
| 91 | vars = dict(project=project) |
Clark Boylan | 692ca39 | 2012-06-13 16:58:40 -0700 | [diff] [blame] | 92 | issue_data = {"url": repo.url + "/issues/" + str(req.number)} |
| 93 | issue = github.Issue.Issue(req._requester, issue_data, completed = True) |
Clark Boylan | cb3b476 | 2012-06-13 10:10:30 -0700 | [diff] [blame] | 94 | issue.create_comment(MESSAGE % vars) |
| 95 | req.edit(state = "closed") |