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 |
| 28 | # api_token = GITHUB_API_TOKEN |
| 29 | |
| 30 | import github2.client |
| 31 | import os |
| 32 | import StringIO |
| 33 | import ConfigParser |
| 34 | import logging |
| 35 | import re |
| 36 | |
| 37 | logging.basicConfig(level=logging.ERROR) |
| 38 | |
| 39 | GITHUB_CONFIG = os.environ.get('GITHUB_CONFIG', |
| 40 | '/home/gerrit2/github.config') |
| 41 | GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG', |
| 42 | '/home/gerrit2/github.secure.config') |
| 43 | |
| 44 | MESSAGE = """Thank you for contributing to OpenStack! |
| 45 | |
| 46 | %(project)s uses Gerrit for code review. |
| 47 | |
| 48 | Please visit http://wiki.openstack.org/GerritWorkflow and follow the instructions there to upload your change to Gerrit. |
| 49 | """ |
| 50 | |
| 51 | PROJECT_RE = re.compile(r'^project\s+"(.*)"$') |
| 52 | |
| 53 | secure_config = ConfigParser.ConfigParser() |
| 54 | secure_config.read(GITHUB_SECURE_CONFIG) |
| 55 | config = ConfigParser.ConfigParser() |
| 56 | config.read(GITHUB_CONFIG) |
| 57 | |
| 58 | github = 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 | |
| 62 | for 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) |