Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit ba3cede

Browse files
committed
refactor: create ReportType enum
we only want to ingest a string from the cli options as click doesn't support enums yet "internally" we only want to use this enum
1 parent b7033a9 commit ba3cede

File tree

13 files changed

+113
-64
lines changed

13 files changed

+113
-64
lines changed

codecov_cli/commands/process_test_results.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
send_get_request,
2121
send_post_request,
2222
)
23+
from codecov_cli.helpers.upload_type import ReportType
2324
from codecov_cli.services.upload.file_finder import select_file_finder
2425
from codecov_cli.types import CommandContext, RequestResult, UploadCollectionResultFile
2526

@@ -100,7 +101,7 @@ def process_test_results(
100101
github_token=None,
101102
):
102103
file_finder = select_file_finder(
103-
dir, exclude_folders, files, disable_search, report_type="test_results"
104+
dir, exclude_folders, files, disable_search, report_type=ReportType.TEST_RESULTS
104105
)
105106

106107
upload_collection_results: List[UploadCollectionResultFile] = (

codecov_cli/commands/upload.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import os
33
import pathlib
44
import typing
5-
65
import click
76

87
from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
98
from codecov_cli.helpers.args import get_cli_args
109
from codecov_cli.helpers.options import global_options
1110
from codecov_cli.services.upload import do_upload_logic
1211
from codecov_cli.types import CommandContext
12+
from codecov_cli.helpers.upload_type import report_type_from_str, ReportType
1313

1414
logger = logging.getLogger("codecovcli")
1515

@@ -166,6 +166,7 @@ def _turn_env_vars_into_dict(ctx, params, value):
166166
),
167167
click.option(
168168
"--report-type",
169+
"report_type_str",
169170
help="The type of the file to upload, coverage by default. Possible values are: testing, coverage.",
170171
default="coverage",
171172
type=click.Choice(["coverage", "test_results"]),
@@ -240,7 +241,7 @@ def do_upload(
240241
network_root_folder: pathlib.Path,
241242
plugin_names: typing.List[str],
242243
pull_request_number: typing.Optional[str],
243-
report_type: str,
244+
report_type_str: str,
244245
slug: typing.Optional[str],
245246
swift_project: typing.Optional[str],
246247
token: typing.Optional[str],
@@ -258,6 +259,8 @@ def do_upload(
258259
extra_log_attributes=args,
259260
),
260261
)
262+
263+
report_type: ReportType = report_type_from_str(report_type_str)
261264
do_upload_logic(
262265
cli_config,
263266
versioning_system,
@@ -293,7 +296,7 @@ def do_upload(
293296
slug=slug,
294297
swift_project=swift_project,
295298
token=token,
296-
upload_file_type=report_type,
299+
report_type=report_type,
297300
use_legacy_uploader=use_legacy_uploader,
298301
args=args,
299302
)

codecov_cli/commands/upload_coverage.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from codecov_cli.commands.upload import do_upload, global_upload_options
1010
from codecov_cli.helpers.args import get_cli_args
1111
from codecov_cli.helpers.options import global_options
12+
from codecov_cli.helpers.upload_type import report_type_from_str, ReportType
1213
from codecov_cli.services.upload_coverage import upload_coverage_logic
1314
from codecov_cli.types import CommandContext
1415

@@ -54,7 +55,7 @@ def upload_coverage(
5455
plugin_names: typing.List[str],
5556
pull_request_number: typing.Optional[str],
5657
report_code: str,
57-
report_type: str,
58+
report_type_str: str,
5859
slug: typing.Optional[str],
5960
swift_project: typing.Optional[str],
6061
token: typing.Optional[str],
@@ -68,7 +69,9 @@ def upload_coverage(
6869
),
6970
)
7071

71-
if not use_legacy_uploader and report_type == "coverage":
72+
report_type = report_type_from_str(report_type_str)
73+
74+
if not use_legacy_uploader and report_type == ReportType.COVERAGE:
7275
versioning_system = ctx.obj["versioning_system"]
7376
codecov_yaml = ctx.obj["codecov_yaml"] or {}
7477
cli_config = codecov_yaml.get("cli", {})
@@ -112,7 +115,7 @@ def upload_coverage(
112115
slug=slug,
113116
swift_project=swift_project,
114117
token=token,
115-
upload_file_type=report_type,
118+
report_type=report_type,
116119
use_legacy_uploader=use_legacy_uploader,
117120
args=args,
118121
)
@@ -128,7 +131,7 @@ def upload_coverage(
128131
git_service=git_service,
129132
fail_on_error=True,
130133
)
131-
if report_type == "coverage":
134+
if report_type == ReportType.COVERAGE:
132135
ctx.invoke(
133136
create_report,
134137
token=token,
@@ -167,7 +170,7 @@ def upload_coverage(
167170
plugin_names=plugin_names,
168171
pull_request_number=pull_request_number,
169172
report_code=report_code,
170-
report_type=report_type,
173+
report_type_str=report_type_str,
171174
slug=slug,
172175
swift_project=swift_project,
173176
token=token,

codecov_cli/commands/upload_process.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from codecov_cli.commands.upload import do_upload, global_upload_options
1010
from codecov_cli.helpers.args import get_cli_args
1111
from codecov_cli.helpers.options import global_options
12+
from codecov_cli.helpers.upload_type import report_type_from_str, ReportType
1213
from codecov_cli.types import CommandContext
1314

1415
logger = logging.getLogger("codecovcli")
@@ -53,7 +54,7 @@ def upload_process(
5354
plugin_names: typing.List[str],
5455
pull_request_number: typing.Optional[str],
5556
report_code: str,
56-
report_type: str,
57+
report_type_str: str,
5758
slug: typing.Optional[str],
5859
swift_project: typing.Optional[str],
5960
token: typing.Optional[str],
@@ -78,7 +79,9 @@ def upload_process(
7879
git_service=git_service,
7980
fail_on_error=True,
8081
)
81-
if report_type == "coverage":
82+
83+
report_type = report_type_from_str(report_type_str)
84+
if report_type == ReportType.COVERAGE:
8285
ctx.invoke(
8386
create_report,
8487
token=token,
@@ -117,7 +120,7 @@ def upload_process(
117120
plugin_names=plugin_names,
118121
pull_request_number=pull_request_number,
119122
report_code=report_code,
120-
report_type=report_type,
123+
report_type_str=report_type_str,
121124
slug=slug,
122125
swift_project=swift_project,
123126
token=token,

codecov_cli/helpers/upload_type.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from enum import Enum
2+
3+
4+
class ReportType(Enum):
5+
COVERAGE = "coverage"
6+
TEST_RESULTS = "test_results"
7+
8+
9+
def report_type_from_str(report_type_str: str) -> ReportType:
10+
if report_type_str == "coverage":
11+
return ReportType.COVERAGE
12+
elif report_type_str == "test_results":
13+
return ReportType.TEST_RESULTS
14+
else:
15+
raise ValueError(f"Invalid upload type: {report_type_str}")

codecov_cli/services/upload/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from codecov_cli.helpers.ci_adapters.base import CIAdapterBase
99
from codecov_cli.helpers.request import log_warnings_and_errors_if_any
1010
from codecov_cli.helpers.versioning_systems import VersioningSystemInterface
11+
from codecov_cli.helpers.upload_type import ReportType
1112
from codecov_cli.plugins import select_preparation_plugins
1213
from codecov_cli.services.upload.file_finder import select_file_finder
1314
from codecov_cli.services.upload.legacy_upload_sender import LegacyUploadSender
@@ -59,7 +60,7 @@ def do_upload_logic(
5960
slug: typing.Optional[str],
6061
swift_project: typing.Optional[str],
6162
token: typing.Optional[str],
62-
upload_file_type: str = "coverage",
63+
report_type: ReportType = ReportType.COVERAGE,
6364
use_legacy_uploader: bool = False,
6465
):
6566
plugin_config = {
@@ -71,18 +72,18 @@ def do_upload_logic(
7172
"project_root": files_search_root_folder,
7273
"swift_project": swift_project,
7374
}
74-
if upload_file_type == "coverage":
75+
if report_type == ReportType.COVERAGE:
7576
preparation_plugins = select_preparation_plugins(
7677
cli_config, plugin_names, plugin_config
7778
)
78-
elif upload_file_type == "test_results":
79+
elif report_type == ReportType.TEST_RESULTS:
7980
preparation_plugins = []
8081
file_selector = select_file_finder(
8182
files_search_root_folder,
8283
files_search_exclude_folders,
8384
files_search_explicitly_listed_files,
8485
disable_search,
85-
upload_file_type,
86+
report_type,
8687
)
8788
network_finder = select_network_finder(
8889
versioning_system,
@@ -98,7 +99,7 @@ def do_upload_logic(
9899
plugin_config,
99100
)
100101
try:
101-
upload_data = collector.generate_upload_data(upload_file_type)
102+
upload_data = collector.generate_upload_data(report_type)
102103
except click.ClickException as exp:
103104
if handle_no_reports_found:
104105
logger.info(
@@ -138,7 +139,7 @@ def do_upload_logic(
138139
token=token,
139140
env_vars=env_vars,
140141
report_code=report_code,
141-
upload_file_type=upload_file_type,
142+
report_type=report_type,
142143
name=name,
143144
branch=branch,
144145
slug=slug,

codecov_cli/services/upload/file_finder.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Iterable, List, Optional, Pattern
55

66
from codecov_cli.helpers.folder_searcher import globs_to_regex, search_files
7+
from codecov_cli.helpers.upload_type import ReportType
78
from codecov_cli.types import UploadCollectionResultFile
89

910
logger = logging.getLogger("codecovcli")
@@ -189,19 +190,19 @@ def __init__(
189190
folders_to_ignore: Optional[List[str]] = None,
190191
explicitly_listed_files: Optional[List[Path]] = None,
191192
disable_search: bool = False,
192-
report_type: str = "coverage",
193+
report_type: ReportType = ReportType.COVERAGE,
193194
):
194195
self.search_root = search_root or Path(os.getcwd())
195196
self.folders_to_ignore = folders_to_ignore or []
196197
self.explicitly_listed_files = explicitly_listed_files or None
197198
self.disable_search = disable_search
198-
self.report_type = report_type
199+
self.report_type: ReportType = report_type
199200

200201
def find_files(self) -> List[UploadCollectionResultFile]:
201-
if self.report_type == "coverage":
202+
if self.report_type == ReportType.COVERAGE:
202203
files_excluded_patterns = coverage_files_excluded_patterns
203204
files_patterns = coverage_files_patterns
204-
elif self.report_type == "test_results":
205+
elif self.report_type == ReportType.TEST_RESULTS:
205206
files_excluded_patterns = test_results_files_excluded_patterns
206207
files_patterns = test_results_files_patterns
207208
regex_patterns_to_exclude = globs_to_regex(files_excluded_patterns)
@@ -278,7 +279,7 @@ def select_file_finder(
278279
folders_to_ignore,
279280
explicitly_listed_files,
280281
disable_search,
281-
report_type="coverage",
282+
report_type: ReportType = ReportType.COVERAGE,
282283
):
283284
return FileFinder(
284285
root_folder_to_search,

codecov_cli/services/upload/upload_collector.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import click
1010

11+
from codecov_cli.helpers.upload_type import ReportType
1112
from codecov_cli.services.upload.file_finder import FileFinder
1213
from codecov_cli.services.upload.network_finder import NetworkFinder
1314
from codecov_cli.types import (
@@ -148,16 +149,18 @@ def _get_file_fixes(
148149
path, fixed_lines_without_reason, fixed_lines_with_reason, eof
149150
)
150151

151-
def generate_upload_data(self, report_type="coverage") -> UploadCollectionResult:
152+
def generate_upload_data(
153+
self, report_type: ReportType = ReportType.COVERAGE
154+
) -> UploadCollectionResult:
152155
for prep in self.preparation_plugins:
153156
logger.debug(f"Running preparation plugin: {type(prep)}")
154157
prep.run_preparation(self)
155158
logger.debug("Collecting relevant files")
156159
network = self.network_finder.find_files()
157160
report_files = self.file_finder.find_files()
158-
logger.info(f"Found {len(report_files)} {report_type} files to report")
161+
logger.info(f"Found {len(report_files)} {report_type.value} files to report")
159162
if not report_files:
160-
if report_type == "test_results":
163+
if report_type == ReportType.TEST_RESULTS:
161164
error_message = "No JUnit XML reports found. Please review our documentation (https://docs.codecov.com/docs/test-result-ingestion-beta) to generate and upload the file."
162165
else:
163166
error_message = "No coverage reports found. Please make sure you're generating reports successfully."

codecov_cli/services/upload/upload_sender.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from codecov_cli import __version__ as codecov_cli_version
99
from codecov_cli.helpers.config import CODECOV_INGEST_URL
1010
from codecov_cli.helpers.encoder import encode_slug
11+
from codecov_cli.helpers.upload_type import ReportType
1112
from codecov_cli.helpers.request import (
1213
get_token_header,
1314
send_post_request,
@@ -30,7 +31,7 @@ def send_upload_data(
3031
token: typing.Optional[str],
3132
env_vars: typing.Dict[str, str],
3233
report_code: str,
33-
upload_file_type: str = "coverage",
34+
report_type: ReportType = ReportType.COVERAGE,
3435
name: typing.Optional[str] = None,
3536
branch: typing.Optional[str] = None,
3637
slug: typing.Optional[str] = None,
@@ -67,7 +68,7 @@ def send_upload_data(
6768
upload_url = enterprise_url or CODECOV_INGEST_URL
6869
url, data = self.get_url_and_possibly_update_data(
6970
data,
70-
upload_file_type,
71+
report_type,
7172
upload_url,
7273
git_service,
7374
branch,
@@ -77,9 +78,7 @@ def send_upload_data(
7778
upload_coverage,
7879
)
7980
# Data that goes to storage
80-
reports_payload = self._generate_payload(
81-
upload_data, env_vars, upload_file_type
82-
)
81+
reports_payload = self._generate_payload(upload_data, env_vars, report_type)
8382

8483
logger.debug("Sending upload request to Codecov")
8584
resp_from_codecov = send_post_request(
@@ -107,10 +106,10 @@ def _generate_payload(
107106
self,
108107
upload_data: UploadCollectionResult,
109108
env_vars: typing.Dict[str, str],
110-
upload_file_type="coverage",
109+
report_type: ReportType = ReportType.COVERAGE,
111110
) -> bytes:
112111
network_files = upload_data.network
113-
if upload_file_type == "coverage":
112+
if report_type == ReportType.COVERAGE:
114113
payload = {
115114
"report_fixes": {
116115
"format": "legacy",
@@ -120,7 +119,7 @@ def _generate_payload(
120119
"coverage_files": self._get_files(upload_data),
121120
"metadata": {},
122121
}
123-
elif upload_file_type == "test_results":
122+
elif report_type == ReportType.TEST_RESULTS:
124123
payload = {
125124
"test_results_files": self._get_files(upload_data),
126125
}
@@ -178,7 +177,7 @@ def _get_format_info(self, file: UploadCollectionResultFile):
178177
def get_url_and_possibly_update_data(
179178
self,
180179
data,
181-
report_type,
180+
report_type: ReportType,
182181
upload_url,
183182
git_service,
184183
branch,
@@ -187,13 +186,13 @@ def get_url_and_possibly_update_data(
187186
report_code,
188187
upload_coverage=False,
189188
):
190-
if report_type == "coverage":
189+
if report_type == ReportType.COVERAGE:
191190
base_url = f"{upload_url}/upload/{git_service}/{encoded_slug}"
192191
if upload_coverage:
193192
url = f"{base_url}/upload-coverage"
194193
else:
195194
url = f"{base_url}/commits/{commit_sha}/reports/{report_code}/uploads"
196-
elif report_type == "test_results":
195+
elif report_type == ReportType.TEST_RESULTS:
197196
data["slug"] = encoded_slug
198197
data["branch"] = branch
199198
data["commit"] = commit_sha

0 commit comments

Comments
 (0)