Skip to content
Closed
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
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ inputs:
description: >
If true, will create an annotation on every line with missing coverage on a pull request.
default: false
INCLUDE_PLAIN_TEXT_REPORT:
description: >
If true, will include a plain-text markdown coverage table in the job summary,
suitable for pasting into text-based tools. Only applies in PR mode.
default: false
ANNOTATION_TYPE:
description: >
Only relevant if ANNOTATE_MISSING_LINES is set to true. This parameter allows you to choose between
Expand Down Expand Up @@ -182,4 +187,5 @@ runs:
MERGE_COVERAGE_FILES: ${{ inputs.MERGE_COVERAGE_FILES }}
ANNOTATE_MISSING_LINES: ${{ inputs.ANNOTATE_MISSING_LINES }}
ANNOTATION_TYPE: ${{ inputs.ANNOTATION_TYPE }}
INCLUDE_PLAIN_TEXT_REPORT: ${{ inputs.INCLUDE_PLAIN_TEXT_REPORT }}
VERBOSE: ${{ inputs.VERBOSE }}
22 changes: 22 additions & 0 deletions coverage_comment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@
except github.CannotDeterminePR:
pr_number = None

plain_text_report: str | None = None
if config.INCLUDE_PLAIN_TEXT_REPORT:
try:
plain_text_report = template.get_plain_text_markdown(
coverage=coverage,
diff_coverage=diff_coverage,
previous_coverage=previous_coverage,
previous_coverage_rate=previous_coverage_rate,
files=files_info,
count_files=count_files,
max_files=None,
subproject_id=config.SUBPROJECT_ID,
failure_msg=failure_msg,
)
except template.TemplateError:
log.warning(

Check warning on line 223 in coverage_comment/main.py

View workflow job for this annotation

GitHub Actions / Run tests & display coverage

Missing coverage

Missing coverage on lines 210-223
"There was a rendering error when computing the plain-text report. "
"The plain-text output will not be available."
)

try:
comment = template.get_comment_markdown(
coverage=coverage,
Expand All @@ -226,6 +246,7 @@
marker=marker,
subproject_id=config.SUBPROJECT_ID,
failure_msg=failure_msg,
plain_text_report=plain_text_report,
)
# Same as above except `max_files` is None
summary_comment = template.get_comment_markdown(
Expand All @@ -248,6 +269,7 @@
marker=marker,
subproject_id=config.SUBPROJECT_ID,
failure_msg=failure_msg,
plain_text_report=plain_text_report,
)
except template.MissingMarker:
log.error(
Expand Down
5 changes: 5 additions & 0 deletions coverage_comment/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
MERGE_COVERAGE_FILES: bool = False
ANNOTATE_MISSING_LINES: bool = False
ANNOTATION_TYPE: str = "warning"
INCLUDE_PLAIN_TEXT_REPORT: bool = False
MAX_FILES_IN_COMMENT: int = 25
USE_GH_PAGES_HTML_URL: bool = False
VERBOSE: bool = False
Expand Down Expand Up @@ -95,6 +96,10 @@
def clean_annotate_missing_lines(cls, value: str) -> bool:
return str_to_bool(value)

@classmethod
def clean_include_plain_text_report(cls, value: str) -> bool:
return str_to_bool(value)

Check warning on line 101 in coverage_comment/settings.py

View workflow job for this annotation

GitHub Actions / Run tests & display coverage

Missing coverage

Missing coverage on line 101

@classmethod
def clean_annotation_type(cls, value: str) -> str:
if value not in {"notice", "warning", "error"}:
Expand Down
44 changes: 44 additions & 0 deletions coverage_comment/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
custom_template: str | None = None,
pr_targets_default_branch: bool = True,
failure_msg: str | None = None,
plain_text_report: str | None = None,
):
loader = CommentLoader(base_template=base_template, custom_template=custom_template)
env = SandboxedEnvironment(loader=loader)
Expand Down Expand Up @@ -190,6 +191,7 @@
marker=marker,
pr_targets_default_branch=pr_targets_default_branch,
failure_msg=failure_msg,
plain_text_report=plain_text_report,
)
except jinja2.exceptions.TemplateError as exc:
raise TemplateError from exc
Expand All @@ -200,6 +202,48 @@
return comment


def get_plain_text_markdown(
*,
coverage: coverage_module.Coverage,
diff_coverage: coverage_module.DiffCoverage,
previous_coverage_rate: decimal.Decimal | None,
previous_coverage: coverage_module.Coverage | None,
files: list[FileInfo],
max_files: int | None,
count_files: int,
subproject_id: str | None = None,
failure_msg: str | None = None,
) -> str:
env = SandboxedEnvironment()
env.filters["pct"] = pct

