-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcompliance_tests.py
86 lines (66 loc) · 2.58 KB
/
compliance_tests.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import logging
import falcon
import falcon.asgi
import zipfile
import io
import features
import lookups
import middleware.auth
import ci.log
ci.log.configure_default_logging()
logger = logging.getLogger(__name__)
@middleware.auth.noauth
class DownloadTestResults:
required_features = (features.FeatureTests,)
def __init__(
self,
component_with_tests_callback,
github_api_lookup,
):
self.component_with_tests_callback = component_with_tests_callback
self.github_api_lookup = github_api_lookup
def on_get(
self,
req: falcon.asgi.Request,
resp: falcon.asgi.Response,
):
"""
Downloads the zipped test results for the specified component release
"""
component_name: str = req.get_param('componentName', required=True)
component_version: str = req.get_param('componentVersion', required=True)
github_repo_lookup = lookups.github_repo_lookup(self.github_api_lookup)
# todo: lookup repository in component-descriptor
gh_api = self.github_api_lookup(
component_name,
)
repo = github_repo_lookup(component_name)
release = repo.release_from_tag(component_version)
assets = release.assets()
# todo: use streaming
zip_buffer = io.BytesIO()
zipf = zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED)
component_with_tests = self.component_with_tests_callback(component_name)
if not component_with_tests:
raise falcon.HTTPBadRequest()
for asset in assets:
# only add test assets that are prefixed like configured in the features config
if not asset.name.startswith(tuple(component_with_tests.assetNamePrefixes)):
continue
logger.debug(f'add test file {asset.name}')
with gh_api._get(
asset.download_url,
allow_redirects=True,
) as assetResp:
if assetResp.headers['Content-Type'] not in ['application/octet-stream']:
content_type = assetResp.headers['Content-Type']
logger.debug(f'unknown content type {content_type} for asset {asset.name}')
continue
zipf.writestr(
data=assetResp.content,
zinfo_or_arcname=asset.name,
)
zipf.close()
resp.content_type = 'application/zip'
resp.downloadable_as = f'{component_with_tests.downloadableName}_{component_version}.zip'
resp.data = zip_buffer.getvalue()