diff --git a/coverage_comment/main.py b/coverage_comment/main.py index a354d6e3..513edbc3 100644 --- a/coverage_comment/main.py +++ b/coverage_comment/main.py @@ -119,11 +119,21 @@ def process_pr( base_ref=base_ref, coverage_path=config.COVERAGE_PATH ) - previous_coverage_data_file = storage.get_datafile_contents( - github=gh, - repository=config.GITHUB_REPOSITORY, - branch=config.COVERAGE_DATA_BRANCH, - ) + # It only really makes sense to display a comparison with the previous + # coverage if the PR target is the branch in which the coverage data is + # stored, e.g. the default branch. + # In the case we're running on a branch without a PR yet, we can't know + # if it's going to target the default branch, so we display it. + previous_coverage_data_file = None + pr_targets_default_branch = base_ref == repo_info.default_branch + + if pr_targets_default_branch: + previous_coverage_data_file = storage.get_datafile_contents( + github=gh, + repository=config.GITHUB_REPOSITORY, + branch=config.COVERAGE_DATA_BRANCH, + ) + previous_coverage = None if previous_coverage_data_file: previous_coverage = files.parse_datafile(contents=previous_coverage_data_file) @@ -135,6 +145,7 @@ def process_pr( previous_coverage_rate=previous_coverage, base_template=template.read_template_file("comment.md.j2"), custom_template=config.COMMENT_TEMPLATE, + pr_targets_default_branch=pr_targets_default_branch, ) except template.MissingMarker: log.error( diff --git a/coverage_comment/template.py b/coverage_comment/template.py index 8cf83381..f212204d 100644 --- a/coverage_comment/template.py +++ b/coverage_comment/template.py @@ -45,6 +45,7 @@ def get_comment_markdown( previous_coverage_rate: decimal.Decimal | None, base_template: str, custom_template: str | None = None, + pr_targets_default_branch: bool = True, ): loader = CommentLoader(base_template=base_template, custom_template=custom_template) env = SandboxedEnvironment(loader=loader) @@ -56,6 +57,7 @@ def get_comment_markdown( coverage=coverage, diff_coverage=diff_coverage, marker=MARKER, + pr_targets_default_branch=pr_targets_default_branch, ) except jinja2.exceptions.TemplateError as exc: raise TemplateError from exc diff --git a/coverage_comment/template_files/comment.md.j2 b/coverage_comment/template_files/comment.md.j2 index 95543145..1151d57c 100644 --- a/coverage_comment/template_files/comment.md.j2 +++ b/coverage_comment/template_files/comment.md.j2 @@ -15,8 +15,20 @@ The coverage rate went from `{{ previous_coverage_rate | pct }}` to `{{ coverage {%- endblock emoji_coverage -%} {%- else -%} {% block no_comparison_info -%} -> **Note** -> No coverage data of the default branch was found for comparison. A possible reason for this is that the coverage action has not yet run after a push event and the data is therefore not yet initialized. +{%- if pr_targets_default_branch -%} +{% block no_comparison_info_data_not_found_message -%} +> [!NOTE] +> Coverage data for the default branch was not found. +> This usually happens when the action has not run on the default +> branch yet, for example right after deploying it into the workflows. +{%- endblock no_comparison_info_data_not_found_message -%} +{% else %} +{% block no_comparison_info_non_default_target -%} +> [!NOTE] +> Coverage evolution disabled because this PR targets a different branch +> than the default branch, for which coverage data is not available. +{%- endblock no_comparison_info_non_default_target -%} +{% endif %} {%- endblock no_comparison_info %} {% block coverage_value_wording -%}