Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upload TA even when a file is not found #585

Merged
merged 4 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion codecov_cli/commands/process_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
send_get_request,
send_post_request,
)
from codecov_cli.helpers.upload_type import ReportType
from codecov_cli.services.upload.file_finder import select_file_finder
from codecov_cli.types import CommandContext, RequestResult, UploadCollectionResultFile

Expand Down Expand Up @@ -103,7 +104,11 @@ def process_test_results(
with sentry_sdk.start_transaction(op="task", name="Process Test Results"):
with sentry_sdk.start_span(name="process_test_results"):
file_finder = select_file_finder(
dir, exclude_folders, files, disable_search, report_type="test_results"
dir,
exclude_folders,
files,
disable_search,
report_type=ReportType.TEST_RESULTS,
)

upload_collection_results: List[UploadCollectionResultFile] = (
Expand Down
13 changes: 9 additions & 4 deletions codecov_cli/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import pathlib
import typing

import click
import sentry_sdk

Expand All @@ -11,6 +10,7 @@
from codecov_cli.helpers.options import global_options
from codecov_cli.services.upload import do_upload_logic
from codecov_cli.types import CommandContext
from codecov_cli.helpers.upload_type import report_type_from_str, ReportType

logger = logging.getLogger("codecovcli")

Expand Down Expand Up @@ -167,6 +167,7 @@ def _turn_env_vars_into_dict(ctx, params, value):
),
click.option(
"--report-type",
"report_type_str",
help="The type of the file to upload, coverage by default. Possible values are: testing, coverage.",
default="coverage",
type=click.Choice(["coverage", "test_results"]),
Expand Down Expand Up @@ -241,7 +242,7 @@ def do_upload(
network_root_folder: pathlib.Path,
plugin_names: typing.List[str],
pull_request_number: typing.Optional[str],
report_type: str,
report_type_str: str,
slug: typing.Optional[str],
swift_project: typing.Optional[str],
token: typing.Optional[str],
Expand All @@ -261,6 +262,8 @@ def do_upload(
extra_log_attributes=args,
),
)

report_type: ReportType = report_type_from_str(report_type_str)
do_upload_logic(
cli_config,
versioning_system,
Expand All @@ -276,7 +279,9 @@ def do_upload(
env_vars=env_vars,
fail_on_error=fail_on_error,
files_search_exclude_folders=list(files_search_exclude_folders),
files_search_explicitly_listed_files=list(files_search_explicitly_listed_files),
files_search_explicitly_listed_files=list(
files_search_explicitly_listed_files
),
files_search_root_folder=files_search_root_folder,
flags=flags,
gcov_args=gcov_args,
Expand All @@ -296,7 +301,7 @@ def do_upload(
slug=slug,
swift_project=swift_project,
token=token,
upload_file_type=report_type,
report_type=report_type,
use_legacy_uploader=use_legacy_uploader,
args=args,
)
13 changes: 8 additions & 5 deletions codecov_cli/commands/upload_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from codecov_cli.helpers.args import get_cli_args
from codecov_cli.helpers.options import global_options
from codecov_cli.opentelemetry import close_telem
from codecov_cli.helpers.upload_type import report_type_from_str, ReportType
from codecov_cli.services.upload_coverage import upload_coverage_logic
from codecov_cli.types import CommandContext

Expand Down Expand Up @@ -56,7 +57,7 @@ def upload_coverage(
plugin_names: typing.List[str],
pull_request_number: typing.Optional[str],
report_code: str,
report_type: str,
report_type_str: str,
slug: typing.Optional[str],
swift_project: typing.Optional[str],
token: typing.Optional[str],
Expand All @@ -72,7 +73,9 @@ def upload_coverage(
),
)

if not use_legacy_uploader and report_type == "coverage":
report_type = report_type_from_str(report_type_str)

if not use_legacy_uploader and report_type == ReportType.COVERAGE:
versioning_system = ctx.obj["versioning_system"]
codecov_yaml = ctx.obj["codecov_yaml"] or {}
cli_config = codecov_yaml.get("cli", {})
Expand Down Expand Up @@ -116,7 +119,7 @@ def upload_coverage(
slug=slug,
swift_project=swift_project,
token=token,
upload_file_type=report_type,
report_type=report_type,
use_legacy_uploader=use_legacy_uploader,
args=args,
)
Expand All @@ -132,7 +135,7 @@ def upload_coverage(
git_service=git_service,
fail_on_error=True,
)
if report_type == "coverage":
if report_type == ReportType.COVERAGE:
ctx.invoke(
create_report,
token=token,
Expand Down Expand Up @@ -171,7 +174,7 @@ def upload_coverage(
plugin_names=plugin_names,
pull_request_number=pull_request_number,
report_code=report_code,
report_type=report_type,
report_type_str=report_type_str,
slug=slug,
swift_project=swift_project,
token=token,
Expand Down
9 changes: 6 additions & 3 deletions codecov_cli/commands/upload_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from codecov_cli.commands.upload import do_upload, global_upload_options
from codecov_cli.helpers.args import get_cli_args
from codecov_cli.helpers.options import global_options
from codecov_cli.helpers.upload_type import report_type_from_str, ReportType
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")
Expand Down Expand Up @@ -54,7 +55,7 @@ def upload_process(
plugin_names: typing.List[str],
pull_request_number: typing.Optional[str],
report_code: str,
report_type: str,
report_type_str: str,
slug: typing.Optional[str],
swift_project: typing.Optional[str],
token: typing.Optional[str],
Expand All @@ -81,7 +82,9 @@ def upload_process(
git_service=git_service,
fail_on_error=True,
)
if report_type == "coverage":

report_type = report_type_from_str(report_type_str)
if report_type == ReportType.COVERAGE:
ctx.invoke(
create_report,
token=token,
Expand Down Expand Up @@ -120,7 +123,7 @@ def upload_process(
plugin_names=plugin_names,
pull_request_number=pull_request_number,
report_code=report_code,
report_type=report_type,
report_type_str=report_type_str,
slug=slug,
swift_project=swift_project,
token=token,
Expand Down
15 changes: 15 additions & 0 deletions codecov_cli/helpers/upload_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from enum import Enum


class ReportType(Enum):
COVERAGE = "coverage"
TEST_RESULTS = "test_results"


def report_type_from_str(report_type_str: str) -> ReportType:
if report_type_str == "coverage":
return ReportType.COVERAGE
elif report_type_str == "test_results":
return ReportType.TEST_RESULTS
else:
raise ValueError(f"Invalid upload type: {report_type_str}")
6 changes: 4 additions & 2 deletions codecov_cli/opentelemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@


def init_telem(ctx):
if ctx['disable_telem']:
if ctx["disable_telem"]:
return
if ctx['enterprise_url']: # dont run on dedicated cloud
if ctx["enterprise_url"]: # dont run on dedicated cloud
return
if os.getenv("CODECOV_ENV", "production") == "test":
return
if os.getenv('CODECOV_ENV', 'production') == 'test':
return
Expand Down
13 changes: 7 additions & 6 deletions codecov_cli/services/upload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from codecov_cli.helpers.ci_adapters.base import CIAdapterBase
from codecov_cli.helpers.request import log_warnings_and_errors_if_any
from codecov_cli.helpers.versioning_systems import VersioningSystemInterface
from codecov_cli.helpers.upload_type import ReportType
from codecov_cli.plugins import select_preparation_plugins
from codecov_cli.services.upload.file_finder import select_file_finder
from codecov_cli.services.upload.legacy_upload_sender import LegacyUploadSender
Expand Down Expand Up @@ -59,7 +60,7 @@ def do_upload_logic(
slug: typing.Optional[str],
swift_project: typing.Optional[str],
token: typing.Optional[str],
upload_file_type: str = "coverage",
report_type: ReportType = ReportType.COVERAGE,
use_legacy_uploader: bool = False,
):
plugin_config = {
Expand All @@ -71,18 +72,18 @@ def do_upload_logic(
"project_root": files_search_root_folder,
"swift_project": swift_project,
}
if upload_file_type == "coverage":
if report_type == ReportType.COVERAGE:
preparation_plugins = select_preparation_plugins(
cli_config, plugin_names, plugin_config
)
elif upload_file_type == "test_results":
elif report_type == ReportType.TEST_RESULTS:
preparation_plugins = []
file_selector = select_file_finder(
files_search_root_folder,
files_search_exclude_folders,
files_search_explicitly_listed_files,
disable_search,
upload_file_type,
report_type,
)
network_finder = select_network_finder(
versioning_system,
Expand All @@ -98,7 +99,7 @@ def do_upload_logic(
plugin_config,
)
try:
upload_data = collector.generate_upload_data(upload_file_type)
upload_data = collector.generate_upload_data(report_type)
except click.ClickException as exp:
if handle_no_reports_found:
logger.info(
Expand Down Expand Up @@ -138,7 +139,7 @@ def do_upload_logic(
token=token,
env_vars=env_vars,
report_code=report_code,
upload_file_type=upload_file_type,
report_type=report_type,
name=name,
branch=branch,
slug=slug,
Expand Down
25 changes: 17 additions & 8 deletions codecov_cli/services/upload/file_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sentry_sdk

from codecov_cli.helpers.folder_searcher import globs_to_regex, search_files
from codecov_cli.helpers.upload_type import ReportType
from codecov_cli.types import UploadCollectionResultFile

logger = logging.getLogger("codecovcli")
Expand Down Expand Up @@ -193,46 +194,54 @@ def __init__(
folders_to_ignore: Optional[List[Path]] = None,
explicitly_listed_files: Optional[List[Path]] = None,
disable_search: bool = False,
report_type: str = "coverage",
report_type: ReportType = ReportType.COVERAGE,
):
self.search_root = search_root or Path(os.getcwd())
self.folders_to_ignore = list(folders_to_ignore) if folders_to_ignore else []
self.explicitly_listed_files = explicitly_listed_files or None
self.disable_search = disable_search
self.report_type = report_type
self.report_type: ReportType = report_type

def find_files(self) -> List[UploadCollectionResultFile]:
with sentry_sdk.start_span(name="find_files"):
if self.report_type == "coverage":
if self.report_type == ReportType.COVERAGE:
files_excluded_patterns = coverage_files_excluded_patterns
files_patterns = coverage_files_patterns
elif self.report_type == "test_results":
elif self.report_type == ReportType.TEST_RESULTS:
files_excluded_patterns = test_results_files_excluded_patterns
files_patterns = test_results_files_patterns
regex_patterns_to_exclude = globs_to_regex(files_excluded_patterns)
assert regex_patterns_to_exclude # this is never `None`
files_paths: Iterable[Path] = []
user_files_paths = []
if self.explicitly_listed_files:
user_files_paths = self.get_user_specified_files(regex_patterns_to_exclude)
user_files_paths = self.get_user_specified_files(
regex_patterns_to_exclude
)
if not self.disable_search:
regex_patterns_to_include = globs_to_regex(files_patterns)
assert regex_patterns_to_include # this is never `None`
files_paths = search_files(
self.search_root,
default_folders_to_ignore + [str(folder) for folder in self.folders_to_ignore],
default_folders_to_ignore
+ [str(folder) for folder in self.folders_to_ignore],
filename_include_regex=regex_patterns_to_include,
filename_exclude_regex=regex_patterns_to_exclude,
)
result_files = [UploadCollectionResultFile(path) for path in files_paths]
user_result_files = [
UploadCollectionResultFile(path)
for path in user_files_paths
if user_files_paths
]

user_result_files = []
for path in user_files_paths:
if os.path.isfile(path):
user_result_files.append(UploadCollectionResultFile(path))
else:
logger.warning(
f"File \"{path}\" could not be found or does not exist. Please enter in the full path or from the search root \"{self.search_root}\"",
f'File "{path}" could not be found or does not exist. Please enter in the full path or from the search root "{self.search_root}"',
)

return list(set(result_files + user_result_files))
Expand Down Expand Up @@ -287,7 +296,7 @@ def select_file_finder(
folders_to_ignore,
explicitly_listed_files,
disable_search,
report_type="coverage",
report_type: ReportType = ReportType.COVERAGE,
):
return FileFinder(
root_folder_to_search,
Expand Down
19 changes: 15 additions & 4 deletions codecov_cli/services/upload/upload_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import click
import sentry_sdk

from codecov_cli.helpers.upload_type import ReportType
from codecov_cli.services.upload.file_finder import FileFinder
from codecov_cli.services.upload.network_finder import NetworkFinder
from codecov_cli.types import (
Expand Down Expand Up @@ -149,7 +150,9 @@ def _get_file_fixes(
path, fixed_lines_without_reason, fixed_lines_with_reason, eof
)

def generate_upload_data(self, report_type="coverage") -> UploadCollectionResult:
def generate_upload_data(
self, report_type: ReportType = ReportType.COVERAGE
) -> UploadCollectionResult:
with sentry_sdk.start_span(name="upload_collector"):
for prep in self.preparation_plugins:
logger.debug(f"Running preparation plugin: {type(prep)}")
Expand All @@ -158,10 +161,18 @@ def generate_upload_data(self, report_type="coverage") -> UploadCollectionResult
with sentry_sdk.start_span(name="file_collector"):
network = self.network_finder.find_files()
report_files = self.file_finder.find_files()
logger.info(f"Found {len(report_files)} {report_type} files to report")
logger.info(
f"Found {len(report_files)} {report_type.value} files to report"
)
if not report_files:
if report_type == "test_results":
if report_type == ReportType.TEST_RESULTS:
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."
logger.error(error_message)
return UploadCollectionResult(
network=network,
files=[],
file_fixes=[],
)
else:
error_message = "No coverage reports found. Please make sure you're generating reports successfully."
raise click.ClickException(
Expand All @@ -177,7 +188,7 @@ def generate_upload_data(self, report_type="coverage") -> UploadCollectionResult
files=report_files,
file_fixes=(
self._produce_file_fixes(self.network_finder.find_files(True))
if report_type == "coverage"
if report_type == ReportType.COVERAGE
else []
),
)
Loading
Loading