Modified: trunk/Tools/CISupport/ews-build/events.py (288245 => 288246)
--- trunk/Tools/CISupport/ews-build/events.py 2022-01-19 23:13:41 UTC (rev 288245)
+++ trunk/Tools/CISupport/ews-build/events.py 2022-01-19 23:27:39 UTC (rev 288246)
@@ -1,4 +1,4 @@
-# Copyright (C) 2019 Apple Inc. All rights reserved.
+# Copyright (C) 2019, 2022 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
@@ -25,9 +25,13 @@
import json
import os
import time
+import twisted
+from base64 import b64encode
+from buildbot.process.results import SUCCESS, FAILURE, CANCELLED, WARNINGS, SKIPPED, EXCEPTION, RETRY
from buildbot.util import service
from buildbot.www.hooks.github import GitHubEventHandler
+from steps import GitHub
from twisted.internet import defer
from twisted.internet import reactor
from twisted.internet.defer import succeed
@@ -76,6 +80,7 @@
class Events(service.BuildbotService):
EVENT_SERVER_ENDPOINT = 'https://ews.webkit{}.org/results/'.format(custom_suffix).encode()
+ MAX_GITHUB_DESCRIPTION = 140
def __init__(self, master_hostname, type_prefix='', name='Events'):
"""
@@ -91,7 +96,7 @@
self.type_prefix = type_prefix
self.master_hostname = master_hostname
- def sendData(self, data):
+ def sendDataToEWS(self, data):
if os.getenv('EWS_API_KEY', None):
data['EWS_API_KEY'] = os.getenv('EWS_API_KEY')
agent = Agent(reactor)
@@ -99,6 +104,24 @@
agent.request(b'POST', self.EVENT_SERVER_ENDPOINT, Headers({'Content-Type': ['application/json']}), body)
+ def sendDataToGitHub(self, repository, sha, data):
+ username, access_token = GitHub.credentials()
+
+ 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')
+
+ agent = Agent(reactor)
+ body = JSONProducer(data)
+ d = agent.request(b'POST', GitHub.commit_status_url(sha, repository).encode('utf-8'), Headers({
+ 'Authorization': ['Basic {}'.format(auth_header)],
+ 'User-Agent': ['python-twisted/{}'.format(twisted.__version__)],
+ 'Accept': ['application/vnd.github.v3+json'],
+ 'Content-Type': ['application/json'],
+ }), body)
+
def getBuilderName(self, build):
if not (build and 'properties' in build):
return ''
@@ -105,11 +128,11 @@
return build.get('properties').get('buildername')[0]
- def getPatchID(self, build):
- if not (build and 'properties' in build and 'patch_id' in build['properties']):
+ 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('patch_id')[0]
+ return build.get('properties').get(property_name)[0]
@defer.inlineCallbacks
def buildStarted(self, key, build):
@@ -123,7 +146,7 @@
"type": self.type_prefix + "build",
"status": "started",
"hostname": self.master_hostname,
- "patch_id": self.getPatchID(build),
+ "patch_id": self.extractProperty(build, 'patch_id'),
"build_id": build.get('buildid'),
"builder_id": build.get('builderid'),
"number": build.get('number'),
@@ -135,9 +158,36 @@
"builder_display_name": builder_display_name,
}
- self.sendData(data)
+ self.sendDataToEWS(data)
@defer.inlineCallbacks
+ def buildFinishedGitHub(self, context, build):
+ sha = self.extractProperty(build, 'revision')
+ 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=context,
+ )
+ self.sendDataToGitHub(repository, sha, data_to_send)
+
+ @defer.inlineCallbacks
def buildFinished(self, key, build):
if not build.get('properties'):
build['properties'] = yield self.master.db.builds.getBuildProperties(build.get('buildid'))
@@ -147,11 +197,18 @@
builder = yield self.master.db.builders.getBuilder(build.get('builderid'))
builder_display_name = builder.get('description')
+ if self.extractProperty(build, 'github.number'):
+ return self.buildFinishedGitHub(builder_display_name, build)
+
+ patch_id = self.extractProperty(build, 'patch_id')
+ if not patch_id:
+ return
+
data = {
"type": self.type_prefix + "build",
"status": "finished",
"hostname": self.master_hostname,
- "patch_id": self.getPatchID(build),
+ "patch_id": self.extractProperty(build, 'patch_id'),
"build_id": build.get('buildid'),
"builder_id": build.get('builderid'),
"number": build.get('number'),
@@ -164,13 +221,49 @@
"steps": build.get('steps'),
}
- self.sendData(data)
+ self.sendDataToEWS(data)
+ @defer.inlineCallbacks
+ def stepStartedGitHub(self, build, state_string):
+ sha = self.extractProperty(build, 'revision')
+ 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 step started to GitHub')
+ return
+
+ builder = yield self.master.db.builders.getBuilder(build.get('builderid'))
+
+ 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: 'pending',
+ WARNINGS: 'pending',
+ FAILURE: 'failure',
+ EXCEPTION: 'error',
+ }.get(build.get('results'), 'pending'),
+ description=state_string,
+ context=builder.get('description'),
+ )
+ self.sendDataToGitHub(repository, sha, data_to_send)
+
+ @defer.inlineCallbacks
def stepStarted(self, key, step):
state_string = step.get('state_string')
if state_string == 'pending':
state_string = 'Running {}'.format(step.get('name'))
+ build = yield self.master.db.builds.getBuild(step.get('buildid'))
+ if not build.get('properties'):
+ build['properties'] = yield self.master.db.builds.getBuildProperties(step.get('buildid'))
+
+ # We need to force the defered properties to resolve
+ if build['properties'].get('github.number'):
+ self.stepStartedGitHub(build, state_string)
+
data = {
"type": self.type_prefix + "step",
"status": "started",
@@ -183,7 +276,7 @@
"complete_at": step.get('complete_at'),
}
- self.sendData(data)
+ self.sendDataToEWS(data)
def stepFinished(self, key, step):
data = {
@@ -198,7 +291,7 @@
"complete_at": step.get('complete_at'),
}
- self.sendData(data)
+ self.sendDataToEWS(data)
@defer.inlineCallbacks
def startService(self):