blob: e7742f1e580b30d4fce0d26229aa51350d9a4e4d [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
Monty Taylor911b6202012-11-22 08:50:18 -080047MESSAGE = """Thank you for contributing to OpenStack!
48
49%(project)s uses Gerrit for code review.
50
Monty Taylorda3bada2012-11-22 09:38:22 -080051Please visit http://wiki.openstack.org/GerritWorkflow and follow the
52instructions there to upload your change to Gerrit.
Monty Taylor911b6202012-11-22 08:50:18 -080053"""
54
Monty Taylor911b6202012-11-22 08:50:18 -080055
Monty Taylorda3bada2012-11-22 09:38:22 -080056def main():
Monty Taylor911b6202012-11-22 08:50:18 -080057
Monty Taylorda3bada2012-11-22 09:38:22 -080058 logging.basicConfig(level=logging.ERROR)
Monty Taylor911b6202012-11-22 08:50:18 -080059
Monty Taylorda3bada2012-11-22 09:38:22 -080060 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 Taylor911b6202012-11-22 08:50:18 -080064
Monty Taylorda3bada2012-11-22 09:38:22 -080065 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 Taylor911b6202012-11-22 08:50:18 -080072 else:
Monty Taylorda3bada2012-11-22 09:38:22 -080073 ghub = github.Github(secure_config.get("github", "username"),
74 secure_config.get("github", "password"))
Monty Taylor911b6202012-11-22 08:50:18 -080075
Monty Taylorda3bada2012-11-22 09:38:22 -080076 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")