missing_diff_lines = {
key: list(value)
for key, value in itertools.groupby(
diff_grouper.get_diff_missing_groups(
coverage=coverage, diff_coverage=diff_coverage
),
lambda x: x.file,
)
}

try:
return env.from_string(read_template_file("comment_plain.md.j2")).render(
previous_coverage_rate=previous_coverage_rate,
coverage=coverage,
diff_coverage=diff_coverage,
previous_coverage=previous_coverage,
count_files=count_files,
max_files=max_files,
files=files,
missing_diff_lines=missing_diff_lines,
subproject_id=subproject_id,
failure_msg=failure_msg,
)
except jinja2.exceptions.TemplateError as exc:
raise TemplateError from exc

Check warning on line 244 in coverage_comment/template.py

View workflow job for this annotation

GitHub Actions / Run tests & display coverage

Missing coverage

Missing coverage on lines 243-244


def select_files(
*,
coverage: coverage_module.Coverage,
Expand Down
7 changes: 7 additions & 0 deletions coverage_comment/template_files/comment.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,11 @@ This report was generated by [python-coverage-comment-action](https://github.com
{%- endif -%}
{%- endblock coverage_by_file %}

{% if plain_text_report %}
<details><summary>Plain-text coverage table (for copy-paste)</summary>

{{ plain_text_report }}
</details>

{% endif -%}
{{ marker -}}
49 changes: 49 additions & 0 deletions coverage_comment/template_files/comment_plain.md.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{%- if subproject_id %}## Plain-text coverage report ({{ subproject_id }}){% else %}## Plain-text coverage report{% endif %}

**Overall coverage:** {{ coverage.info.percent_covered | pct }}{% if previous_coverage_rate %} (was {{ previous_coverage_rate | pct }}){% endif %}

{% if failure_msg -%}
> WARNING: {{ failure_msg }}

**Diff coverage:** N/A
{%- else -%}
**Diff coverage:** {{ diff_coverage.total_percent_covered | pct }} ({{ diff_coverage.total_num_lines - diff_coverage.total_num_violations }}/{{ diff_coverage.total_num_lines }} new statements covered)
{%- endif %}
{% if not files %}

_This PR does not seem to contain any modification to coverable code._
{%- else %}

| File | Stmts | Missing | Coverage | New stmts coverage | Missing lines |
| ---- | ----: | ------: | -------: | -----------------: | ------------- |
{%- for parent, files_in_folder in files|groupby(attribute="path.parent") %}
{%- for file in files_in_folder %}
{%- set path = file.coverage.path %}
{%- if file.previous %}
{%- set cov_str = (file.previous.info.percent_covered | pct) ~ " -> " ~ (file.coverage.info.percent_covered | pct) %}
{%- else %}
{%- set cov_str = file.coverage.info.percent_covered | pct %}
{%- endif %}
{%- if file.diff and file.diff.added_statements %}
{%- set diff_str = (file.diff.percent_covered | pct) ~ " (" ~ (file.diff.covered_statements | length) ~ "/" ~ (file.diff.added_statements | length) ~ ")" %}
{%- else %}
{%- set diff_str = "N/A" %}
{%- endif %}
{%- set comma = joiner(", ") %}
{%- set ns = namespace(missing="") %}
{%- for group in missing_diff_lines.get(path, []) %}
{%- if group.line_start == group.line_end %}
{%- set ns.missing = ns.missing ~ comma() ~ (group.line_start | string) %}
{%- else %}
{%- set ns.missing = ns.missing ~ comma() ~ (group.line_start | string) ~ "-" ~ (group.line_end | string) %}
{%- endif %}
{%- endfor %}
| {{ path }} | {{ file.coverage.info.num_statements }} | {{ file.coverage.info.missing_lines }} | {{ cov_str }} | {{ diff_str }} | {{ ns.missing }} |
{%- endfor %}
{%- endfor %}
| **Total** | {{ coverage.info.num_statements }} | {{ coverage.info.missing_lines }} | {% if previous_coverage %}{{ previous_coverage.info.percent_covered | pct }} -> {% endif %}{{ coverage.info.percent_covered | pct }} | {{ diff_coverage.total_percent_covered | pct }} ({{ diff_coverage.total_num_lines - diff_coverage.total_num_violations }}/{{ diff_coverage.total_num_lines }}) | |
{%- if max_files and count_files > max_files %}

_Report truncated to {{ max_files }} files out of {{ count_files }}. See the workflow summary for the full report._
{%- endif %}
{%- endif %}
Loading
Loading