2012-05-01 17:11:48 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# Copyright (C) 2011 OpenStack, LLC.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2012-07-29 12:54:27 -05:00
|
|
|
# Github pull requests closer reads a project config file called projects.yaml
|
|
|
|
# It should look like:
|
2012-05-01 17:11:48 -04:00
|
|
|
|
2012-11-04 22:20:36 +01:00
|
|
|
# - homepage: http://openstack.org
|
|
|
|
# team-id: 153703
|
|
|
|
# has-wiki: False
|
|
|
|
# has-issues: False
|
|
|
|
# has-downloads: False
|
|
|
|
# ---
|
2012-07-29 12:54:27 -05:00
|
|
|
# - project: PROJECT_NAME
|
|
|
|
# options:
|
2012-11-08 11:51:01 +01:00
|
|
|
# - has-pull-requests
|
2012-05-01 17:11:48 -04:00
|
|
|
|
|
|
|
# Github authentication information is read from github.secure.config,
|
|
|
|
# which should look like:
|
|
|
|
|
|
|
|
# [github]
|
|
|
|
# username = GITHUB_USERNAME
|
2012-06-13 10:10:30 -07:00
|
|
|
# password = GITHUB_PASSWORD
|
|
|
|
#
|
|
|
|
# or
|
|
|
|
#
|
|
|
|
# [github]
|
|
|
|
# oauth_token = GITHUB_OAUTH_TOKEN
|
2012-05-01 17:11:48 -04:00
|
|
|
|
2012-07-29 12:54:27 -05:00
|
|
|
import ConfigParser
|
2012-06-13 10:10:30 -07:00
|
|
|
import github
|
2012-05-01 17:11:48 -04:00
|
|
|
import os
|
2012-07-29 12:54:27 -05:00
|
|
|
import yaml
|
2012-05-01 17:11:48 -04:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.ERROR)
|
|
|
|
|
2012-07-29 12:54:27 -05:00
|
|
|
PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
|
|
|
|
'/home/gerrit2/projects.yaml')
|
2012-05-01 17:11:48 -04:00
|
|
|
GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG',
|
2012-10-31 10:24:07 +01:00
|
|
|
'/etc/github/github.secure.config')
|
2012-05-01 17:11:48 -04:00
|
|
|
|
|
|
|
MESSAGE = """Thank you for contributing to OpenStack!
|
|
|
|
|
|
|
|
%(project)s uses Gerrit for code review.
|
|
|
|
|
|
|
|
Please visit http://wiki.openstack.org/GerritWorkflow and follow the instructions there to upload your change to Gerrit.
|
|
|
|
"""
|
|
|
|
|
|
|
|
secure_config = ConfigParser.ConfigParser()
|
|
|
|
secure_config.read(GITHUB_SECURE_CONFIG)
|
2012-11-04 22:20:36 +01:00
|
|
|
(defaults, config) = [config for config in yaml.load_all(open(PROJECTS_YAML))]
|
2012-05-01 17:11:48 -04:00
|
|
|
|
2012-06-13 10:10:30 -07:00
|
|
|
if secure_config.has_option("github", "oauth_token"):
|
|
|
|
ghub = github.Github(secure_config.get("github", "oauth_token"))
|
|
|
|
else:
|
|
|
|
ghub = github.Github(secure_config.get("github", "username"),
|
|
|
|
secure_config.get("github", "password"))
|
2012-05-01 17:11:48 -04:00
|
|
|
|
2012-06-13 12:00:10 -07:00
|
|
|
orgs = ghub.get_user().get_orgs()
|
2012-06-13 14:48:28 -07:00
|
|
|
orgs_dict = dict(zip([o.login.lower() for o in orgs], orgs))
|
2012-07-29 12:54:27 -05:00
|
|
|
for section in config:
|
|
|
|
project = section['project']
|
2012-05-01 17:11:48 -04:00
|
|
|
|
|
|
|
# Make sure we're supposed to close pull requests for this project:
|
2012-11-08 11:51:01 +01:00
|
|
|
if 'options' in section and 'has-pull-requests' in section['options']:
|
2012-05-01 17:11:48 -04:00
|
|
|
continue
|
|
|
|
|
2012-06-13 12:00:10 -07:00
|
|
|
# Find the project's repo
|
|
|
|
project_split = project.split('/', 1)
|
|
|
|
if len(project_split) > 1:
|
|
|
|
repo = orgs_dict[project_split[0].lower()].get_repo(project_split[1])
|
|
|
|
else:
|
|
|
|
repo = ghub.get_user().get_repo(project)
|
|
|
|
|
2012-05-01 17:11:48 -04:00
|
|
|
# Close each pull request
|
2012-06-13 10:10:30 -07:00
|
|
|
pull_requests = repo.get_pulls("open")
|
2012-05-01 17:11:48 -04:00
|
|
|
for req in pull_requests:
|
|
|
|
vars = dict(project=project)
|
2012-06-13 16:58:40 -07:00
|
|
|
issue_data = {"url": repo.url + "/issues/" + str(req.number)}
|
|
|
|
issue = github.Issue.Issue(req._requester, issue_data, completed = True)
|
2012-06-13 10:10:30 -07:00
|
|
|
issue.create_comment(MESSAGE % vars)
|
|
|
|
req.edit(state = "closed")
|