This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
trigger-hubbot
executable file
·186 lines (157 loc) · 6.32 KB
/
trigger-hubbot
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This file is part of Hubbot.
Copyright (C) 2015 Red Hat, Inc.
Hubbot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Hubbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hubbot. If not, see <http://www.gnu.org/licenses/>
"""
import subprocess
import json
import os
import sys
import ast
dry_run = False
new_style = False
config = ast.literal_eval(open("%s/.hubbotrc" % os.environ['HOME']).read())
github_token = config['github_token']
user_whitelist = config['user_whitelist']
master_repo = 'cockpit-project/cockpit'
if 'master_repo' in config:
master_repo = config['master_repo']
if 'dry_run' in config:
dry_run = config['dry_run']
os_arch_configurations = [{'os': 'fedora-22',
'short': 'f22',
'arch': 'x86_64',
'offset_master': 0,
'offset_pull': 0
},
{'os': 'fedora-rawhide',
'short': 'fraw',
'arch': 'x86_64',
'offset_master': 0,
'offset_pull': 0,
'noauto': True
},
{'os': 'rhel-7',
'short': 'r7',
'arch': 'x86_64',
'offset_master': -1,
'offset_pull': 2
}
]
default_arch_config = os_arch_configurations[0]
if 'os_arch' in config and config['os_arch']:
os_arch_configurations = config['os_arch']
if os_arch_configurations and len(os_arch_configurations) > 0:
default_arch_config = os_arch_configurations[0]
default_os = default_arch_config['os']
default_short = default_arch_config['short']
default_arch = default_arch_config['arch']
if 'default' in config:
(default_os, default_short, default_arch) = config['default']
def status_context_from_config(config):
if config is None:
config = default_arch_config
n_suffix = ""
if "HUBBOT_NEW_STYLE" in os.environ and os.environ["HUBBOT_NEW_STYLE"]:
n_suffix = "/new"
return "hubbot/%s/%s%s" % (config['short'], config['arch'].replace('_', '-'), n_suffix)
def trace(msg):
print msg
def github_get(url):
return json.loads(subprocess.check_output ([ "curl", "-s",
"-u", "%s:x-oauth-basic" % github_token,
"https://api.github.com/repos/%s/%s" % (master_repo, url),
]))
def set_github_status(repo, sha, status):
if dry_run:
print "Status %s: %s %s" % (sha, status['state'], status['description'])
else:
subprocess.check_output ([ "curl", "-s",
"-u", "%s:x-oauth-basic" % github_token,
"https://api.github.com/repos/%s/statuses/%s" % (repo, sha),
"-d", json.dumps(status)
])
def trigger_pull(pull, status_context):
data = github_get("pulls/%s" % pull)
sha = data['head']['sha']
set_github_status(master_repo, sha, { "state": "pending" if not new_style else "success",
"description": "Hubbot will get to this eventually",
"context": status_context })
def trigger_master(status_context):
master = github_get('branches/master')
sha = master['commit']['sha']
set_github_status(master_repo, sha, { "state": "pending" if not new_style else "success",
"description": "Hubbot will get to this eventually",
"context": status_context })
def consider_head(head, status_context):
status = github_get("commits/%s/status" % head['sha'])
for s in status['statuses']:
if s['context'] == status_context:
if s['state'] == 'failure':
return True
return False
def scan_pulls(status_context):
pulls = github_get("pulls")
master = github_get('branches/master')['commit']['sha']
for p in pulls:
if p['head']['user']['login'] not in user_whitelist:
trace("%s not in whitelist" % p['head']['user']['login'])
continue
if consider_head(p['head'], status_context):
trigger_pull(str(p['number']), status_context)
def is_context(context):
for config in os_arch_configurations:
if status_context_from_config(config) == context:
return True
return False
def verify_context(context):
if not is_context(context):
print "Unknown configuration: '%s'" % context
exit(1)
if "HUBBOT_NEW_STYLE" in os.environ and os.environ["HUBBOT_NEW_STYLE"]:
new_style = True
if dry_run:
print "hubbot dry run"
status_context = status_context_from_config(default_arch_config)
if len(sys.argv) == 4:
master_repo = sys.argv[1]
status_context = sys.argv[2]
verify_context(status_context)
pull = sys.argv[3]
elif len(sys.argv) == 3:
if is_context(sys.argv[1]):
status_context = sys.argv[1]
else:
master_repo = sys.argv[1]
pull = sys.argv[2]
elif len(sys.argv) == 2:
pull = sys.argv[1]
else:
print "Usage: trigger-hubbot [MASTER-REPO] [CONFIG] PULL"
print " PULL values:"
print " failed Restart all pull requests with status 'failure'"
print " master Check master"
print " NUMBER number of the pull request (e.g. '2000')"
print " CONFIG values:"
for config in os_arch_configurations:
print " %s" % status_context_from_config(config)
exit(1)
if not "/" in master_repo:
master_repo += "/cockpit"
if pull == "failed":
scan_pulls(status_context)
elif pull == "master":
trigger_master(status_context)
else:
trigger_pull(pull, status_context)