Skip to content

Commit

Permalink
Addressing the issue of PRs not targetting the default branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed Sep 10, 2023
1 parent 492b997 commit ea13912
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
21 changes: 16 additions & 5 deletions coverage_comment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions coverage_comment/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
16 changes: 14 additions & 2 deletions coverage_comment/template_files/comment.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down

0 comments on commit ea13912

Please sign in to comment.