aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/run_tests
diff options
context:
space:
mode:
authorGravatar Lidi Zheng <lidiz@google.com>2018-12-13 16:33:02 -0800
committerGravatar Lidi Zheng <lidiz@google.com>2018-12-13 16:40:47 -0800
commit0f0822d53f37ea9da8bf838e5b3c62b05afe2f10 (patch)
treecf31a7b5d99db1f537760beeb0fd488265e13d03 /tools/run_tests
parent3c3436d2b4530580c2b95d897a3614fb1d430518 (diff)
WIP: Utilize the GitHub Check Feature
Diffstat (limited to 'tools/run_tests')
-rw-r--r--tools/run_tests/python_utils/check_on_pr.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/tools/run_tests/python_utils/check_on_pr.py b/tools/run_tests/python_utils/check_on_pr.py
new file mode 100644
index 0000000000..96418bf2d3
--- /dev/null
+++ b/tools/run_tests/python_utils/check_on_pr.py
@@ -0,0 +1,98 @@
+# Copyright 2018 The gRPC Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import print_function
+import os
+import json
+import time
+import datetime
+
+import requests
+import jwt
+
+_GITHUB_API_PREFIX = 'https://api.github.com'
+_GITHUB_REPO = 'lidizheng/grpc'
+_GITHUB_APP_ID = 22288
+_INSTALLATION_ID = 516307
+_GITHUB_APP_KEY = open(os.environ['HOME'] + '/.ssh/google-grpc-checker.2018-12-13.private-key.pem', 'r').read()
+
+_ACCESS_TOKEN_CACHE = None
+
+def _jwt_token():
+ return jwt.encode({
+ 'iat': int(time.time()),
+ 'exp': int(time.time() + 60 * 10), # expire in 10 minutes
+ 'iss': _GITHUB_APP_ID,
+ }, _GITHUB_APP_KEY, algorithm='RS256')
+
+def _access_token():
+ global _ACCESS_TOKEN_CACHE
+ if _ACCESS_TOKEN_CACHE == None or _ACCESS_TOKEN_CACHE['exp'] < time.time():
+ resp = requests.post(
+ url='https://api.github.com/app/installations/%s/access_tokens' % _INSTALLATION_ID,
+ headers={
+ 'Authorization': 'Bearer %s' % _jwt_token().decode('ASCII'),
+ 'Accept': 'application/vnd.github.machine-man-preview+json',
+ }
+ )
+ _ACCESS_TOKEN_CACHE = {'token': resp.json()['token'], 'exp': time.time()+60}
+ return _ACCESS_TOKEN_CACHE['token']
+
+def _call(url, method='GET', json=None):
+ if not url.startswith('https://'):
+ url = _GITHUB_API_PREFIX + url
+ headers={
+ 'Authorization': 'Bearer %s' % _access_token(),
+ 'Accept': 'application/vnd.github.antiope-preview+json',
+ }
+ return requests.request(
+ method=method,
+ url=url,
+ headers=headers,
+ json=json)
+
+def _latest_commit():
+ resp = _call('/repos/%s/pulls/%s/commits' % (_GITHUB_REPO, os.environ['ghprbPullId']))
+ return resp.json()[-1]
+
+def check_on_pr(name, summary, success=True):
+ """Create/Update a check on current pull request.
+
+ The check runs are aggregated by their name, so newer check will update the
+ older check with the same name.
+
+ Requires environment variable 'ghprbPullId' to indicate which pull request
+ should be updated.
+
+ Args:
+ name: The name of the check.
+ summary: A str in Markdown to be used as the detail information of the check.
+ success: A bool indicates whether the check is succeed or not.
+ """
+ if 'ghprbPullId' not in os.environ:
+ print('Missing ghprbPullId env var: not commenting')
+ return
+ commit = _latest_commit()
+ resp = _call('/repos/%s/check-runs' % _GITHUB_REPO, method='POST', json={
+ 'name': name,
+ 'head_sha': commit['sha'],
+ 'status': 'completed',
+ 'completed_at': '%sZ' % datetime.datetime.utcnow().replace(microsecond=0).isoformat(),
+ 'conclusion': 'success' if success else 'failure',
+ 'output': {
+ 'title': name,
+ 'summary': summary,
+ }
+ })
+ print('Result of Creating/Updating Check on PR:', json.dumps(resp.json(), indent=2))