repos.ts

7.4 KB · raw

1import type { Context, Hono } from "hono";2import { syncAll } from "../bucket/sync.ts";3import { operatorIssues } from "../issues/store.ts";4import { languageLines, repoStats } from "../mirror/queries.ts";5import { openMirror, quarantinedPaths } from "../mirror/store.ts";6import { html } from "../pages/html.ts";7import { formatDate, plural } from "../pages/layout.ts";8import { operatorReleases, visibleReleases } from "../releases/releases.ts";9import { repoText } from "../server/db.ts";10import { type AdminContext, adminPage, formField, notice, textFields } from "./console.ts";11import { type AdminRepo, allRepos, findRepo, type RepoEdit, updateRepo } from "./store.ts";1213const MAX_FIELD = 500;14const HTTP_URL = /^https?:\/\/[^\s]+$/i;15const MAX_LANGUAGES = 5;16const MAX_LANGUAGE = 30;1718/** Shown languages as typed, comma-separated; blanks between commas are dropped. */19function languageNames(value: string): string[] {20	return value21		.split(",")22		.map((name) => name.trim())23		.filter((name) => name !== "");24}2526/** §24's Repos section: visibility (§20), card settings, sync status and "sync now". */27export function registerRepos(app: Hono, ctx: AdminContext): void {28	app.get("/admin/repos", (c) => c.html(reposPage(c, ctx, "")));2930	app.post("/admin/sync", async (c) => {31		await syncAll(ctx.sync);32		return c.redirect("/admin/repos", 303);33	});3435	app.post("/admin/repos/:repo", async (c) => {36		const repo = findRepo(ctx.kepr, c.req.param("repo"));37		if (!repo) return c.notFound();38		const fields = await textFields(c);39		const text = (name: string) => (fields[name] ?? "").trim();40		const languages = languageNames(fields.languages ?? "");41		const edit: RepoEdit = {42			public: fields.public === "1" ? 1 : 0,43			category: text("category"),44			homepage: text("homepage"),45			// Joined with the separator `publicRepo` splits on, so a name never contains it.46			languages: languages.join(", "),47			issues_paused: fields.issues_paused === "1" ? 1 : 0,48		};49		const refuse = (error: string) => c.html(reposPage(c, ctx, `${repo.name}: ${error}`), 400);50		if (Object.values(fields).some((value) => value.length > MAX_FIELD)) {51			return refuse(`keep each field under ${MAX_FIELD} characters.`);52		}53		if (edit.homepage !== "" && !HTTP_URL.test(edit.homepage)) {54			return refuse("the homepage must be an http or https address.");55		}56		if (languages.length > MAX_LANGUAGES) return refuse("up to five languages.");57		if (languages.some((name) => name.length > MAX_LANGUAGE)) {58			return refuse(`a language name is at most ${MAX_LANGUAGE} characters.`);59		}60		if (languages.some((name) => /\p{C}/u.test(name))) {61			return refuse("a language name is printable text.");62		}63		updateRepo(ctx.kepr, repo.name, edit);64		return c.redirect(`/admin/repos?repo=${repo.name}&saved=1#repo-editor`, 303);65	});66}6768function reposPage(c: Context, ctx: AdminContext, error: string): string {69	const repos = allRepos(ctx.kepr);70	const selected = repos.find((repo) => repo.name === c.req.query("repo")) ?? repos[0];71	const body = html`${notice(error)}72<p class="admin-intro">Choose a repository to manage its visibility and public page.</p>73<form method="post" action="/admin/sync">74${formField(c)}<button class="issues-submit" type="submit">sync now</button></form>75${repos.length === 0 && html`<p class="muted mono save-empty">no repos found in the bucket yet</p>`}76<div class="admin-repos">77<nav aria-label="repositories">78${repos.map(79	(repo) => html`<a class="issue-row" id="repo-${repo.name}"80href="/admin/repos?repo=${repo.name}#repo-editor"${selected?.name === repo.name ? html` aria-current="page"` : ""}>81<span>${repo.name}</span><span>${repo.public ? "public" : "private"}</span>82<span class="issue-meta mono">${repo.last_sync_at === null ? "never synced" : `last sync ${formatDate(repo.last_sync_at)} UTC`}</span>83${repo.last_sync_error && html`<span class="issues-error">${repo.last_sync_error}</span>`}</a>`,84)}85</nav>86${selected && repoForm(c, ctx, selected)}87</div>`;88	return adminPage(c, ctx, "repos", body);89}9091/** The selected repo at a glance: its numbers, languages, description and releases (§24). */92function workspace(ctx: AdminContext, repo: AdminRepo) {93	const mirror = openMirror(ctx.mirrorDir, repo.name);94	const stats = repoStats(mirror);95	const releases = operatorReleases(ctx.kepr, mirror, repo.name);96	const downloads = (release: (typeof releases)[number]) =>97		release.downloads.reduce((sum, [, count]) => sum + count, 0);98	const open = operatorIssues(ctx.kepr, "open").filter((issue) => issue.repo === repo.name);99	const vital = (label: string, value: string | number) =>100		html`<div><strong>${String(value)}</strong><span class="mono">${label}</span></div>`;101	const own = repoText(ctx.kepr, repo.name).description !== "";102	return html`<div class="admin-vitals">103${vital("saves", stats.saves)}104${vital("lines", stats.lines)}105${vital("newest version", visibleReleases(ctx.kepr, mirror, repo.name)[0]?.version ?? "none")}106${vital("open issues", open.length)}107${vital(108	"downloads",109	releases.reduce((sum, release) => sum + downloads(release), 0),110)}111</div>112<p class="issue-meta mono">detected languages:113${languageLines(mirror).map(114	([language, lines], i) =>115		html`${i > 0 ? " · " : " "}${language} <span class="muted">${plural(lines, "line")}</span>`,116)}</p>117<p class="issue-meta mono">description: ${own ? "your own words" : "from the README"} ·118<a href="/${repo.name}">edit on its page ↗</a></p>119${120	releases.length > 0 &&121	html`<ul class="admin-releases mono">${releases.map(122		(release) => html`<li><a href="/admin/releases#${repo.name}-${release.version}">123${release.version}</a> <span class="muted">${release.hidden ? "hidden" : "shown"} · ${plural(124			downloads(release),125			"download",126		)}</span></li>`,127	)}</ul>`128}`;129}130131function repoForm(c: Context, ctx: AdminContext, repo: AdminRepo) {132	const quarantined = quarantinedPaths(openMirror(ctx.mirrorDir, repo.name));133	const input = (name: keyof RepoEdit, label: string) =>134		html`<label class="issues-label" for="${repo.name}-${name}">${label}</label>135<input class="issues-input" id="${repo.name}-${name}" name="${name}" maxlength="${MAX_FIELD}" value="${String(repo[name])}">`;136	const synced =137		repo.last_sync_at === null ? "never synced" : `last sync ${formatDate(repo.last_sync_at)} UTC`;138	return html`<article class="issue-post" id="repo-editor">139<p class="issue-post-head mono">140<span class="issue-author">141${repo.public ? html`<a href="/${repo.name}">${repo.name}</a>` : repo.name}</span>142<span>${repo.public ? "public" : "private"}</span><span class="history-when">${synced}</span></p>143${workspace(ctx, repo)}144${145	repo.last_sync_error && html`<p class="issues-error mono">sync error: ${repo.last_sync_error}</p>`146}147${148	quarantined.length > 0 &&149	html`<p class="issues-error mono">${plural(quarantined.length, "path")} quarantined: ${quarantined.join(", ")}</p>`150}151<form class="issues-form" method="post" action="/admin/repos/${repo.name}">152${formField(c)}153<label class="issues-label">154<input type="checkbox" name="public" value="1"${repo.public ? html` checked` : ""}> public</label>155<label class="issues-label">156<input type="checkbox" name="issues_paused" value="1"${repo.issues_paused ? html` checked` : ""}>157pause new issues</label>158${input("category", "category")}159${input("homepage", "homepage")}160${input("languages", "shown languages")}161<p class="issue-meta mono">Up to five, separated by commas. Leave blank to show the detected one.</p>162<button class="issues-submit" type="submit">save ${repo.name}</button>163</form>164</article>`;165}