Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [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 | # Github pull requests closer reads a project config file called projects.yaml |
| 17 | # It should look like: |
| 18 | |
| 19 | # - homepage: http://openstack.org |
| 20 | # team-id: 153703 |
| 21 | # has-wiki: False |
| 22 | # has-issues: False |
| 23 | # has-downloads: False |
| 24 | # --- |
| 25 | # - project: PROJECT_NAME |
| 26 | # options: |
| 27 | # - has-pull-requests |
| 28 | |
| 29 | # Github authentication information is read from github.secure.config, |
| 30 | # which should look like: |
| 31 | |
| 32 | # [github] |
| 33 | # username = GITHUB_USERNAME |
| 34 | # password = GITHUB_PASSWORD |
| 35 | # |
| 36 | # or |
| 37 | # |
| 38 | # [github] |
| 39 | # oauth_token = GITHUB_OAUTH_TOKEN |
| 40 | |
| 41 | import ConfigParser |
| 42 | import github |
| 43 | import os |
| 44 | import yaml |
| 45 | import logging |
| 46 | |
Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [diff] [blame] | 47 | MESSAGE = """Thank you for contributing to OpenStack! |
| 48 | |
| 49 | %(project)s uses Gerrit for code review. |
| 50 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 51 | Please visit http://wiki.openstack.org/GerritWorkflow and follow the |
| 52 | instructions there to upload your change to Gerrit. |
Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [diff] [blame] | 53 | """ |
| 54 | |
Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [diff] [blame] | 55 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 56 | def main(): |
Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [diff] [blame] | 57 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 58 | logging.basicConfig(level=logging.ERROR) |
Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [diff] [blame] | 59 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 60 | PROJECTS_YAML = os.environ.get('PROJECTS_YAML', |
| 61 | '/home/gerrit2/projects.yaml') |
| 62 | GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG', |
| 63 | '/etc/github/github.secure.config') |
Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [diff] [blame] | 64 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 65 | secure_config = ConfigParser.ConfigParser() |
| 66 | secure_config.read(GITHUB_SECURE_CONFIG) |
| 67 | (defaults, config) = [config for config in |
| 68 | yaml.load_all(open(PROJECTS_YAML))] |
| 69 | |
| 70 | if secure_config.has_option("github", "oauth_token"): |
| 71 | ghub = github.Github(secure_config.get("github", "oauth_token")) |
Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [diff] [blame] | 72 | else: |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 73 | ghub = github.Github(secure_config.get("github", "username"), |
| 74 | secure_config.get("github", "password")) |
Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [diff] [blame] | 75 | |
Monty Taylor | da3bada | 2012-11-22 09:38:22 -0800 | [diff] [blame] | 76 | orgs = ghub.get_user().get_orgs() |
| 77 | orgs_dict = dict(zip([o.login.lower() for o in orgs], orgs)) |
| 78 | for section in config: |
| 79 | project = section['project'] |
| 80 | |
| 81 | # Make sure we're supposed to close pull requests for this project: |
| 82 | if 'options' in section and 'has-pull-requests' in section['options']: |
| 83 | continue |
| 84 | |
| 85 | # Find the project's repo |
| 86 | project_split = project.split('/', 1) |
| 87 | if len(project_split) > 1: |
| 88 | org = orgs_dict[project_split[0].lower()] |
| 89 | repo = org.get_repo(project_split[1]) |
| 90 | else: |
| 91 | repo = ghub.get_user().get_repo(project) |
| 92 | |
| 93 | # Close each pull request |
| 94 | pull_requests = repo.get_pulls("open") |
| 95 | for req in pull_requests: |
| 96 | vars = dict(project=project) |
| 97 | issue_data = {"url": repo.url + "/issues/" + str(req.number)} |
| 98 | issue = github.Issue.Issue(req._requester, |
| 99 | issue_data, |
| 100 | completed=True) |
| 101 | issue.create_comment(MESSAGE % vars) |
| 102 | req.edit(state="closed") |