From 911b620ab131dd17a44177aade027ce2103d7a43 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Thu, 22 Nov 2012 08:50:18 -0800 Subject: [PATCH] Reorg for gerritx split. --- close_pull_requests.py | 94 ++++++++++++++++++++++++++++++++++++++++++ run_mirror.py | 81 ++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100755 close_pull_requests.py create mode 100755 run_mirror.py diff --git a/close_pull_requests.py b/close_pull_requests.py new file mode 100755 index 0000000..cecbd32 --- /dev/null +++ b/close_pull_requests.py @@ -0,0 +1,94 @@ +#! /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. + +# Github pull requests closer reads a project config file called projects.yaml +# It should look like: + +# - homepage: http://openstack.org +# team-id: 153703 +# has-wiki: False +# has-issues: False +# has-downloads: False +# --- +# - project: PROJECT_NAME +# options: +# - has-pull-requests + +# Github authentication information is read from github.secure.config, +# which should look like: + +# [github] +# username = GITHUB_USERNAME +# password = GITHUB_PASSWORD +# +# or +# +# [github] +# oauth_token = GITHUB_OAUTH_TOKEN + +import ConfigParser +import github +import os +import yaml +import logging + +logging.basicConfig(level=logging.ERROR) + +PROJECTS_YAML = os.environ.get('PROJECTS_YAML', + '/home/gerrit2/projects.yaml') +GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG', + '/etc/github/github.secure.config') + +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) +(defaults, config) = [config for config in yaml.load_all(open(PROJECTS_YAML))] + +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")) + +orgs = ghub.get_user().get_orgs() +orgs_dict = dict(zip([o.login.lower() for o in orgs], orgs)) +for section in config: + project = section['project'] + + # Make sure we're supposed to close pull requests for this project: + if 'options' in section and 'has-pull-requests' in section['options']: + continue + + # 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) + + # Close each pull request + pull_requests = repo.get_pulls("open") + for req in pull_requests: + vars = dict(project=project) + issue_data = {"url": repo.url + "/issues/" + str(req.number)} + issue = github.Issue.Issue(req._requester, issue_data, completed = True) + issue.create_comment(MESSAGE % vars) + req.edit(state = "closed") diff --git a/run_mirror.py b/run_mirror.py new file mode 100755 index 0000000..b6b07e8 --- /dev/null +++ b/run_mirror.py @@ -0,0 +1,81 @@ +#! /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. + +# run_mirrors reads a project config file called projects.yaml +# It should look like: + +# - project: PROJECT_NAME + +import logging +import os +import subprocess +import shlex +import yaml + +def run_command(cmd, status=False, env={}): + cmd_list = shlex.split(str(cmd)) + newenv = os.environ + newenv.update(env) + p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, env=newenv) + (out, nothing) = p.communicate() + if status: + return (p.returncode, out.strip()) + return out.strip() + + +def run_command_status(cmd, env={}): + return run_command(cmd, True, env) + + +logging.basicConfig(level=logging.ERROR) + +PROJECTS_YAML = os.environ.get('PROJECTS_YAML', + '/etc/openstackci/projects.yaml') +PIP_TEMP_DOWNLOAD = os.environ.get('PIP_TEMP_DOWNLOAD', + '/var/lib/pip-download') +GIT_SOURCE = os.environ.get('GIT_SOURCE', 'https://github.com') +pip_command = '/usr/local/bin/pip install -M -U -I --exists-action=w ' \ + '--no-install %s' + +run_command(pip_command % "pip") + +(defaults, config) = [config for config in yaml.load_all(open(PROJECTS_YAML))] + +for section in config: + project = section['project'] + + os.chdir(PIP_TEMP_DOWNLOAD) + short_project = project.split('/')[1] + if not os.path.isdir(short_project): + run_command("git clone %s/%s.git %s" % (GIT_SOURCE, project, + short_project)) + os.chdir(short_project) + run_command("git fetch origin") + + for branch in run_command("git branch -a").split("\n"): + branch = branch.strip() + if (not branch.startswith("remotes/origin") + or "origin/HEAD" in branch): + continue + run_command("git reset --hard %s" % branch) + run_command("git clean -x -f -d -q") + print("*********************") + print("Fetching pip requires for %s:%s" % (project, branch)) + for requires_file in ("tools/pip-requires", "tools/test-requires"): + if os.path.exists(requires_file): + stanza = "-r %s" % requires_file + run_command(pip_command % stanza) +