Skip to content

Commit

Permalink
chore: Generate coverage badge alongside reports. (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
huxuan authored Sep 4, 2023
1 parent 155fbec commit 59cae68
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 14 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ jobs:
- run: env | sort
- run: make dev-test
- run: make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
flags: ${{ matrix.python-version }}
strategy:
matrix:
python-version:
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"autofix",
"automodule",
"cobertura",
"codecov",
"elif",
"endmacro",
"epub",
Expand Down Expand Up @@ -83,6 +82,7 @@
"author_email",
"repo_host_type",
"repo_host",
"page_host",
"ci",
"repo_namespace",
"repo_name",
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ docs-autobuild:
docs-mypy:
${PIPRUN} python -m mypy src test --html-report ${PUBLIC_DIR}/reports/mypy

# Generate coverage reports.
# Generate coverage reports and badge.
docs-coverage:
${PIPRUN} python -m coverage erase
${PIPRUN} python -m coverage run -m pytest
${PIPRUN} python -m coverage html -d ${PUBLIC_DIR}/reports/coverage
${PIPRUN} bash scripts/generate-coverage-badge.sh ${PUBLIC_DIR}/reports/coverage

# Generate all reports.
reports: docs-mypy docs-coverage
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A development-focused Python project template with various integrations, configurations and modules.

[![CI Status](https://github.com/serious-scaffold/serious-scaffold-python/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/serious-scaffold/serious-scaffold-python/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/serious-scaffold/serious-scaffold-python/branch/main/graph/badge.svg?token=4JPKXI122N)](https://codecov.io/gh/serious-scaffold/serious-scaffold-python)
[![Coverage](https://img.shields.io/endpoint?url=https://serious-scaffold.github.io/serious-scaffold-python/reports/coverage/coverage_badge.json)](https://serious-scaffold.github.io/serious-scaffold-python/reports/coverage)
[![Documentation Status](https://readthedocs.org/projects/serious-scaffold-python/badge/)](https://serious-scaffold-python.readthedocs.io/)
[![GitHub](https://img.shields.io/github/license/serious-scaffold/serious-scaffold-python)](https://github.com/serious-scaffold/serious-scaffold-python/blob/main/LICENSE)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/serious-scaffold)](https://pypi.org/project/serious-scaffold/)
Expand Down
10 changes: 10 additions & 0 deletions copier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ repo_host:
help: 'Specify the host of the self-managed GitLab:'
type: str
when: '{{ repo_host_type == "gitlab-self-managed" }}'
page_host:
default: |-
[% if repo_host_type == 'github.com' -%]
github.io
[%- elif repo_host_type == 'gitlab.com' -%]
gitlab.io
[%- endif %]
help: 'Specify the host of the self-managed GitLab Pages:'
type: str
when: '{{ repo_host_type == "gitlab-self-managed" }}'
repo_namespace:
default: '{{ organization_name|lower|replace(" ", "-") }}'
help: 'Indicate the GitHub Repository Owner or GitLab Namespace. This is typically the account name of the author or the organization:'
Expand Down
13 changes: 13 additions & 0 deletions includes/variable.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
[%- endif %]
[%- endmacro %]

[% macro page_base_url() -%]
[% if repo_host_type == 'github.com' -%]
{{ repo_namespace }}.github.io/{{ repo_name }}
[%- elif repo_host_type == 'gitlab.com' or repo_host_type == 'gitlab-self-managed' -%]
[%- set repo_namespace_root = repo_namespace.split("/")[0] -%]
{{ repo_namespace_root }}.{{ page_host }}{{ repo_namespace | replace(repo_namespace_root, "", 1) }}/{{ repo_name }}
[%- endif %]
[%- endmacro %]

[% macro ci_badge() -%]
[% if repo_host_type == 'github.com' -%]
[![CI Status](https://{{ repo_url() }}/actions/workflows/ci.yml/badge.svg?branch=main)](https://{{ repo_url() }}/actions/workflows/ci.yml)
Expand All @@ -18,6 +27,10 @@
[%- endif %]
[%- endmacro %]

[% macro coverage_badge() -%]
[![Coverage](https://img.shields.io/endpoint?url=https://{{ page_base_url() }}/reports/coverage/coverage_badge.json)](https://{{ page_base_url() }}/reports/coverage)
[%- endmacro %]

[% macro license_badge() -%]
[% if repo_host_type == 'github.com' -%]
[![GitHub](https://img.shields.io/github/license/{{ repo_namespace }}/{{ repo_name }})]
Expand Down
19 changes: 19 additions & 0 deletions scripts/generate-coverage-badge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

TOTAL_COVERAGE=$(coverage report --format=total)
COLOR="red"

if [ "$TOTAL_COVERAGE" -gt 90 ]; then
COLOR="green"
elif [ "$TOTAL_COVERAGE" -gt 75 ]; then
COLOR="yellow"
fi

cat << EOF > "${1:-.}/coverage_badge.json"
{
"schemaVersion": 1,
"label": "coverage",
"message": "${TOTAL_COVERAGE}%",
"color": "${COLOR}"
}
EOF
2 changes: 1 addition & 1 deletion template/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"autofix",
"automodule",
"cobertura",
"codecov",
"elif",
"endmacro",
"epub",
Expand Down Expand Up @@ -83,6 +82,7 @@
"author_email",
"repo_host_type",
"repo_host",
"page_host",
"ci",
"repo_namespace",
"repo_name",
Expand Down
3 changes: 2 additions & 1 deletion template/Makefile.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ docs-autobuild:
docs-mypy:
${PIPRUN} python -m mypy src test --html-report ${PUBLIC_DIR}/reports/mypy

# Generate coverage reports.
# Generate coverage reports and badge.
docs-coverage:
${PIPRUN} python -m coverage erase
${PIPRUN} python -m coverage run -m pytest
${PIPRUN} python -m coverage html -d ${PUBLIC_DIR}/reports/coverage
${PIPRUN} bash scripts/generate-coverage-badge.sh ${PUBLIC_DIR}/reports/coverage

# Generate all reports.
reports: docs-mypy docs-coverage
Expand Down
3 changes: 2 additions & 1 deletion template/README.md.jinja
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[% from pathjoin("includes", "variable.jinja") import ci_badge with context -%]
[% from pathjoin("includes", "variable.jinja") import coverage_badge with context -%]
[% from pathjoin("includes", "variable.jinja") import license_badge with context -%]
[% from pathjoin("includes", "variable.jinja") import repo_url with context -%]
# {{ project_name }}

{{ project_description }}

{{ ci_badge() }}
[![codecov](https://codecov.io/gh/{{ repo_namespace }}/{{ repo_name }}/branch/main/graph/badge.svg?token=4JPKXI122N)](https://codecov.io/gh/{{ repo_namespace }}/{{ repo_name }})
{{ coverage_badge() }}
[![Documentation Status](https://readthedocs.org/projects/{{ repo_name }}/badge/)](https://{{ repo_name }}.readthedocs.io/)
{{ license_badge() }}
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/{{ package_name }})](https://pypi.org/project/{{ package_name }}/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ jobs:
- run: env | sort
- run: make dev-test
- run: make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
flags: {{ '${{ matrix.python-version }}' }}
strategy:
matrix:
python-version:
Expand Down
19 changes: 19 additions & 0 deletions template/scripts/generate-coverage-badge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

TOTAL_COVERAGE=$(coverage report --format=total)
COLOR="red"

if [ "$TOTAL_COVERAGE" -gt 90 ]; then
COLOR="green"
elif [ "$TOTAL_COVERAGE" -gt 75 ]; then
COLOR="yellow"
fi

cat << EOF > "${1:-.}/coverage_badge.json"
{
"schemaVersion": 1,
"label": "coverage",
"message": "${TOTAL_COVERAGE}%",
"color": "${COLOR}"
}
EOF

0 comments on commit 59cae68

Please sign in to comment.