Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Note: Either `GITHUB_PR_NUMBER` or `GITHUB_REF` is required. `GITHUB_PR_NUMBER`
- `MAX_FILES_IN_COMMENT`: The maximum number of files to include in the coverage report comment. Default is 25.
- `SKIP_COVERED_FILES_IN_REPORT`: Skip the files with coverage 100% from the report. Default is True.
- `COMPLETE_PROJECT_REPORT`: Whether to include the complete project coverage report in the comment. Default is False.
- `COVERAGE_REPORT_URL`: URL of the full coverage report to mention in the comment.
- `LABEL`: Optional text rendered in the comment footer. Default is unset (no footer).
- `DEBUG`: Whether to enable debug mode. Default is False.

## Notes
Expand Down
1 change: 0 additions & 1 deletion codecov/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class Config:
MAX_FILES_IN_COMMENT: int = 25
SKIP_COVERED_FILES_IN_REPORT: bool = True
COMPLETE_PROJECT_REPORT: bool = False
COVERAGE_REPORT_URL: str | None = None
LABEL: str | None = None
DEBUG: bool = False

Expand Down
1 change: 0 additions & 1 deletion codecov/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def _create_comment(self) -> None:
self.marker,
branch_coverage=self.config.BRANCH_COVERAGE,
complete_project_report=self.config.COMPLETE_PROJECT_REPORT,
coverage_report_url=self.config.COVERAGE_REPORT_URL,
max_files=self.config.MAX_FILES_IN_COMMENT,
files=diff_files_info,
count_files=diff_count_files,
Expand Down
14 changes: 3 additions & 11 deletions codecov/template_files/comment.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
{#- Project coverage badge -#}
{%- block coverage_evolution_badge -%}
{%- if coverage %}
{%- set text = "The project coverage is at " ~ coverage.info.percent_covered_display ~ "%." -%}
{%- set color = coverage.info.percent_covered | x100 | get_badge_color -%}
<img title="{{ text }}" src="{{ 'Coverage' | generate_badge(message=coverage.info.percent_covered_display ~ '%', color=color) }}">
<img src="{{ 'Coverage' | generate_badge(message=coverage.info.percent_covered_display ~ '%', color=color) }}">
{%- endif -%}
{%- endblock coverage_evolution_badge -%}
&nbsp;&nbsp;
{#- PR coverage badge -#}
{%- block diff_coverage_badge -%}
{%- set text = (diff_coverage.total_percent_covered | pct) ~ " of the statements added in this PR are covered." -%}
{%- set color = diff_coverage.total_percent_covered | x100 | get_badge_color -%}
<img title="{{ text }}" src="{{ 'PR Coverage' | generate_badge(message=diff_coverage.total_percent_covered | pct(precision=0), color=color) }}">
<img src="{{ 'PR Coverage' | generate_badge(message=diff_coverage.total_percent_covered | pct(precision=0), color=color) }}">
{%- endblock diff_coverage_badge -%}
{%- endblock coverage_badges -%}

Expand All @@ -32,16 +30,10 @@
{% include "project.md.j2" %}
{%- endblock project_coverage_by_file %}

{%- block full_coverage_report_link -%}
{%- if coverage_report_url %}
<h4> See the full coverage report of the project <a href="{{ coverage_report_url }}">here</a>.</h4>
{%- endif -%}
{%- endblock full_coverage_report_link %}

{%- block footer %}
{% if label %}
<sub>
This report was generated by <strong>{{ label }}</strong>
{{ label }}
</sub>
{% endif %}
{% endblock footer -%}
Expand Down
108 changes: 73 additions & 35 deletions codecov/template_files/macros.md.j2
Original file line number Diff line number Diff line change
@@ -1,54 +1,92 @@
{#-
Every badge cell is a shields.io image linked to the file, so the wrapper is
factored out into `badge`. Cells no longer carry their own
<td align="center">: in a GitHub Flavoured Markdown table the alignment
belongs to the delimiter row of the table.
-#}
{%- macro badge(path, label, message, color, base=false) -%}
[![]({{ label | generate_badge(message=message, color=color) }})]({{ path | file_url(base=base) }})
{%- endmacro -%}

{%- macro statements_badge(path, statements_count, base=false) -%}
{% set text = "The " ~ path ~ " contains " ~ statements_count ~ " statement" ~ (statements_count | pluralize) ~"." -%}
{% set color = "007ec6" -%}
<td align="center"><a href="{{ path | file_url(base=base) }}"><img title="{{ text }}" src="{{ '' | generate_badge(message=(statements_count | string), color=color) }}"></a></td>
{{- badge(path, "", statements_count | string, "007ec6", base) -}}
{%- endmacro -%}

{%- macro branches_badge(path, branches_count, base=false) -%}
{% set text = "The " ~ path ~ " contains " ~ branches_count ~ " branch" ~ (branches_count | pluralize(plural='es')) ~"." -%}
{% set color = "008080" -%}
<td align="center"><a href="{{ path | file_url(base=base) }}"><img title="{{ text }}" src="{{ '' | generate_badge(message=(branches_count | string), color=color) }}"></a></td>
{{- badge(path, "", branches_count | string, "008080", base) -}}
{%- endmacro -%}

{%- macro missing_branches_badge(path, missing_branches_count, base=false) -%}
{%- set text = missing_branches_count ~ " branch" ~ (missing_branches_count | pluralize(plural='es')) ~ " missing the coverage in " ~ path ~ "." -%}
{% if missing_branches_count == 0 -%}
{%- set color = "brightgreen" -%}
{% else -%}
{%- set color = "red" -%}
{% endif -%}
<td align="center"><a href="{{ path | file_url(base=base) }}"><img title="{{ text }}" src="{{ '' | generate_badge(message=(missing_branches_count | string), color=color) }}"></a></td>
{{- badge(path, "", missing_branches_count | string, "brightgreen" if missing_branches_count == 0 else "red", base) -}}
{%- endmacro -%}

{%- macro missing_lines_badge(path, missing_lines_count, base=false) -%}
{%- set text = missing_lines_count ~ " statement" ~ (statements_count | pluralize) ~ " missing the coverage in " ~ path ~ "." -%}
{% if missing_lines_count == 0 -%}
{%- set color = "brightgreen" -%}
{% else -%}
{%- set color = "red" -%}
{% endif -%}
<td align="center"><a href="{{ path | file_url(base=base) }}"><img title="{{ text }}" src="{{ '' | generate_badge(message=(missing_lines_count | string), color=color) }}"></a></td>
{{- badge(path, "", missing_lines_count | string, "brightgreen" if missing_lines_count == 0 else "red", base) -}}
{%- endmacro -%}

{%- macro coverage_rate_badge(path, percent_covered, percent_covered_display, covered_statements_count, statements_count, base=false) -%}
{%- set text = "The coverage rate of " ~ path ~ " is " ~ percent_covered_display ~ "% (" ~ covered_statements_count ~ "/" ~ statements_count ~ ")." -%}
{%- set label = percent_covered_display ~ "%" -%}
{%- set message = "(" ~ covered_statements_count ~ "/" ~ statements_count ~ ")" -%}
{%- set color = percent_covered | x100 | get_badge_color -%}
<td align="center"><a href="{{ path | file_url(base=base) }}"><img title="{{ text }}" src="{{ label | generate_badge(message=message, color=color) }}"></a></td>
{{- badge(path, label, message, percent_covered | x100 | get_badge_color, base) -}}
{%- endmacro -%}

{%- macro diff_coverage_rate_badge(path, added_statements_count, covered_statements_count, percent_covered) -%}
{% if added_statements_count -%}
{% set text = "In this PR, " ~ (added_statements_count) ~ " new statement" ~ (added_statements_count | pluralize) ~ " " ~ (added_statements_count | pluralize(singular='is', plural='are')) ~ " added to " ~ path ~ ", and " ~ covered_statements_count ~ " statement" ~ (covered_statements_count | pluralize) ~ " "~ (covered_statements_count | pluralize(singular='is', plural='are')) ~ " covered (" ~ (percent_covered | pct) ~ ")." -%}
{% set label = (percent_covered | pct(precision=0)) -%}
{% set message = "(" ~ covered_statements_count ~ "/" ~ added_statements_count ~ ")" -%}
{%- set color = (percent_covered | x100 | get_badge_color()) -%}
{% else -%}
{% set text = "This PR does not seem to add statements to " ~ path ~ "." -%}
{% set label = "" -%}
{%- set color = "grey" -%}
{% set message = "N/A" -%}
{% endif -%}
<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ label | generate_badge(message=message, color=color) }}"></a></td>
{%- if added_statements_count -%}
{%- set label = (percent_covered | pct(precision=0)) -%}
{%- set message = "(" ~ covered_statements_count ~ "/" ~ added_statements_count ~ ")" -%}
{{- badge(path, label, message, percent_covered | x100 | get_badge_color()) -}}
{%- else -%}
{{- badge(path, "", "N/A", "grey") -}}
{%- endif -%}
{%- endmacro -%}

{%- macro missing_lines_links(path, groups, base=false) -%}
{%- set comma = joiner() -%}
{%- for group in groups -%}
{{- comma() -}}
[{{ group.line_start }}{% if group.line_start != group.line_end %}-{{ group.line_end }}{% endif %}]({{ path | file_url(lines=(group.line_start, group.line_end), base=base) }})
{%- endfor -%}
{%- endmacro -%}

{%- macro missing_branches_links(path, branches, base=false) -%}
{%- set comma = joiner() -%}
{%- for branch in branches -%}
{{- comma() -}}
[{{ branch[0] | abs }} -> {{ branch[1] | abs }}]({{ path | file_url(lines=(branch[0] | abs, branch[1] | abs), base=base) }})
{%- endfor -%}
{%- endmacro -%}

{#-
The pull request table and the whole project table only differ by the column
holding the coverage of the newly added statements, by the links pointing at
the pull request diff instead of the base branch, and by the set of missing
line groups, so they share the three macros below.

The em spaces keep the original column widths: GitHub compresses a table that
overflows the width of a comment, and Markdown has no syntax for a column
width, so the header cell is what reserves the room.
-#}
{%- macro table_header(branch_coverage, with_diff) -%}
| File | Statements | Missing |{% if branch_coverage %} Branches | Missing |{% endif %} <br>Coverage &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |{% if with_diff %} Coverage &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;<br>(new stmts) |{% endif %} <br>Missing stmts &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |{% if branch_coverage %} <br>Missing branches &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; |{% endif %}
| :-- | :-: | :-: |{% if branch_coverage %} :-: | :-: |{% endif %} :-: |{% if with_diff %} :-: |{% endif %} :-- |{% if branch_coverage %} :-- |{% endif %}
{%- endmacro -%}

{%- macro file_row(file, missing_map, branch_coverage, base, with_diff) -%}
{%- set path = file.coverage.path -%}
{%- set info = file.coverage.info -%}
| &nbsp;&nbsp;[{{ path.name }}]({{ path | file_url(base=base) }}) | {{ statements_badge(path, info.num_statements, base) }} | {{ missing_lines_badge(path, info.missing_lines, base) }} |{% if branch_coverage %} {{ branches_badge(path, info.num_branches, base) }} | {{ missing_branches_badge(path, info.missing_branches, base) }} |{% endif %} {{ coverage_rate_badge(path, info.percent_covered, info.percent_covered_display, info.covered_lines, info.num_statements, base) }} |{% if with_diff %} {{ diff_coverage_rate_badge(path, (file.diff.added_statements | length) if file.diff else none, (file.diff.covered_statements | length) if file.diff else none, file.diff.percent_covered if file.diff else none) }} |{% endif %} {{ missing_lines_links(path, missing_map.get(path, []), base) }} |{% if branch_coverage %} {{ missing_branches_links(path, file.coverage.missing_branches or [], base) }} |{% endif %}
{%- endmacro -%}

{%- macro total_row(totals, diff_totals, branch_coverage, with_diff) -%}
| **Project Total** | {{ statements_badge("whole project", totals.num_statements) }} | {{ missing_lines_badge("the whole project", totals.missing_lines) }} |{% if branch_coverage %} {{ branches_badge("the whole project", totals.num_branches) }} | {{ missing_branches_badge("the whole project", totals.missing_branches) }} |{% endif %} {{ coverage_rate_badge("the whole project", totals.percent_covered, totals.percent_covered_display, totals.covered_lines, totals.num_statements) }} |{% if with_diff %} {{ diff_coverage_rate_badge("the whole project", diff_totals.total_num_lines, diff_totals.total_num_lines - diff_totals.total_num_violations, diff_totals.total_percent_covered) }} |{% endif %} &nbsp; |{% if branch_coverage %} &nbsp; |{% endif %}
{%- endmacro -%}

{#- `diff_totals` is the DiffCoverage object for the pull request table, or none
for the whole project table, which has no newly added statements. -#}
{%- macro coverage_table(files, missing_map, totals, diff_totals, branch_coverage=false, base=false) -%}
{%- set with_diff = diff_totals is not none -%}
{{ table_header(branch_coverage, with_diff) }}
{% for parent, files_in_folder in files | groupby(attribute="path.parent") %}| &nbsp;&nbsp;**{{ parent }}** |
{% for file in files_in_folder %}{{ file_row(file, missing_map, branch_coverage, base, with_diff) }}
{% endfor %}{% endfor %}{{ total_row(totals, diff_totals, branch_coverage, with_diff) }}
{%- endmacro -%}
181 changes: 2 additions & 179 deletions codecov/template_files/pr.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,188 +4,11 @@
{%- else -%}
<details><summary>Click to see the coverage of changed files</summary>
<br>
<table>
<thead>
<tr><th>File</th><th>Statements</th><th>Missing</th>{% if branch_coverage %}<th>Branches</th><th>Missing</th>{% endif %}
<th><br>Coverage &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;</th><th>Coverage &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;<br>(new stmts)</th>
<th><br>Missing stmts &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;</th>
{% if branch_coverage %}<th><br>Missing branches &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;</th>{% endif %}</tr>
</thead>
<tbody>

{%- for parent, files_in_folder in files|groupby(attribute="path.parent") -%}
<tr>
<td colspan="{%if branch_coverage%}9{% else -%}6{% endif %}">&nbsp;&nbsp;<b>{{ parent }}</b></td>
</tr>

{%- for file in files_in_folder -%}
{%- set path = file.coverage.path -%}
<tr>
<td>&nbsp;&nbsp;<a href="{{ path | file_url }}">{{ path.name }}</a></td>

{#- Statements cell -#}
{%- block statements_badge_cell scoped -%}
{{- macros.statements_badge(
path=path,
statements_count=file.coverage.info.num_statements,
) -}}
{%- endblock statements_badge_cell-%}

{#- Missing cell -#}
{%- block missing_lines_badge_cell scoped -%}
{{- macros.missing_lines_badge(
path=path,
missing_lines_count=file.coverage.info.missing_lines,
) -}}
{%- endblock missing_lines_badge_cell -%}

{% if branch_coverage %}
{#- Branches cell -#}
{%- block branches_badge_cell scoped -%}
{{- macros.branches_badge(
path=path,
branches_count=file.coverage.info.num_branches,
) -}}
{%- endblock branches_badge_cell -%}

{#- Missing cell -#}
{%- block missing_branches_badge_cell scoped -%}
{{- macros.missing_branches_badge(
path=path,
missing_branches_count=file.coverage.info.missing_branches,
) -}}
{%- endblock missing_branches_badge_cell -%}
{% endif %}

{#- Coverage rate -#}
{%- block coverage_rate_badge_cell scoped -%}
{{- macros.coverage_rate_badge(
path=path,
percent_covered=file.coverage.info.percent_covered,
percent_covered_display=file.coverage.info.percent_covered_display,
covered_statements_count=file.coverage.info.covered_lines,
statements_count=file.coverage.info.num_statements,
) -}}
{%- endblock coverage_rate_badge_cell -%}

{#- Coverage of added lines (new stmts) -#}
{%- block diff_coverage_rate_badge_cell scoped -%}
{{- macros.diff_coverage_rate_badge(
path=path,
added_statements_count=((file.diff.added_statements | length) if file.diff else none),
covered_statements_count=((file.diff.covered_statements | length) if file.diff else none),
percent_covered=(file.diff.percent_covered if file.diff else none)
) -}}
{%- endblock diff_coverage_rate_badge_cell -%}

{#- Link to lines missing -#}
{%- block link_to_missing_diff_lines_cell scoped -%}
<td>

{%- set comma = joiner() -%}
{%- for group in missing_diff_lines.get(path, []) -%}
{{- comma() -}}
<a href="{{ path | file_url(lines=(group.line_start, group.line_end))}}">

{{- group.line_start -}}
{%- if group.line_start != group.line_end -%}
-
{{- group.line_end -}}
{%- endif -%}
</a>

{%- endfor -%}
</td>

{%- endblock link_to_missing_diff_lines_cell -%}

{#- Link to branch missing lines -#}
{%- if branch_coverage -%}
{%- block link_to_branches_missing_lines_cell scoped -%}
<td>
{%- set comma = joiner() -%}
{%- for branch in file.coverage.missing_branches -%}
{{- comma() -}}
<a href="{{ path | file_url(lines=(branch[0] | abs, branch[1] | abs))}}">{{- branch[0] | abs -}} -> {{- branch[1] | abs -}}</a>
{%- endfor -%}
</td>
{%- endblock link_to_branches_missing_lines_cell -%}
{%- endif -%}
</tr>

{%- endfor -%}
{%- endfor -%}
</tbody>
<tfoot>
<tr>
<td><b>Project Total</b></td>


{#- Statements cell -#}
{%- block statements_badge_total_cell scoped -%}
{{- macros.statements_badge(
path="whole project",
statements_count=coverage.info.num_statements,
) -}}
{%- endblock statements_badge_total_cell -%}

{#- Missing cell -#}
{%- block missing_lines_badge_total_cell scoped -%}
{{- macros.missing_lines_badge(
path="the whole project",
missing_lines_count=coverage.info.missing_lines,
) -}}
{%- endblock missing_lines_badge_total_cell -%}

{% if branch_coverage %}
{#- Branches cell -#}
{%- block branches_badge_total_cell scoped -%}
{{- macros.branches_badge(
path="the whole project",
branches_count=coverage.info.num_branches,
) -}}
{%- endblock branches_badge_total_cell -%}

{#- Missing cell -#}
{%- block missing_branches_badge_total_cell scoped -%}
{{- macros.missing_branches_badge(
path="the whole project",
missing_branches_count=coverage.info.missing_branches,
) -}}
{%- endblock missing_branches_badge_total_cell -%}
{% endif %}

{#- Coverage rate -#}
{%- block coverage_rate_badge_total_cell scoped -%}
{{- macros.coverage_rate_badge(
path="the whole project",
percent_covered=coverage.info.percent_covered,
percent_covered_display=coverage.info.percent_covered_display,
covered_statements_count=coverage.info.covered_lines,
statements_count=coverage.info.num_statements,
) -}}
{%- endblock coverage_rate_badge_total_cell -%}

{# Coverage of added lines #}
{%- block diff_coverage_rate_badge_total_cell scoped -%}
{{- macros.diff_coverage_rate_badge(
path="the whole project",
added_statements_count=diff_coverage.total_num_lines,
covered_statements_count=(diff_coverage.total_num_lines-diff_coverage.total_num_violations),
percent_covered=diff_coverage.total_percent_covered,
) -}}
{%- endblock diff_coverage_rate_badge_total_cell -%}

<td>&nbsp;</td>
{% if branch_coverage %}
<td>&nbsp;</td>
{% endif %}
</tr>
</tfoot>
</table>
{{ macros.coverage_table(files, missing_diff_lines, coverage.info, diff_coverage, branch_coverage, false) }}

{%- if max_files and count_files > max_files %}
_The report is truncated to {{ max_files }} files out of {{ count_files }}.
_The report is truncated to {{ max_files }} files out of {{ count_files }}._
{% endif %}

</details>
Expand Down
Loading
Loading