commit 644e41cfe5ffb9482653715ae7b956161ccf9fc2 Author: Monty Taylor Date: Mon Jul 23 10:21:29 2012 -0500 Split github into its own module. TODO: Add another script that sets the project description. Add the project description to the config hash. Change-Id: If4584b2a1e55e6eb912e1f557e31de216d49a516 diff --git a/files/scripts/close_pull_requests.py b/files/scripts/close_pull_requests.py new file mode 100755 index 0000000..202d2eb --- /dev/null +++ b/files/scripts/close_pull_requests.py @@ -0,0 +1,95 @@ +#! /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. + +# Close Github pull requests with instructions to use Gerrit for +# code review. The list of projects is found in github.config +# and should look like: + +# [project "GITHUB_PROJECT"] +# close_pull = true + +# 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 github +import os +import ConfigParser +import logging +import re + +logging.basicConfig(level=logging.ERROR) + +GITHUB_CONFIG = os.environ.get('GITHUB_CONFIG', + '/home/gerrit2/github.config') +GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG', + '/home/gerrit2/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. +""" + +PROJECT_RE = re.compile(r'^project\s+"(.*)"$') + +secure_config = ConfigParser.ConfigParser() +secure_config.read(GITHUB_SECURE_CONFIG) +config = ConfigParser.ConfigParser() +config.read(GITHUB_CONFIG) + +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.sections(): + # Each section looks like [project "openstack/project"] + m = PROJECT_RE.match(section) + if not m: continue + project = m.group(1) + + # Make sure we're supposed to close pull requests for this project: + if not (config.has_option(section, "close_pull") and + config.get(section, "close_pull").lower() == 'true'): + 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/manifests/init.pp b/manifests/init.pp new file mode 100644 index 0000000..b2fc5ce --- /dev/null +++ b/manifests/init.pp @@ -0,0 +1,87 @@ +class github ( + $username, + $oauth_token, + $projects = [] + ) { + + package { "python-dev": + ensure => present, + } + + package { "python-pip": + ensure => present, + require => Package[python-dev] + } + + package { "PyGithub": + ensure => latest, # okay to use latest for pip + provider => pip, + require => Package[python-pip] + } + + group { "github": + ensure => present + } + + user { "github": + ensure => present, + comment => "Github API User", + shell => "/bin/bash", + gid => "github", + require => Group["github"] + } + + file { '/etc/github': + owner => 'root', + group => 'root', + mode => 755, + ensure => 'directory', + } + + file { '/etc/github/github.config': + owner => 'root', + group => 'root', + mode => 444, + ensure => 'present', + content => template('github/github.config.erb'), + replace => 'true', + require => File['/etc/github'], + } + + file { '/etc/github/github.secure.config': + owner => 'root', + group => 'github', + mode => 440, + ensure => 'present', + content => template('gerrit/github.secure.config.erb'), + replace => 'true', + require => [Group['github'], File['/etc/github']], + } + + file { '/usr/local/github': + owner => 'root', + group => 'root', + mode => 755, + ensure => 'directory', + } + + file { '/usr/local/github/scripts': + owner => 'root', + group => 'root', + mode => 755, + ensure => 'directory', + recurse => true, + require => File['/usr/local/github'], + source => [ + "puppet:///modules/github/scripts", + ], + } + + cron { "githubclosepull": + user => github, + minute => "*/5", + command => 'sleep $((RANDOM\%60+90)) && python /usr/local/github/scripts/close_pull_requests.py', + require => File['/usr/local/github/scripts'], + } + +} diff --git a/templates/github.config.erb b/templates/github.config.erb new file mode 100644 index 0000000..2945ae9 --- /dev/null +++ b/templates/github.config.erb @@ -0,0 +1,7 @@ +# This file is managed by puppet. +# https://github.com/openstack/openstack-ci-puppet + +<% projects.each do |project| -%> +[project "<%= project['name'] %>"] +close_pull = <%= project['close_pull'] %> +<% end -%> diff --git a/templates/github.secure.config.erb b/templates/github.secure.config.erb new file mode 100644 index 0000000..92e1493 --- /dev/null +++ b/templates/github.secure.config.erb @@ -0,0 +1,3 @@ +[github] +username = <%= username %> +oauth_token = <%= oauth_token %>