15 from __future__
import print_function
27 _GITHUB_API_PREFIX =
'https://api.github.com'
28 _GITHUB_REPO =
'grpc/grpc'
29 _GITHUB_APP_ID = 22338
30 _INSTALLATION_ID = 519109
32 _ACCESS_TOKEN_CACHE =
None
33 _ACCESS_TOKEN_FETCH_RETRIES = 6
34 _ACCESS_TOKEN_FETCH_RETRIES_INTERVAL_S = 15
44 _INCREASE_DECREASE = {
52 github_app_key =
open(
53 os.path.join(os.environ[
'KOKORO_KEYSTORE_DIR'],
54 '73836_grpc_checks_private_key'),
'rb').
read()
57 'iat':
int(time.time()),
58 'exp':
int(time.time() + 60 * 10),
59 'iss': _GITHUB_APP_ID,
66 global _ACCESS_TOKEN_CACHE
67 if _ACCESS_TOKEN_CACHE ==
None or _ACCESS_TOKEN_CACHE[
'exp'] < time.time():
68 for i
in range(_ACCESS_TOKEN_FETCH_RETRIES):
70 url=
'https://api.github.com/app/installations/%s/access_tokens'
74 'Accept':
'application/vnd.github.machine-man-preview+json',
78 _ACCESS_TOKEN_CACHE = {
79 'token': resp.json()[
'token'],
80 'exp': time.time() + 60
83 except (KeyError, ValueError):
85 print(
'HTTP Status %d %s' % (resp.status_code, resp.reason))
86 print(
"Fetch access token from Github API failed:")
88 if i != _ACCESS_TOKEN_FETCH_RETRIES - 1:
89 print(
'Retrying after %.2f second.' %
90 _ACCESS_TOKEN_FETCH_RETRIES_INTERVAL_S)
91 time.sleep(_ACCESS_TOKEN_FETCH_RETRIES_INTERVAL_S)
93 print(
"error: Unable to fetch access token, exiting...")
96 return _ACCESS_TOKEN_CACHE[
'token']
99 def _call(url, method='GET', json=None):
100 if not url.startswith(
'https://'):
101 url = _GITHUB_API_PREFIX + url
104 'Accept':
'application/vnd.github.antiope-preview+json',
106 return requests.request(method=method, url=url, headers=headers, json=json)
111 '/repos/%s/pulls/%s/commits' %
112 (_GITHUB_REPO, os.environ[
'KOKORO_GITHUB_PULL_REQUEST_NUMBER']))
113 return resp.json()[-1]
117 """Create/Update a check on current pull request.
119 The check runs are aggregated by their name, so newer check will update the
120 older check with the same name.
122 Requires environment variable 'KOKORO_GITHUB_PULL_REQUEST_NUMBER' to indicate which pull request
126 name: The name of the check.
127 summary: A str in Markdown to be used as the detail information of the check.
128 success: A bool indicates whether the check is succeed or not.
130 if 'KOKORO_GIT_COMMIT' not in os.environ:
131 print(
'Missing KOKORO_GIT_COMMIT env var: not checking')
133 if 'KOKORO_KEYSTORE_DIR' not in os.environ:
134 print(
'Missing KOKORO_KEYSTORE_DIR env var: not checking')
136 if 'KOKORO_GITHUB_PULL_REQUEST_NUMBER' not in os.environ:
137 print(
'Missing KOKORO_GITHUB_PULL_REQUEST_NUMBER env var: not checking')
139 MAX_SUMMARY_LEN = 65400
140 if len(summary) > MAX_SUMMARY_LEN:
142 print(
'Clipping too long summary')
144 summary = summary[:MAX_SUMMARY_LEN] +
'\n\n\n... CLIPPED (too long)'
145 completion_time =
str(
146 datetime.datetime.utcnow().replace(microsecond=0).isoformat()) +
'Z'
147 resp =
_call(
'/repos/%s/check-runs' % _GITHUB_REPO,
151 'head_sha': os.environ[
'KOKORO_GIT_COMMIT'],
152 'status':
'completed',
153 'completed_at': completion_time,
154 'conclusion':
'success' if success
else 'failure',
160 print(
'Result of Creating/Updating Check on PR:',
161 json.dumps(resp.json(), indent=2))
165 """Add a label to the PR indicating the significance of the check.
167 Requires environment variable 'KOKORO_GITHUB_PULL_REQUEST_NUMBER' to indicate which pull request
171 name: The name of the label.
172 value: A str in Markdown to be used as the detail information of the label.
174 if change <
min(list(labels.keys())):
175 change =
min(list(labels.keys()))
176 if change >
max(list(labels.keys())):
177 change =
max(list(labels.keys()))
178 value = labels[change]
179 if 'KOKORO_GIT_COMMIT' not in os.environ:
180 print(
'Missing KOKORO_GIT_COMMIT env var: not checking')
182 if 'KOKORO_KEYSTORE_DIR' not in os.environ:
183 print(
'Missing KOKORO_KEYSTORE_DIR env var: not checking')
185 if 'KOKORO_GITHUB_PULL_REQUEST_NUMBER' not in os.environ:
186 print(
'Missing KOKORO_GITHUB_PULL_REQUEST_NUMBER env var: not checking')
189 '/repos/%s/issues/%s/labels' %
190 (_GITHUB_REPO, os.environ[
'KOKORO_GITHUB_PULL_REQUEST_NUMBER']),
192 print(
'Result of fetching labels on PR:', existing)
193 new = [x[
'name']
for x
in existing
if not x[
'name'].startswith(name +
'/')]
194 new.append(name +
'/' + value)
196 '/repos/%s/issues/%s/labels' %
197 (_GITHUB_REPO, os.environ[
'KOKORO_GITHUB_PULL_REQUEST_NUMBER']),
200 print(
'Result of setting labels on PR:', resp.text)
204 if change <= -significant:
206 elif change >= significant: