-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint
executable file
·102 lines (81 loc) · 3.61 KB
/
entrypoint
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2020 Pier Luigi Fiorini <[email protected]>
#
# SPDX-License-Identifier: MIT
from zipfile import ZipFile
import os
import requests
import sys
class GitHub(object):
url = 'https://api.github.com'
def __init__(self, username, password):
self.username = username
self.password = password
def workflows(self, owner, repo):
url = self.url + f'/repos/{owner}/{repo}/actions/workflows'
r = requests.get(url, auth=(self.username, self.password)).json() or {}
return r.get('workflows', [])
def runs(self, owner, repo, workflow_id):
url = self.url + f'/repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs'
r = requests.get(url, auth=(self.username, self.password)).json() or {}
return r.get('workflow_runs', [])
def artifacts(self, url):
artifacts = []
r = requests.get(url, auth=(self.username, self.password)).json() or {}
for artifact in r.get('artifacts', []):
if artifact['expired']:
print('Ignoring expired artifact #{}'.format(artifact['id']))
continue
artifacts.append(artifact)
return artifacts
def download_artifact(self, url, filename):
response = requests.get(url, stream=True, auth=(self.username, self.password))
if response.status_code == 200:
print(f'Downloading {filename}...')
with open(filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return True
return False
def main():
github_actor = os.environ['GITHUB_ACTOR']
github_token = os.environ.get('INPUT_GITHUB_TOKEN', os.environ.get('GITHUB_TOKEN'))
for key in ('INPUT_REPOSITORY', 'INPUT_WORKFLOW_PATH', 'INPUT_ARTIFACT_NAME', 'INPUT_EXTRACT_TO'):
if key not in os.environ:
raise SystemExit(f'Missing environment variable {key}')
repository = os.environ['INPUT_REPOSITORY']
workflow_path = os.environ['INPUT_WORKFLOW_PATH']
artifact_name = os.environ['INPUT_ARTIFACT_NAME']
extract_path = os.environ['INPUT_EXTRACT_TO']
save_as = os.environ.get('INPUT_SAVE_AS')
if not repository:
raise SystemExit('Empty "repository"')
if not workflow_path:
raise SystemExit('Empty "workflow_path"')
if not artifact_name:
raise SystemExit('Empty "artifact_name"')
if not extract_path:
raise SystemExit('Empty "extract_to"')
if save_as and os.path.isabs(save_as):
raise SystemExit('Please provide a file name, not an absolute path in "save_as"')
owner, repo = repository.split('/')
gh = GitHub(github_actor, github_token)
for workflow in gh.workflows(owner, repo):
if workflow['path'] == workflow_path:
for run in gh.runs(owner, repo, workflow['id']):
for artifact in gh.artifacts(run['artifacts_url']):
if artifact['name'] == artifact_name:
filename = artifact['name'] + '.zip'
if gh.download_artifact(artifact['archive_download_url'], filename):
with ZipFile(filename, 'r') as z:
z.extractall(extract_path)
if save_as:
os.rename(filename, save_as)
return True
return False
print(f'Unable to find {artifact_name}', file=sys.stderr)
return False
if __name__ == '__main__':
if main() is False:
sys.exit(1)