-
Notifications
You must be signed in to change notification settings - Fork 4
/
compliance_tests.py
95 lines (73 loc) · 2.89 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
87
88
89
90
91
92
93
94
95
import io
import logging
import zipfile
import aiohttp.web
import ci.log
import consts
import features
import util
ci.log.configure_default_logging()
logger = logging.getLogger(__name__)
class DownloadTestResults(aiohttp.web.View):
required_features = (features.FeatureTests,)
async def get(self):
'''
---
description: Downloads the zipped test results for the specified component release.
tags:
- Components
produces:
- application/zip
parameters:
- in: query
name: componentName
type: string
required: true
- in: query
name: componentVersion
type: string
required: true
'''
params = self.request.rel_url.query
component_name = util.param(params, 'componentName', required=True)
component_version = util.param(params, 'componentVersion', required=True)
# todo: lookup repository in component-descriptor
gh_api = self.request.app[consts.APP_GITHUB_API_LOOKUP](component_name)
repo = self.request.app[consts.APP_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_callback = self.request.app[consts.APP_COMPONENT_WITH_TESTS_CALLBACK]
component_with_tests = component_with_tests_callback(component_name)
if not component_with_tests:
raise aiohttp.web.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,
headers={
'Accept': 'application/octet-stream',
},
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()
response = aiohttp.web.Response(
body=zip_buffer.getvalue(),
content_type='application/zip',
)
fname = f'{component_with_tests.downloadableName}_{component_version}.zip'
response.headers.add('Content-Disposition', f'attachment; filename="{fname}"')
return response