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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion codecov_cli/commands/process_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -100,7 +101,7 @@ def process_test_results(
github_token=None,
):
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
9 changes: 6 additions & 3 deletions codecov_cli/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import os
import pathlib
import typing

import click

from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers.args import get_cli_args
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 @@ -166,6 +166,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 @@ -240,7 +241,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 @@ -258,6 +259,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 Down Expand Up @@ -293,7 +296,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 @@ -9,6 +9,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.services.upload_coverage import upload_coverage_logic
from codecov_cli.types import CommandContext

Expand Down Expand Up @@ -54,7 +55,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 @@ -68,7 +69,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 @@ -112,7 +115,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 @@ -128,7 +131,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 @@ -167,7 +170,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 @@ -9,6 +9,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 @@ -53,7 +54,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 @@ -78,7 +79,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 @@ -117,7 +120,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}")
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
11 changes: 6 additions & 5 deletions codecov_cli/services/upload/file_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Iterable, List, Optional, Pattern

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 @@ -189,19 +190,19 @@ def __init__(
folders_to_ignore: Optional[List[str]] = 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 = folders_to_ignore or []
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]:
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)
Expand Down Expand Up @@ -278,7 +279,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
27 changes: 18 additions & 9 deletions codecov_cli/services/upload/upload_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import click

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 @@ -148,33 +149,41 @@ 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:
for prep in self.preparation_plugins:
logger.debug(f"Running preparation plugin: {type(prep)}")
prep.run_preparation(self)
logger.debug("Collecting relevant files")
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(
click.style(
error_message,
fg="red",
raise click.ClickException(
click.style(
error_message,
fg="red",
)
)
)
for file in report_files:
logger.info(f"> {file}")
return UploadCollectionResult(
network=network,
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