Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[misc] Fix check previous run by using GITHUB_TOKEN to authenticate APIs #1849

Merged
merged 4 commits into from Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/workflows/persubmit.yml
Expand Up @@ -67,10 +67,15 @@ jobs:
with:
python-version: 3.8
- name: Check the previous run
env:
PR: ${{ github.event.pull_request.number }}
SHA: ${{ github.event.pull_request.head.sha }}
# https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token
# https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets
# Do not leak the secret
OAUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR=${{ github.event.pull_request.number }}
SHA=${{ github.event.pull_request.head.sha }}
python misc/ci_check_previous_run.py --pr ${PR} --sha ${SHA}
python misc/ci_check_previous_run.py --pr "${PR}" --sha "${SHA}" --token "${OAUTH_TOKEN}"

code_format:
name: Code Format
Expand Down
20 changes: 17 additions & 3 deletions misc/ci_check_previous_run.py
Expand Up @@ -7,15 +7,26 @@

API_PREFIX = 'https://api.github.com/repos/taichi-dev/taichi'
SHA = 'sha'
OAUTH_TOKEN = None


def make_api_url(p):
return f'{API_PREFIX}/{p}'


def send_request(url):
logging.debug(f'request={url}')
return ur.urlopen(url)
# https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#example-calling-the-rest-api
hdrs = {'Authorization': f'Bearer {OAUTH_TOKEN}'}
# https://stackoverflow.com/a/47029281/12003165
req = ur.Request(url, headers=hdrs)
res = ur.urlopen(req)
# Headers are defined in https://developer.github.com/v3/rate_limit/
# https://docs.github.com/en/actions/getting-started-with-github-actions/about-github-actions#usage-limits
rl = res.getheader('X-Ratelimit-Limit', None)
rl_remain = res.getheader('X-Ratelimit-Remaining', None)
logging.debug(
f'request={url} rate_limit={rl} rate_limit_remaining={rl_remain}')
return res


def get_commits(pr):
Expand Down Expand Up @@ -88,14 +99,15 @@ def get_status_of_run(run_id):
logging.info(
f'Waiting to get the status of run={run_id} (url={url}). retries={retries}'
)
time.sleep(15)
time.sleep(60)
return False


def get_cmd_args():
parser = argparse.ArgumentParser()
parser.add_argument('--pr', help='PR number')
parser.add_argument('--sha', help='Head commit SHA in the PR')
parser.add_argument('--token', help='OAuth token')
return parser.parse_args()


Expand All @@ -104,6 +116,8 @@ def main():
level=logging.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S')
args = get_cmd_args()
global OAUTH_TOKEN
OAUTH_TOKEN = args.token

pr = args.pr
commits = get_commits(pr)
Expand Down