-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
events.py
549 lines (459 loc) · 22.8 KB
/
events.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# Copyright (C) 2019-2023 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import calendar
import datetime
import json
import os
import re
import time
import twisted
from base64 import b64encode
from buildbot.process.results import SUCCESS, FAILURE, CANCELLED, WARNINGS, SKIPPED, EXCEPTION, RETRY
from buildbot.util import httpclientservice, service
from buildbot.www.hooks.github import GitHubEventHandler
from rapidfuzz import fuzz
from steps import GitHub
from twisted.internet import defer, reactor, task
from twisted.internet.defer import succeed
from twisted.python import log
from twisted_additions import TwistedAdditions
from utils import load_password
custom_suffix = '-uat' if load_password('BUILDBOT_UAT') else ''
class Events(service.BuildbotService):
EVENT_SERVER_ENDPOINT = 'https://ews.webkit{}.org/results/'.format(custom_suffix)
MAX_GITHUB_DESCRIPTION = 140
STEPS_TO_REPORT = [
'configuration', 'checkout-pull-request', 'apply-patch',
'compile-webkit', 'compile-webkit-without-change', 'compile-jsc', 'compile-jsc-without-change',
'layout-tests', 'layout-tests-repeat-failures', 're-run-layout-tests',
'run-layout-tests-without-change', 'layout-tests-repeat-failures-without-change',
'run-layout-tests-in-stress-mode', 'run-layout-tests-in-guard-malloc-stress-mode',
'run-api-tests', 'run-api-tests-without-change', 're-run-api-tests',
'jscore-test', 'jscore-test-without-change',
'add-reviewer-to-commit-message', 'commit-patch', 'push-commit-to-webkit-repo', 'canonicalize-commit',
'build-webkit-org-unit-tests', 'buildbot-check-config', 'buildbot-check-config-for-build-webkit', 'buildbot-check-config-for-ews',
'ews-unit-tests', 'resultsdbpy-unit-tests',
'upload-built-product', 'upload-test-results',
'apply-watch-list', 'bindings-tests', 'check-webkit-style',
'webkitperl-tests', 're-run-webkitperl-tests',
'webkitpy-tests-python2', 'webkitpy-tests-python3'
]
def __init__(self, master_hostname, type_prefix='', name='Events'):
"""
Initialize the Events Plugin. Sends data to event server on specific buildbot events.
:param type_prefix: [optional] prefix we want to add to the 'type' field on the json we send
to event server. (i.e. ews-build, where 'ews-' is the prefix.
:return: Events Object
"""
service.BuildbotService.__init__(self, name=name)
if type_prefix and not type_prefix.endswith("-"):
type_prefix += "-"
self.type_prefix = type_prefix
self.master_hostname = master_hostname
def sendDataToEWS(self, data):
if os.getenv('EWS_API_KEY', None):
data['EWS_API_KEY'] = os.getenv('EWS_API_KEY')
TwistedAdditions.request(
url=self.EVENT_SERVER_ENDPOINT,
type=b'POST',
headers={'Content-Type': ['application/json']},
json=data,
)
def sendDataToGitHub(self, repository, sha, data, user=None):
username, access_token = GitHub.credentials(user=user)
data['description'] = data.get('description', '')
if len(data['description']) > self.MAX_GITHUB_DESCRIPTION:
data['description'] = '{}...'.format(data['description'][:self.MAX_GITHUB_DESCRIPTION - 3])
auth_header = b64encode('{}:{}'.format(username, access_token).encode('utf-8')).decode('utf-8')
TwistedAdditions.request(
url=GitHub.commit_status_url(sha, repository),
type=b'POST',
headers={
'Authorization': ['Basic {}'.format(auth_header)],
'User-Agent': ['python-twisted/{}'.format(twisted.__version__)],
'Accept': ['application/vnd.github.v3+json'],
'Content-Type': ['application/json'],
}, json=data,
)
def getBuilderName(self, build):
if not (build and 'properties' in build):
return ''
return build.get('properties').get('buildername')[0]
def extractProperty(self, build, property_name):
if not (build and 'properties' in build and property_name in build['properties']):
return None
return build.get('properties').get(property_name)[0]
@defer.inlineCallbacks
def buildStarted(self, key, build):
if not build.get('properties'):
build['properties'] = yield self.master.db.builds.getBuildProperties(build.get('buildid'))
builder = yield self.master.db.builders.getBuilder(build.get('builderid'))
builder_display_name = builder.get('description')
data = {
"type": self.type_prefix + "build",
"status": "started",
"hostname": self.master_hostname,
"change_id": self.extractProperty(build, 'github.head.sha') or self.extractProperty(build, 'patch_id'),
"pr_id": self.extractProperty(build, 'github.number') or -1,
"pr_project": self.extractProperty(build, 'project') or '',
"build_id": build.get('buildid'),
"builder_id": build.get('builderid'),
"number": build.get('number'),
"result": build.get('results'),
"started_at": build.get('started_at'),
"complete_at": build.get('complete_at'),
"state_string": build.get('state_string'),
"builder_name": self.getBuilderName(build),
"builder_display_name": builder_display_name,
}
self.sendDataToEWS(data)
def buildFinishedGitHub(self, build):
sha = self.extractProperty(build, 'github.head.sha')
repository = self.extractProperty(build, 'repository')
if not sha or not repository:
print('Pull request number defined, but sha is {} and repository {}, which are invalid'.format(sha, repository))
print('Not reporting build result to GitHub')
return
data_to_send = dict(
owner=(self.extractProperty(build, 'owners') or [None])[0],
repo=(self.extractProperty(build, 'github.head.repo.full_name') or '').split('/')[-1],
sha=sha,
target_url='{}#/builders/{}/builds/{}'.format(self.master.config.buildbotURL, build.get('builderid'), build.get('number')),
state={
SUCCESS: 'success',
WARNINGS: 'success',
SKIPPED: 'success',
RETRY: 'pending',
FAILURE: 'failure'
}.get(build.get('results'), 'error'),
description=build.get('state_string'),
context=build['description'] + custom_suffix,
)
self.sendDataToGitHub(repository, sha, data_to_send, user=GitHub.user_for_queue(self.extractProperty(build, 'buildername')))
@defer.inlineCallbacks
def buildFinished(self, key, build):
if not build.get('properties'):
build['properties'] = yield self.master.db.builds.getBuildProperties(build.get('buildid'))
if not build.get('steps'):
build['steps'] = yield self.master.db.steps.getSteps(build.get('buildid'))
builder = yield self.master.db.builders.getBuilder(build.get('builderid'))
build['description'] = builder.get('description', '?')
if self.extractProperty(build, 'github.number') and ('uat' not in custom_suffix):
self.buildFinishedGitHub(build)
data = {
"type": self.type_prefix + "build",
"status": "finished",
"hostname": self.master_hostname,
"change_id": self.extractProperty(build, 'github.head.sha') or self.extractProperty(build, 'patch_id'),
"pr_id": self.extractProperty(build, 'github.number') or -1,
"pr_project": self.extractProperty(build, 'project') or '',
"build_id": build.get('buildid'),
"builder_id": build.get('builderid'),
"number": build.get('number'),
"result": build.get('results'),
"started_at": build.get('started_at'),
"complete_at": build.get('complete_at'),
"state_string": build.get('state_string'),
"builder_name": self.getBuilderName(build),
"builder_display_name": builder.get('description'),
"steps": build.get('steps'),
}
self.sendDataToEWS(data)
def stepStarted(self, key, step):
if step.get('name') not in self.STEPS_TO_REPORT:
return
state_string = step.get('state_string')
if state_string == 'pending':
state_string = 'Running {}'.format(step.get('name'))
data = {
"type": self.type_prefix + "step",
"status": "started",
"hostname": self.master_hostname,
"step_id": step.get('stepid'),
"build_id": step.get('buildid'),
"result": step.get('results'),
"state_string": state_string,
"started_at": step.get('started_at'),
"complete_at": step.get('complete_at'),
}
self.sendDataToEWS(data)
def stepFinished(self, key, step):
if step.get('name') not in self.STEPS_TO_REPORT:
return
data = {
"type": self.type_prefix + "step",
"status": "finished",
"hostname": self.master_hostname,
"step_id": step.get('stepid'),
"build_id": step.get('buildid'),
"result": step.get('results'),
"state_string": step.get('state_string'),
"started_at": step.get('started_at'),
"complete_at": step.get('complete_at'),
}
self.sendDataToEWS(data)
@defer.inlineCallbacks
def startService(self):
yield service.BuildbotService.startService(self)
startConsuming = self.master.mq.startConsuming
self._buildStartedConsumer = yield startConsuming(self.buildStarted, ('builds', None, 'new'))
self._buildCompleteConsumer = yield startConsuming(self.buildFinished, ('builds', None, 'finished'))
self._stepStartedConsumer = yield startConsuming(self.stepStarted, ('steps', None, 'started'))
self._stepFinishedConsumer = yield startConsuming(self.stepFinished, ('steps', None, 'finished'))
def stopService(self):
self._buildStartedConsumer.stopConsuming()
self._buildCompleteConsumer.stopConsuming()
self._stepStartedConsumer.stopConsuming()
self._stepFinishedConsumer.stopConsuming()
class logging_disabled(object):
def __init__(self):
self.saved_logger = None
def log_disabled(*args, **kwargs):
pass
def __enter__(self):
self.saved_logger = log.msg
log.msg = self.log_disabled
def __exit__(self, type, value, tb):
log.msg = self.saved_logger
# Based off of webkitscmpy's CommitClassifier, although modified to not raise exceptions
# when provided invalid commit classes.
class CommitClassifier(object):
class LineFilter(object):
DEFAULT_FUZZ_RATIO = 90
@classmethod
def fuzzy(cls, string, ratio=None):
ratio = cls.DEFAULT_FUZZ_RATIO if not ratio else ratio
return lambda x: fuzz.partial_ratio(string, x) >= ratio
def __init__(self, value):
self.description = value
if isinstance(value, str):
self.do = lambda x: re.search(value, x)
elif isinstance(value, dict) and 'value' in value:
self.description = 'fuzz({}, {}%)'.format(value['value'], value.get('ratio', self.DEFAULT_FUZZ_RATIO))
self.do = self.fuzzy(value['value'], ratio=value.get('ratio'))
else:
self.do = None
def __repr__(self):
return self.description
def __call__(self, string):
return bool(self.do(string)) if self.do else False
def __init__(self, name=None, pickable=True, headers=None, trailers=None, paths=None, **kwargs):
self.name = name or '?'
self.pickable = pickable
self.headers = [self.LineFilter(header) for header in headers or []]
self.trailers = [self.LineFilter(trailer) for trailer in trailers or []]
self.paths = [re.compile(r'^{}'.format(path)) for path in (paths or [])]
if not self.headers and not self.trailers and not self.paths:
log.msg(f'Commit class {self.name} matches all commits')
for argument, _ in kwargs.items():
log.msg(f'{argument} is not a valid member of CommitClassifier')
def matches(self, header, trailers, files):
if not self.headers and not self.trailers and not self.paths:
return False
matching_header = bool(self.headers and header)
matching_trailer = bool(self.trailers and trailers)
matches_header = self.headers and header and any([f(header) for f in self.headers])
matches_trailers = self.trailers and trailers and any([any([f(trailer) for f in self.trailers]) for trailer in trailers])
if (matching_header or matching_trailer) and not matches_header and not matches_trailers:
return False
if self.paths and not files:
return False
if self.paths and not all([
any([p.match(path) for p in self.paths]) for path in files
]):
return False
return True
class GitHubEventHandlerNoEdits(GitHubEventHandler):
OPEN_STATES = ('open',)
PUBLIC_REPOS = ('WebKit/WebKit',)
SENSATIVE_FIELDS = ('github.title',)
UNSAFE_MERGE_QUEUE_LABEL = 'unsafe-merge-queue'
MERGE_QUEUE_LABEL = 'merge-queue'
LABEL_PROCESS_DELAY = 10
ACCOUNTS_TO_IGNORE = ('webkit-early-warning-system', 'webkit-commit-queue')
TRAILER_RE = re.compile(r'^(?P<key>[^:()\t\/*]+): (?P<value>.+)')
_commit_classes = []
_last_commit_classes_refresh = 0
COMMIT_CLASSES_REFRESH = 60 * 60 * 24 # Refresh our commit-classes once a day
COMMIT_CLASSES_URL = f'https://raw.githubusercontent.com/{PUBLIC_REPOS[0]}/main/metadata/commit_classes.json'
@classmethod
def file_with_status_sign(cls, info):
if info.get('status') in ('removed', 'renamed'):
return '--- {}'.format(info['filename'])
return '+++ {}'.format(info['filename'])
@classmethod
@defer.inlineCallbacks
def commit_classes(cls):
current_time = time.time()
if cls._last_commit_classes_refresh + cls.COMMIT_CLASSES_REFRESH > time.time():
return defer.returnValue(cls._commit_classes)
try:
response = yield TwistedAdditions.request(
url=cls.COMMIT_CLASSES_URL,
type=b'GET',
timeout=60,
logger=log.msg,
)
if not response or response.status_code // 100 != 2:
log.msg(f'Failed to fetch metadata/commit_classes.json from network with status code {response.status_code}')
else:
cls._commit_classes = [CommitClassifier(**node) for node in response.json()]
cls._last_commit_classes_refresh = current_time
except Exception as e:
log.msg('Exception while fetching metadata/commit_classes.json from network')
log.msg(f' {e}')
return defer.returnValue(cls._commit_classes)
@classmethod
@defer.inlineCallbacks
def classifiy(cls, message, files):
classes = yield cls.commit_classes()
if not classes:
return defer.returnValue(None)
lines = message.splitlines()
header = lines[0]
trailers = []
for line in reversed(lines):
if not cls.TRAILER_RE.match(line):
break
trailers.append(line)
for klass in classes:
if klass.matches(header, trailers, files):
return defer.returnValue(klass.name)
return defer.returnValue(None)
def _get_payload(self, request):
# Disable excessive logging inside _get_payload()
with logging_disabled():
return super()._get_payload(request)
def _get_commit_msg(self, repo, sha):
# Used by buildbot to skip commits based on their commit message.
# even though we want to grab commit messages for each commit in the PR,
# we excplicitly _do not_ want to use this functionality, hence why we're
# disabling it.
return ''
@defer.inlineCallbacks
def _get_commit_messages(self, repo, head, count):
if not head or not count:
return defer.returnValue([])
username, access_token = GitHub.credentials()
auth_header = b64encode('{}:{}'.format(username, access_token).encode('utf-8')).decode('utf-8')
response = yield TwistedAdditions.request(
url="{}/repos/{}/commits".format(self.github_api_endpoint, repo),
type=b'GET',
timeout=60,
params=dict(
per_page=100,
sha=head,
), headers=dict(
Authorization=['Basic {}'.format(auth_header)],
Accept=['application/vnd.github.v3+json'],
), logger=log.msg,
)
if not response or response.status_code // 100 != 2:
return defer.returnValue([])
result = []
data = response.json()
for i in range(min(count, len(data))):
message = (data[i].get('commit') or {}).get('message')
if message:
result.append(message)
return defer.returnValue(result)
@defer.inlineCallbacks
def _get_pr_files(self, repo, number):
# Heavy modification of https://github.com/buildbot/buildbot/blob/v2.10.5/master/buildbot/www/hooks/github.py
PER_PAGE_LIMIT = 100 # GitHub will list a maximum of 100 files in a single response
NUM_PAGE_LIMIT = 30 # GitHub stops returning files in a PR after 3000 files
username, access_token = GitHub.credentials()
auth_header = b64encode('{}:{}'.format(username, access_token).encode('utf-8')).decode('utf-8')
page = 1
files = []
while page < NUM_PAGE_LIMIT:
response = yield TwistedAdditions.request(
url="{}/repos/{}/pulls/{}/files".format(self.github_api_endpoint, repo, number),
type=b'GET',
params=dict(
per_page=PER_PAGE_LIMIT,
page=page,
), headers=dict(
Authorization=['Basic {}'.format(auth_header)],
Accept=['application/vnd.github.v3+json'],
), logger=log.msg,
)
if not response or response.status_code // 100 != 2:
break
data = response.json()
files += [self.file_with_status_sign(f) for f in data]
if len(data) < PER_PAGE_LIMIT:
break
page += 1
if not files:
log.msg('Failed fetching files for PR #{}: response code {}'.format(number, response.status_code if response else '?'))
return defer.returnValue(files)
def extractProperties(self, payload):
result = super().extractProperties(payload)
if payload.get('base', {}).get('repo', {}).get('full_name') not in self.PUBLIC_REPOS:
for field in self.SENSATIVE_FIELDS:
if field in result:
del result[field]
return result
@defer.inlineCallbacks
def handle_pull_request(self, payload, event):
pr_number = payload['number']
action = payload.get('action')
state = payload.get('pull_request', {}).get('state')
labels = [label.get('name') for label in payload.get('pull_request', {}).get('labels', [])]
sender = payload.get('sender', {}).get('login', '')
head_sha = payload.get('pull_request', {}).get('head', {}).get('sha')
if state not in self.OPEN_STATES:
log.msg("PR #{} is '{}', which triggers nothing".format(pr_number, state))
return defer.returnValue(([], 'git'))
if sender in self.ACCOUNTS_TO_IGNORE:
log.msg(f"PR #{pr_number} ({head_sha}) was updated by '{sender}', ignoring it")
return defer.returnValue(([], 'git'))
log.msg(f'Handling PR #{pr_number} with hash: {head_sha}')
if action == 'labeled' and self.UNSAFE_MERGE_QUEUE_LABEL in labels:
log.msg(f'PR #{pr_number} ({head_sha}) was labeled for unsafe-merge-queue')
# 'labeled' is usually an ignored action, override it to force build
payload['action'] = 'synchronize'
yield task.deferLater(reactor, self.LABEL_PROCESS_DELAY, lambda: None)
event = 'unsafe_merge_queue'
elif action == 'labeled' and self.MERGE_QUEUE_LABEL in labels:
log.msg(f'PR #{pr_number} ({head_sha}) was labeled for merge-queue')
# 'labeled' is usually an ignored action, override it to force build
yield task.deferLater(reactor, self.LABEL_PROCESS_DELAY, lambda: None)
payload['action'] = 'synchronize'
event = 'merge_queue'
result = yield super().handle_pull_request(payload, event)
changes = result[0]
if not changes or 'properties' not in changes[0]:
return defer.returnValue(result)
change = changes[0]
repo_full_name = payload['repository']['full_name']
head_sha = payload['pull_request']['head']['sha']
number_commits = payload['pull_request']['commits']
messages = yield self._get_commit_messages(repo_full_name, head_sha, number_commits)
raw_files = [file.split(' ', 1)[-1] for file in change.get('files') or []]
classes = set()
for message in messages:
classification = yield self.classifiy(message, raw_files)
classes.add(classification or 'Unclassified')
change['properties'].update({'classification': list(classes)})
return defer.returnValue(([change], result[1]))