-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_artifact_url.py
72 lines (63 loc) · 2.68 KB
/
get_artifact_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#! /usr/bin/env python3
from argparse import ArgumentParser
from typing import Optional
from utils import get_response_json
def main():
"""
Given a branch of the Horace-Euphonic-Interface repo,
prints the latest artifact url
"""
parser = ArgumentParser()
parser.add_argument('branch', type=str,
help='Branch of repo to query')
args = parser.parse_args()
url = get_artifact_url(branch=args.branch)
print(url)
def get_artifact_url(repo: str = 'horace-euphonic-interface',
branch: str = 'master',
workflow_name: str = 'Horace-Euphonic-Interface Tests',
artifact_name: str = 'horace_euphonic_interface.mltbx'):
"""
For a specific pace-neutrons Github repository and branch, query
the workflows API to get the latest successful build, get the matching
build artifact and print the artifact download URL.
"""
base_url = 'https://api.github.com'
# Get workflow ID
workflows_url = f'{base_url}/repos/pace-neutrons/{repo}/actions/workflows'
content = get_response_json(workflows_url)
workflow_id = None
for workflow in content['workflows']:
if workflow['name'] == workflow_name:
workflow_id = workflow['id']
break
if workflow_id is None:
raise RuntimeError(f'Workflow with name {workflow_name} '
f'couldn\'t be found at {workflows_url}')
# Get matching workflow run id
workflow_runs_url = f'{workflows_url}/{workflow_id}/runs'
content = get_response_json(workflow_runs_url)
workflow_run_id = None
for workflow_run in content['workflow_runs']:
if (workflow_run['status'] == 'completed' and
workflow_run['conclusion'] == 'success' and
workflow_run['head_branch'] == branch):
workflow_run_id = workflow_run['id']
break
if workflow_run_id is None:
raise RuntimeError(f'Successful workflow with branch {branch} '
f'couldn\'t be found at {workflow_runs_url}')
# Get matching artifact
artifacts_url = (f'{base_url}/repos/pace-neutrons/{repo}/actions/'
f'runs/{workflow_run_id}/artifacts')
content = get_response_json(artifacts_url)
artifact_download_url = None
for artifact in content['artifacts']:
if artifact['name'] == artifact_name:
artifact_download_url = artifact['archive_download_url']
if artifact_download_url is None:
raise RuntimeError(f'Artifact with name {artifact_name} '
f'couldn\'t be found at {artifacts_url}')
return artifact_download_url
if __name__ == '__main__':
main()