-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtests-scan
executable file
·319 lines (270 loc) · 13.1 KB
/
tests-scan
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
# This file is part of Cockpit.
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
import argparse
import json
import logging
import sys
import time
from collections.abc import Collection, Iterable, Mapping, Sequence
from typing import Any
import pika
from lib import ALLOWLIST, testmap
from lib.aio.jsonutil import JsonObject, get_str
from lib.jobqueue import QueueEntry
from task import distributed_queue, github, labels_of_pull
sys.dont_write_bytecode = True
logging.basicConfig(level=logging.INFO)
Policy = Mapping[str, Sequence[str]]
def build_policy(repo: str, requested_contexts: Collection[str]) -> Policy:
policy = testmap.tests_for_project(repo)
if requested_contexts:
short_contexts = []
for c in requested_contexts:
short_contexts.append(c.split("@")[0])
new_policy = {}
for (branch, contexts) in policy.items():
branch_context = []
for c in short_contexts:
if c in contexts:
branch_context.append(c)
if branch_context:
new_policy[branch] = branch_context
policy = new_policy
return policy
def is_internal_context(context: str) -> bool:
for pattern in ["rhel"]:
if pattern in context:
return True
return False
def queue_test(entry: QueueEntry, dq: distributed_queue.DistributedQueue) -> None:
context = entry['job']['context']
queue = 'rhel' if is_internal_context(context) else 'public'
priority = distributed_queue.MAX_PRIORITY if '/devel' in context else distributed_queue.BASELINE_PRIORITY
properties = pika.BasicProperties(priority=priority)
dq.channel.basic_publish('', queue, json.dumps(entry), properties=properties)
logging.info("Published job: %s", json.dumps(entry["job"]))
def update_status(
api: github.GitHub, revision: str, last: JsonObject, dry: bool, changes: JsonObject
) -> bool:
if {**last, **changes} == last or dry:
return True
response: Any = api.post("statuses/" + revision, changes, accept=[422]) # 422 Unprocessable Entity
errors = response.get("errors", None)
if not errors:
return True
for error in response.get("errors", []):
sys.stderr.write(f"{revision}: {error.get('message', json.dumps(error))}\n")
sys.stderr.write(json.dumps(changes))
return False
def cockpit_tasks(api: github.GitHub, contexts: Sequence[str], opts: argparse.Namespace) -> Iterable[QueueEntry]:
pulls: Sequence[Any]
if opts.pull_data:
pulls = [json.loads(opts.pull_data)['pull_request']]
elif opts.pull_number:
pull = api.get(f"pulls/{opts.pull_number}")
if pull:
pulls = [pull]
else:
sys.exit(f"Can't find pull request {opts.pull_number}")
else:
pulls = api.pulls()
# Find matching PR for --sha
if opts.sha:
for pull in pulls:
if pull["head"]["sha"].startswith(opts.sha):
pulls = [pull]
break
else:
logging.info("Processing revision %s without pull request", opts.sha)
pulls = [{
"title": f"{opts.sha}",
"number": 0,
"head": {
"sha": opts.sha,
"user": {
"login": "cockpit-project"
}
},
"labels": [],
}]
for pull in pulls:
title = pull["title"]
number = pull["number"]
revision = pull["head"]["sha"]
statuses = api.statuses(revision)
login = pull["head"]["user"]["login"]
base = pull.get("base", {}).get("ref") # The branch this pull request targets (None for direct SHA triggers)
logging.info("Processing #%s titled '%s' on revision %s", number, title, revision)
labels = labels_of_pull(pull)
# Create list of statuses to process: always process the requested contexts, if given
todos: dict[str, JsonObject] = {context: {} for context in contexts}
for context in statuses: # Firstly add all valid contexts that already exist in github
if contexts and context not in contexts:
continue
if testmap.is_valid_context(context, api.repo):
todos[context] = statuses[context]
if not statuses and base: # If none already present in PR, add basic set of contexts
for context in build_policy(api.repo, contexts).get(base, []):
todos[context] = {}
# there are 3 different HEADs
# ref: the PR or SHA that we are testing
# base: the target branch of that PR (None for direct SHA trigger)
# branch: the branch of the external project that we are testing
# against this PR (only applies to cockpit-project/bots PRs)
for context, status in todos.items():
state = get_str(status, 'state', None)
description = get_str(status, 'description', None)
# This commit definitively succeeded or failed
if state in ["success", "failure"]:
logging.info("Skipping '%s' on #%s because it has already finished", context, number)
continue
# Ignore context when the PR has [no-test] in the title or as label, unless
# the context was directly triggered
if (('no-test' in labels or '[no-test]' in title) and description != github.NOT_TESTED_DIRECT):
logging.info("Skipping '%s' on #%s because it is no-test", context, number)
continue
if description and description.startswith(github.TESTING):
logging.info("Skipping '%s' on #%s because it is already running", context, number)
continue
# For unmarked and untested status, user must be allowed
# Not this only applies to this specific commit. A new status
# will apply if the user pushes a new commit.
if login not in ALLOWLIST and description in (None, github.NO_TESTING):
update_status(api, revision, status, opts.dry,
{"description": github.NO_TESTING, "context": context, "state": "pending"})
continue
if state != "pending":
# with --amqp (as called from run-queue), trigger tests as NOT_TESTED, as they already get queued;
# without --amqp (as called manually or from workflows), trigger tests as NOT_TESTED_DIRECT,
# so that the webhook queues them
changes = {
"context": context,
"state": "pending",
"description": github.NOT_TESTED if opts.amqp else github.NOT_TESTED_DIRECT
}
if not update_status(api, revision, status, opts.dry, changes):
continue
else:
logging.info("Not updating status for '%s' on #%s because it is pending", context, number)
# We're going to post an item to the queue now. Let's figure what
# what it should look like...
# Get correct project and branch. Ones from test name have priority
project = api.repo
branch = base
image_scenario, bots_pr, context_project, context_branch = testmap.split_context(context)
if context_project:
project = context_project
branch = context_branch or testmap.get_default_branch(project)
# Note: Don't use `pull/<pr_number>/head` as it may point to an old revision
ref = revision
checkout_ref = ref
if project != api.repo:
checkout_ref = testmap.get_default_branch(project)
if base != branch:
checkout_ref = branch
if api.repo == "cockpit-project/bots":
# bots own test doesn't need bots/ setup as there is a permanent symlink to itself there
# otherwise if we're testing an external project (repo != project) then checkout bots from the PR
bots_ref = None if api.repo == project else ref
else:
if bots_pr:
# Note: Don't use `pull/<pr_number>/head` as it may point to an old revision
bots_api = github.GitHub(repo="cockpit-project/bots")
bots_ref = bots_api.get_head(bots_pr) or "xxx" # Make sure we fail when cannot get the head
else:
bots_ref = "main"
slug_suffix = context.replace('/', '-').replace('@', '-')
slug = f"pull-{number}-{revision[:8]}-{time.strftime('%Y%m%d-%H%M%S')}-{slug_suffix}"
(image, _, scenario) = image_scenario.partition("/")
env = {
"TEST_OS": image,
"TEST_REVISION": revision,
}
if scenario:
env["TEST_SCENARIO"] = scenario
if bots_ref:
env["COCKPIT_BOTS_REF"] = bots_ref
if branch:
env["BASE_BRANCH"] = branch
if number:
env["TEST_PULL"] = f'{number}'
yield {
"job": {
"repo": api.repo,
"sha": revision,
"context": context,
"pull": number or None, # number uses 0 to mean 'None'
"report": None if number else {
"title": f"Tests failed on {revision}",
"labels": ["nightly"],
},
"command_subject": None if project == api.repo else {
"repo": project,
"branch": checkout_ref,
},
"slug": slug,
"env": env,
# for updating naughty trackers and downloading private images
"secrets": ["github-token", "image-download"],
},
"human": "{name:11} {context:25} {revision:10} {repo}{bots_ref}{branches}".format(
revision=revision[0:7],
context=image_scenario,
name=f'pull-{number}',
repo=(" (%s)" % project) if project else "",
bots_ref=f" [bots@{bots_ref}]" if bots_ref else "",
branches=f" {{{branch}}}" if branch else "",
)
}
def scan_for_pull_tasks(api: github.GitHub, contexts: Sequence[str], opts: argparse.Namespace) -> None:
results = cockpit_tasks(api, contexts, opts)
if opts.human_readable:
for entry in sorted(results, reverse=True, key=str):
print(entry['human'])
elif not opts.amqp:
for entry in results:
print(json.dumps(entry['job'], indent=4))
else:
with distributed_queue.DistributedQueue(opts.amqp, ['rhel', 'public']) as dq:
for entry in results:
queue_test(entry, dq=dq)
def main() -> None:
parser = argparse.ArgumentParser(description='Bot: scan and update status of pull requests on GitHub')
parser.add_argument('-v', '--human-readable', action="store_true", default=False,
help='Display human readable output rather than tasks')
parser.add_argument('-n', '--dry', action="store_true", default=False,
help="Don't actually change anything on GitHub")
parser.add_argument('--repo', default=None,
help='Repository to scan and checkout')
parser.add_argument('-c', '--context', action="append", default=[],
help='Test contexts to use.')
parser.add_argument('-p', '--pull-number', default=None,
help='Single pull request to scan for tasks')
parser.add_argument('--pull-data', default=None,
help='pull_request event GitHub JSON data to evaluate; mutualy exclusive with -p and -s')
parser.add_argument('-s', '--sha', default=None,
help='Trigger for a specific SHA; file issue on failure if SHA is not on a PR')
parser.add_argument('--amqp', default=None,
help='The host:port of the AMQP server to publish to (format host:port)')
opts = parser.parse_args()
if opts.pull_data and (opts.pull_number or opts.sha):
parser.error("--pull-data and --pull-number/--sha are mutually exclusive")
api = github.GitHub(repo=opts.repo)
scan_for_pull_tasks(api, opts.context, opts)
if __name__ == '__main__':
main()