Title: [288246] trunk/Tools
Revision
288246
Author
[email protected]
Date
2022-01-19 15:27:39 -0800 (Wed, 19 Jan 2022)

Log Message

[EWS] Report basic statuses to GitHub
https://bugs.webkit.org/show_bug.cgi?id=235210
<rdar://problem/87576183>

Reviewed by Aakash Jain.

* Tools/CISupport/ews-build/events.py:
(Events.sendDataToEWS): Renamed from sendData.
(Events.sendDataToGitHub): Post status for a commit to GitHub.
(Events.extractProperty): We need to extract more than patchID from
build properties.
(Events.buildFinishedGitHub): Report the pass/failed state of a build to GitHub.
(Events.buildFinished): Every time a build finishes, send data to GitHub.
(Events.stepStartedGitHub): Update GitHub with the current step being run,
and a failure status if that's already known.
(Events.stepStarted): Every time a new step starts, send data to GitHub.
(Events.stepFinished):
(Events.sendData): Deleted.
(Events.getPatchID): Deleted.
* Tools/CISupport/ews-build/steps.py:
(GitHub.commit_status_url): Construct a status url for a commit in a repository.

Canonical link: https://commits.webkit.org/246199@main

Modified Paths

Diff

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):

Modified: trunk/Tools/CISupport/ews-build/steps.py (288245 => 288246)


--- trunk/Tools/CISupport/ews-build/steps.py	2022-01-19 23:13:41 UTC (rev 288245)
+++ trunk/Tools/CISupport/ews-build/steps.py	2022-01-19 23:27:39 UTC (rev 288246)
@@ -101,6 +101,13 @@
         return 'https://api.{}/repos/{}'.format(host, path)
 
     @classmethod
+    def commit_status_url(cls, sha, repository_url=None):
+        api_url = cls.api_url(repository_url)
+        if not sha or not api_url:
+            return ''
+        return '{}/statuses/{}'.format(api_url, sha)
+
+    @classmethod
     def credentials(cls):
         try:
             passwords = json.load(open('passwords.json'))

Modified: trunk/Tools/ChangeLog (288245 => 288246)


--- trunk/Tools/ChangeLog	2022-01-19 23:13:41 UTC (rev 288245)
+++ trunk/Tools/ChangeLog	2022-01-19 23:27:39 UTC (rev 288246)
@@ -1,3 +1,27 @@
+2022-01-19  Jonathan Bedard  <[email protected]>
+
+        [EWS] Report basic statuses to GitHub
+        https://bugs.webkit.org/show_bug.cgi?id=235210
+        <rdar://problem/87576183>
+
+        Reviewed by Aakash Jain.
+
+        * CISupport/ews-build/events.py:
+        (Events.sendDataToEWS): Renamed from sendData.
+        (Events.sendDataToGitHub): Post status for a commit to GitHub.
+        (Events.extractProperty): We need to extract more than patchID from
+        build properties.
+        (Events.buildFinishedGitHub): Report the pass/failed state of a build to GitHub.
+        (Events.buildFinished): Every time a build finishes, send data to GitHub.
+        (Events.stepStartedGitHub): Update GitHub with the current step being run,
+        and a failure status if that's already known.
+        (Events.stepStarted): Every time a new step starts, send data to GitHub.
+        (Events.stepFinished):
+        (Events.sendData): Deleted.
+        (Events.getPatchID): Deleted.
+        * CISupport/ews-build/steps.py:
+        (GitHub.commit_status_url): Construct a status url for a commit in a repository.
+
 2022-01-10  Jonathan Bedard  <[email protected]>
 
         [git-webkit] Improve setup usability
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to