blob: cecbd324ccc35262ff9919729812b313f190d5ec [file] [log] [blame]
Monty Taylor911b6202012-11-22 08:50:18 -08001#! /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
41import ConfigParser
42import github
43import os
44import yaml
45import logging
46
47logging.basicConfig(level=logging.ERROR)
48
49PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
50 '/home/gerrit2/projects.yaml')
51GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG',
52 '/etc/github/github.secure.config')
53
54MESSAGE = """Thank you for contributing to OpenStack!
55
56%(project)s uses Gerrit for code review.
57
58Please visit http://wiki.openstack.org/GerritWorkflow and follow the instructions there to upload your change to Gerrit.
59"""
60
61secure_config = ConfigParser.ConfigParser()
62secure_config.read(GITHUB_SECURE_CONFIG)
63(defaults, config) = [config for config in yaml.load_all(open(PROJECTS_YAML))]
64
65if secure_config.has_option("github", "oauth_token"):
66 ghub = github.Github(secure_config.get("github", "oauth_token"))
67else:
68 ghub = github.Github(secure_config.get("github", "username"),
69 secure_config.get("github", "password"))
70
71orgs = ghub.get_user().get_orgs()
72orgs_dict = dict(zip([o.login.lower() for o in orgs], orgs))
73for section in config:
74 project = section['project']
75
76 # Make sure we're supposed to close pull requests for this project:
77 if 'options' in section and 'has-pull-requests' in section['options']:
78 continue
79
80 # Find the project's repo
81 project_split = project.split('/', 1)
82 if len(project_split) > 1:
83 repo = orgs_dict[project_split[0].lower()].get_repo(project_split[1])
84 else:
85 repo = ghub.get_user().get_repo(project)
86
87 # Close each pull request
88 pull_requests = repo.get_pulls("open")
89 for req in pull_requests:
90 vars = dict(project=project)
91 issue_data = {"url": repo.url + "/issues/" + str(req.number)}
92 issue = github.Issue.Issue(req._requester, issue_data, completed = True)
93 issue.create_comment(MESSAGE % vars)
94 req.edit(state = "closed")