This repository was archived by the owner on Jul 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathupload_sender.py
More file actions
232 lines (209 loc) · 8.18 KB
/
upload_sender.py
File metadata and controls
232 lines (209 loc) · 8.18 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import base64
import json
import logging
import typing
import zlib
from typing import Any, Dict
import sentry_sdk
from codecov_cli import __version__ as codecov_cli_version
from codecov_cli.helpers.config import CODECOV_INGEST_URL
from codecov_cli.helpers.encoder import encode_slug
from codecov_cli.helpers.upload_type import ReportType
from codecov_cli.helpers.request import (
get_token_header,
send_post_request,
send_put_request,
)
from codecov_cli.types import (
RequestResult,
UploadCollectionResult,
UploadCollectionResultFile,
)
logger = logging.getLogger("codecovcli")
class UploadSender(object):
def send_upload_data(
self,
upload_data: UploadCollectionResult,
commit_sha: str,
token: typing.Optional[str],
env_vars: typing.Dict[str, str],
report_code: str,
report_type: ReportType = ReportType.COVERAGE,
name: typing.Optional[str] = None,
branch: typing.Optional[str] = None,
slug: typing.Optional[str] = None,
pull_request_number: typing.Optional[str] = None,
build_code: typing.Optional[str] = None,
build_url: typing.Optional[str] = None,
job_code: typing.Optional[str] = None,
flags: typing.List[str] = None,
ci_service: typing.Optional[str] = None,
git_service: typing.Optional[str] = None,
enterprise_url: typing.Optional[str] = None,
parent_sha: typing.Optional[str] = None,
upload_coverage: bool = False,
args: dict = None,
) -> RequestResult:
current_transaction = sentry_sdk.get_current_scope().transaction
if current_transaction:
current_transaction.set_data("commit_sha", commit_sha)
current_transaction.set_data("slug", slug)
with sentry_sdk.start_span(name="upload_sender"):
with sentry_sdk.start_span(name="upload_sender_preparation"):
file_not_found = False
if report_type == ReportType.TEST_RESULTS and not upload_data.files:
file_not_found = True
data = {
"ci_service": ci_service,
"ci_url": build_url,
"cli_args": args,
"env": env_vars,
"flags": flags,
"job_code": job_code,
"name": name,
"version": codecov_cli_version,
"file_not_found": file_not_found,
}
if upload_coverage:
data["branch"] = branch
data["code"] = report_code
data["commitid"] = commit_sha
data["parent_commit_id"] = parent_sha
data["pullid"] = pull_request_number
headers = get_token_header(token)
encoded_slug = encode_slug(slug)
upload_url = enterprise_url or CODECOV_INGEST_URL
url, data = self.get_url_and_possibly_update_data(
data,
report_type,
upload_url,
git_service,
branch,
encoded_slug,
commit_sha,
report_code,
upload_coverage,
)
# Data that goes to storage
reports_payload = self._generate_payload(
upload_data, env_vars, report_type
)
with sentry_sdk.start_span(name="upload_sender_storage_request"):
logger.debug("Sending upload request to Codecov")
resp_from_codecov = send_post_request(
url=url,
data=data,
headers=headers,
)
if file_not_found:
logger.info(
"No test results reports found. Triggering notifications without uploading."
)
return resp_from_codecov
if resp_from_codecov.status_code >= 400:
return resp_from_codecov
resp_json_obj = json.loads(resp_from_codecov.text)
if resp_json_obj.get("url"):
logger.info(
f"Your upload is now processing. When finished, results will be available at: {resp_json_obj.get('url')}"
)
logger.debug(
"Upload request to Codecov complete.",
extra=dict(extra_log_attributes=dict(response=resp_json_obj)),
)
put_url = resp_json_obj["raw_upload_location"]
with sentry_sdk.start_span(name="upload_sender_storage"):
logger.debug("Sending upload to storage")
resp_from_storage = send_put_request(put_url, data=reports_payload)
return resp_from_storage
def _generate_payload(
self,
upload_data: UploadCollectionResult,
env_vars: typing.Dict[str, str],
report_type: ReportType = ReportType.COVERAGE,
) -> bytes:
network_files = upload_data.network
if report_type == ReportType.COVERAGE:
payload = {
"report_fixes": {
"format": "legacy",
"value": self._get_file_fixers(upload_data),
},
"network_files": network_files if network_files is not None else [],
"coverage_files": self._get_files(upload_data),
"metadata": {},
}
elif report_type == ReportType.TEST_RESULTS:
payload = {
"test_results_files": self._get_files(upload_data),
}
json_data = json.dumps(payload)
return json_data.encode()
def _get_file_fixers(
self, upload_data: UploadCollectionResult
) -> Dict[str, Dict[str, Any]]:
"""
Returns file/path fixes in the following format:
{
{path}: {
"eof": int(eof_line),
"lines": {set_of_lines},
},
}
"""
file_fixers = {}
for file_fixer in upload_data.file_fixes:
fixed_lines_with_reason = set(
[fixer[0] for fixer in file_fixer.fixed_lines_with_reason]
)
total_fixed_lines = list(
file_fixer.fixed_lines_without_reason.union(fixed_lines_with_reason)
)
file_fixers[file_fixer.path.as_posix()] = {
"eof": file_fixer.eof,
"lines": total_fixed_lines,
}
return file_fixers
def _get_files(self, upload_data: UploadCollectionResult):
return [self._format_file(file) for file in upload_data.files]
def _format_file(self, file: UploadCollectionResultFile):
format, formatted_content = self._get_format_info(file)
return {
"filename": file.get_filename(),
"format": format,
"data": formatted_content,
"labels": "",
}
def _get_format_info(self, file: UploadCollectionResultFile):
format = "base64+compressed"
formatted_content = (
base64.b64encode(zlib.compress((file.get_content())))
).decode()
return format, formatted_content
def get_url_and_possibly_update_data(
self,
data,
report_type: ReportType,
upload_url,
git_service,
branch,
encoded_slug,
commit_sha,
report_code,
upload_coverage=False,
file_not_found=False,
):
if report_type == ReportType.COVERAGE:
base_url = f"{upload_url}/upload/{git_service}/{encoded_slug}"
if upload_coverage:
url = f"{base_url}/upload-coverage"
else:
url = f"{base_url}/commits/{commit_sha}/reports/{report_code}/uploads"
elif report_type == ReportType.TEST_RESULTS:
data["slug"] = encoded_slug
data["branch"] = branch
data["commit"] = commit_sha
data["service"] = git_service
data["file_not_found"] = file_not_found
url = f"{upload_url}/upload/test_results/v1"
return url, data