-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclose_pull_requests.py
executable file
·146 lines (108 loc) · 4.57 KB
/
close_pull_requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/env python
import argparse
import ConfigParser
import github
import yaml
import sys
import os
from urllib2 import urlopen, Request
import simplejson as json
import string
from optparse import OptionParser
usage = "usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="Turn on debugging output")
(options, args) = parser.parse_args()
config_file = '/home/admin/secret/github.oauth'
projects_yaml = '/tmp/repositories.yaml'
github_url = 'https://api.github.com/orgs/GNOME/repos?per_page=100'
excludes_file = '/home/admin/github.excludes'
github_organization = 'gnome'
repositories = []
if not os.path.isfile(excludes_file):
print 'No excludes file could be found at %s' % excludes_file
sys.exit(1)
else:
execfile(excludes_file)
GITHUB_SECURE_CONFIG = os.environ.get('GITHUB_SECURE_CONFIG',
'%s' % config_file)
secure_config = ConfigParser.ConfigParser()
secure_config.read(GITHUB_SECURE_CONFIG)
MESSAGE = """Thank you for contributing to %(project)s!
%(project)s uses Bugzilla for code review.
If you have never contributed to GNOME before make sure you have read the
getting started documentation:
http://www.gnome.org/get-involved
Otherwise please visit
https://wiki.gnome.org/Newcomers
and follow the instructions there to upload your change to Bugzilla.
"""
TEMPLATE = string.Template("""
- project: $project_name
options:
- $has_pull_requests
""")
def fetch_repositories(url):
url = str(url)
if not os.path.isfile(config_file):
print 'No configuration file could be found at %s' % config_file
sys.exit(1)
if secure_config.has_option("github", "oauth_token"):
auth_token = secure_config.get("github", "oauth_token")
else:
print 'Make sure %s has a github section and an oauth_token key/value pair' % config_file
sys.exit(1)
req = Request(url)
req.add_header('Authorization', 'token %s' % auth_token)
response = urlopen(req)
content = response.read()
parsed_json = json.loads(content)
the_page = response.info().getheader('link')
next_url = the_page.split(';')[0].replace('<','').replace('>','')
is_last = the_page.split(';')[1].split(',')[0].replace('rel=','').replace('"','').replace(' ','')
for repository in parsed_json:
repo_name = repository['name']
if options.verbose:
print 'Appending %s to the repositories list' % repo_name
repositories.append(repo_name)
if is_last == 'next':
url = next_url
fetch_repositories(url)
with open('%s' % projects_yaml, 'w') as repo_list:
for repo in repositories:
repo = str(repo)
if repo in excludes:
has_pull_requests='has-no-pull-requests'
repo_list.write(TEMPLATE.substitute(project_name = repo, has_pull_requests=has_pull_requests))
else:
has_pull_requests='has-pull-requests'
repo_list.write(TEMPLATE.substitute(project_name = repo, has_pull_requests=has_pull_requests))
def close_pull_requests():
pull_request_text = MESSAGE
ghub = github.Github(secure_config.get("github", "oauth_token"))
org = ghub.get_organization(github_organization)
with open('%s' % projects_yaml, 'r') as yaml_file:
for section in yaml.load(yaml_file):
project = section['project']
if 'options' in section and section['options'][0] == 'has-no-pull-requests':
if options.verbose:
print 'EXCLUDES: Project %s has been excluded' % project
# Make sure we're supposed to close pull requests for this project
if 'options' in section and section['options'][0] == 'has-pull-requests':
repo = org.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(requester=req._requester,
headers={},
attributes=issue_data,
completed=True)
issue.create_comment(pull_request_text % vars)
req.edit(state="closed")
if __name__ == "__main__":
fetch_repositories(github_url)
close_pull_requests()