-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbadge.py
More file actions
97 lines (79 loc) · 2.22 KB
/
badge.py
File metadata and controls
97 lines (79 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
This module should contain only the things relevant to the badge being computed
by shields.io
"""
from __future__ import annotations
import decimal
import json
import urllib.parse
from typing import Literal
import httpx
def get_badge_color(
rate: decimal.Decimal,
minimum_green: decimal.Decimal,
minimum_orange: decimal.Decimal,
) -> str:
if rate >= minimum_green:
return "brightgreen"
elif rate >= minimum_orange:
return "orange"
else:
return "red"
def get_evolution_badge_color(
delta: decimal.Decimal | int,
up_is_good: bool = True,
neutral_color: str = "lightgrey",
) -> str:
if delta == 0:
return neutral_color
elif (delta > 0) is up_is_good:
return "brightgreen"
else:
return "red"
def compute_badge_endpoint_data(
line_rate: decimal.Decimal,
color: str,
) -> str:
badge = {
"schemaVersion": 1,
"label": "Coverage",
"message": f"{int(line_rate)}%",
"color": color,
}
return json.dumps(badge)
def compute_badge_image(
line_rate: decimal.Decimal, color: str, http_session: httpx.Client
) -> str:
return http_session.get(
"https://img.shields.io/static/v1?"
+ urllib.parse.urlencode(
{
"label": "Coverage",
"message": f"{int(line_rate)}%",
"color": color,
}
)
).text
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}.{format}")
def get_endpoint_url(endpoint_url: str) -> str:
return f"https://img.shields.io/endpoint?url={endpoint_url}"
def get_dynamic_url(endpoint_url: str) -> str:
return "https://img.shields.io/badge/dynamic/json?" + urllib.parse.urlencode(
{
"color": "brightgreen",
"label": "coverage",
"query": "$.message",
"url": endpoint_url,
}
)