Title: [294966] trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/github.py
Revision
294966
Author
ab...@igalia.com
Date
2022-05-27 16:40:43 -0700 (Fri, 27 May 2022)

Log Message

git-webkit pr: Show server response when updating an issue fails

Reviewed by Jonathan Bedard.

Small changes are also made to the request() method to make it more
reusable: now it can handle methods other than GET, and can print custom
error messages when requests fail.

Bare usages of python-requests have been refactored to use
self.request() where possible (that is, when the path being accessed is
within the repo URL).

* Tools/Scripts/libraries/webkitbugspy/webkitbugspy/github.py:

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

Modified Paths

Diff

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/github.py (294965 => 294966)


--- trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/github.py	2022-05-27 23:09:30 UTC (rev 294965)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/github.py	2022-05-27 23:40:43 UTC (rev 294966)
@@ -137,7 +137,7 @@
             save_in_keyring=save_in_keyring,
         )
 
-    def request(self, path=None, params=None, headers=None, authenticated=None, paginate=True):
+    def request(self, path=None, params=None, method='GET', headers=None, authenticated=None, paginate=True, json=None, error_message=None):
         headers = {key: value for key, value in headers.items()} if headers else dict()
         headers['Accept'] = headers.get('Accept', 'application/vnd.github.v3+json')
 
@@ -158,10 +158,12 @@
             name=self.name,
             path='{}'.format(path) if path else '',
         )
-        response = requests.get(url, params=params, headers=headers, auth=auth)
+        response = requests.request(method, url, params=params, headers=headers, auth=auth, json=json)
         if authenticated is None and not auth and response.status_code // 100 == 4:
-            return self.request(path=path, params=params, headers=headers, authenticated=True, paginate=paginate)
-        if response.status_code != 200:
+            return self.request(path=path, params=params, method=method, headers=headers, authenticated=True, paginate=paginate, json=json, error_message=error_message)
+        if response.status_code // 100 != 2:
+            if error_message:
+                sys.stderr.write("{}\n".format(error_message))
             sys.stderr.write("Request to '{}' returned status code '{}'\n".format(url, response.status_code))
             message = response.json().get('message')
             if message:
@@ -193,6 +195,9 @@
         )
         if response.status_code // 100 != 2:
             sys.stderr.write("Request to '{}' returned status code '{}'\n".format(url, response.status_code))
+            message = response.json().get('message')
+            if message:
+                sys.stderr.write('Message: {}\n'.format(message))
             sys.stderr.write(self.REFRESH_TOKEN_PROMPT)
             return None
 
@@ -368,19 +373,14 @@
             for label in labels:
                 if not self.labels.get(label):
                     raise ValueError("'{}' is not a label for '{}'".format(label, self.url))
-            response = requests.put(
-                '{api_url}/repos/{owner}/{name}/issues/{id}/labels'.format(
-                    api_url=self.api_url,
-                    owner=self.owner,
-                    name=self.name,
-                    id=issue.id,
-                ), auth=HTTPBasicAuth(*self.credentials(required=True)),
-                headers=dict(Accept='application/vnd.github.v3+json'),
+            response = self.request(
+                'issues/{id}/labels'.format(id=issue.id),
+                method='PUT',
+                authenticated=True,
                 json=dict(labels=labels),
+                error_message="Failed to modify '{}'".format(issue)
             )
-            if response.status_code // 100 != 2:
-                sys.stderr.write("Failed to modify '{}'\n".format(issue))
-                sys.stderr.write(self.REFRESH_TOKEN_PROMPT)
+            if not response:
                 if not update_dict:
                     return None
             elif project and component and version:
@@ -390,45 +390,33 @@
 
         if update_dict:
             update_dict['number'] = [issue.id]
-            response = requests.patch(
-                '{api_url}/repos/{owner}/{name}/issues/{id}'.format(
-                    api_url=self.api_url,
-                    owner=self.owner,
-                    name=self.name,
-                    id=issue.id,
-                ), auth=HTTPBasicAuth(*self.credentials(required=True)),
-                headers=dict(Accept='application/vnd.github.v3+json'),
+            response = self.request(
+                'issues/{id}'.format(id=issue.id),
+                method='PATCH',
+                authenticated=True,
                 json=update_dict,
+                error_message="Failed to modify '{}'".format(issue)
             )
-            if response.status_code // 100 != 2:
+            if not response:
                 if assignee:
                     issue._assignee = None
                 if opened is not None:
                     issue._opened = None
-                sys.stderr.write("Failed to modify '{}'\n".format(issue))
-                sys.stderr.write(self.REFRESH_TOKEN_PROMPT)
                 return None
 
         return self.add_comment(issue, why) if why else issue
 
     def add_comment(self, issue, text):
-        response = requests.post(
-            '{api_url}/repos/{owner}/{name}/issues/{id}/comments'.format(
-                api_url=self.api_url,
-                owner=self.owner,
-                name=self.name,
-                id=issue.id,
-            ),
-            auth=HTTPBasicAuth(*self.credentials(required=True)),
-            headers=dict(Accept='application/vnd.github.v3+json'),
+        data = ""
+            'issues/{id}/comments'.format(id=issue.id),
+            method='POST',
+            authenticated=True,
             json=dict(body=text),
+            error_message="Failed to add comment to '{}'".format(issue)
         )
-        if response.status_code // 100 != 2:
-            sys.stderr.write("Failed to add comment to '{}'\n".format(issue))
-            sys.stderr.write(self.REFRESH_TOKEN_PROMPT)
+        if not data:
             return None
 
-        data = ""
         tm = data.get('updated_at', data.get('created_at'))
         if tm:
             tm = int(calendar.timegm(datetime.strptime(tm, '%Y-%m-%dT%H:%M:%SZ').timetuple()))
@@ -540,17 +528,14 @@
         if assign:
             data['assignee'] = self.me().username
 
-        response = requests.post(
-            '{api_url}/repos/{owner}/{name}/issues'.format(
-                api_url=self.api_url,
-                owner=self.owner,
-                name=self.name,
-            ), auth=HTTPBasicAuth(*self.credentials(required=True)),
-            headers=dict(Accept='application/vnd.github.v3+json'),
+        response = self.request(
+            'issues',
+            method='POST',
+            authenticated=True,
             json=data,
+            error_message="Failed to create issue"
         )
-        if response.status_code // 100 != 2:
-            sys.stderr.write("Failed to create issue: '{}'\n".format(response.json().get('message', '?')))
+        if not response:
             return None
 
-        return self.issue(response.json()['number'])
+        return self.issue(response['number'])
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to