import { Hono } from "hono";
import {
getLatestHealthScore,
computeAndStoreHealthScore,
getBadgeColor,
} from "../lib/health-score";
import { loadRepoByPath } from "../lib/namespace";
const badge = new Hono();
function makeBadgeSvg(grade: string, color: string): string {
return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="90" height="20">
<linearGradient id="s" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<clipPath id="r"><rect width="90" height="20" rx="3" fill="#fff"/></clipPath>
<g clip-path="url(#r)">
<rect width="62" height="20" fill="#555"/>
<rect x="62" width="28" height="20" fill="${color}"/>
<rect width="90" height="20" fill="url(#s)"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="32" y="15" fill="#010101" fill-opacity=".3" lengthAdjust="spacing">health</text>
<text x="32" y="14" lengthAdjust="spacing">health</text>
<text x="76" y="15" fill="#010101" fill-opacity=".3" font-weight="bold">${grade}</text>
<text x="76" y="14" font-weight="bold">${grade}</text>
</g>
</svg>`;
}
badge.get("/badge/:owner/:repo", async (c) => {
const { owner, repo } = c.req.param();
c.header("Content-Type", "image/svg+xml");
c.header("Cache-Control", "max-age=3600, s-maxage=3600");
c.header("X-Content-Type-Options", "nosniff");
const repoRow = await loadRepoByPath(owner, repo);
if (!repoRow) {
return c.body(makeBadgeSvg("?", "#9f9f9f"), 200);
}
if (repoRow.isPrivate) {
return c.body(makeBadgeSvg("?", "#9f9f9f"), 200);
}
const scoreRow = await getLatestHealthScore(repoRow.id);
if (!scoreRow) {
computeAndStoreHealthScore(repoRow.id, owner, repo).catch((err) => {
console.error("[badge] background compute failed:", err);
});
return c.body(makeBadgeSvg("?", "#9f9f9f"), 200);
}
const color = getBadgeColor(scoreRow.grade);
return c.body(makeBadgeSvg(scoreRow.grade, color), 200);
});
export default badge;
|