Skip to content

Commit

Permalink
PNG badges in coverage comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed May 26, 2024
1 parent bddb8d0 commit f82ea85
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
10 changes: 8 additions & 2 deletions coverage_comment/badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import decimal
import json
import urllib.parse
from typing import Literal

import httpx

Expand Down Expand Up @@ -67,13 +68,18 @@ def compute_badge_image(
).text


def get_static_badge_url(label: str, message: str, color: str) -> str:
def get_static_badge_url(
label: str,
message: str,
color: str,
format: Literal["svg", "png"] = "png",
) -> str:
if not color or not message:
raise ValueError("color and message are required")
code = "-".join(
e.replace("_", "__").replace("-", "--") for e in (label, message, color) if e
)
return "https://img.shields.io/badge/" + urllib.parse.quote(f"{code}.svg")
return "https://img.shields.io/badge/" + urllib.parse.quote(f"{code}.{format}")


def get_endpoint_url(endpoint_url: str) -> str:
Expand Down
6 changes: 5 additions & 1 deletion coverage_comment/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pathlib
from collections.abc import Callable
from importlib import resources
from typing import Literal

import jinja2
from jinja2.sandbox import SandboxedEnvironment
Expand Down Expand Up @@ -127,14 +128,17 @@ def get_comment_markdown(
subproject_id: str | None = None,
custom_template: str | None = None,
pr_targets_default_branch: bool = True,
image_format: Literal["svg", "png"] = "png",
):
loader = CommentLoader(base_template=base_template, custom_template=custom_template)
env = SandboxedEnvironment(loader=loader)
env.filters["pct"] = pct
env.filters["delta"] = delta
env.filters["x100"] = x100
env.filters["get_evolution_color"] = badge.get_evolution_badge_color
env.filters["generate_badge"] = badge.get_static_badge_url
env.filters["generate_badge"] = functools.partial(
badge.get_static_badge_url, format=image_format
)
env.filters["pluralize"] = pluralize
env.filters["file_url"] = functools.partial(
get_file_url, repo_name=repo_name, pr_number=pr_number
Expand Down
2 changes: 1 addition & 1 deletion tests/end_to_end/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def test_public_repo(
fail_value="\n",
)

assert "-brightgreen.svg" in ext_comment
assert "-brightgreen.png" in ext_comment


@pytest.mark.repo_suffix("private")
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def test_compute_badge_image(session):


def test_get_static_badge_url():
result = badge.get_static_badge_url(label="a-b", message="c_d e", color="green")
result = badge.get_static_badge_url(
label="a-b", message="c_d e", color="green", format="svg"
)

assert result == "https://img.shields.io/badge/a--b-c__d%20e-green.svg"

Expand Down
Loading

0 comments on commit f82ea85

Please sign in to comment.