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 SORT_ORDER = /^-?\d{1,6}$/;16const MAX_LANGUAGES = 5;17const MAX_LANGUAGE = 30;181920function languageNames(value: string): string[] {21 return value22 .split(",")23 .map((name) => name.trim())24 .filter((name) => name !== "");25}262728export function registerRepos(app: Hono, ctx: AdminContext): void {29 app.get("/admin/repos", (c) => c.html(reposPage(c, ctx, "")));3031 app.post("/admin/sync", async (c) => {32 await syncAll(ctx.sync);33 return c.redirect("/admin/repos", 303);34 });3536 app.post("/admin/repos/:repo", async (c) => {37 const repo = findRepo(ctx.kepr, c.req.param("repo"));38 if (!repo) return c.notFound();39 const fields = await textFields(c);40 const text = (name: string) => (fields[name] ?? "").trim();41 const languages = languageNames(fields.languages ?? "");42 const edit: RepoEdit = {43 public: fields.public === "1" ? 1 : 0,44 category: text("category"),45 homepage: text("homepage"),46 sort_order: Number(text("sort_order") || "0"),47 install1: text("install1"),48 install2: text("install2"),49 install3: text("install3"),50 51 languages: languages.join(", "),52 issues_paused: fields.issues_paused === "1" ? 1 : 0,53 };54 const refuse = (error: string) => c.html(reposPage(c, ctx, `${repo.name}: ${error}`), 400);55 if (Object.values(fields).some((value) => value.length > MAX_FIELD)) {56 return refuse(`keep each field under ${MAX_FIELD} characters.`);57 }58 if (edit.homepage !== "" && !HTTP_URL.test(edit.homepage)) {59 return refuse("the homepage must be an http or https address.");60 }61 if (!SORT_ORDER.test(text("sort_order") || "0")) return refuse("the order must be a number.");62 if ([edit.install1, edit.install2, edit.install3].some((line) => /[\r\n]/.test(line))) {63 return refuse("an install line is one line.");64 }65 if (languages.length > MAX_LANGUAGES) return refuse("up to five languages.");66 if (languages.some((name) => name.length > MAX_LANGUAGE)) {67 return refuse(`a language name is at most ${MAX_LANGUAGE} characters.`);68 }69 if (languages.some((name) => /\p{C}/u.test(name))) {70 return refuse("a language name is printable text.");71 }72 updateRepo(ctx.kepr, repo.name, edit);73 return c.redirect(`/admin/repos?repo=${repo.name}&saved=1#repo-editor`, 303);74 });75}7677function reposPage(c: Context, ctx: AdminContext, error: string): string {78 const repos = allRepos(ctx.kepr);79 const selected = repos.find((repo) => repo.name === c.req.query("repo")) ?? repos[0];80 const body = html`${notice(error)}81<p class="admin-intro">Choose a repository to manage its visibility and public page.</p>82<form method="post" action="/admin/sync">83${formField(c)}<button class="issues-submit" type="submit">sync now</button></form>84${repos.length === 0 && html`<p class="muted mono save-empty">no repos found in the bucket yet</p>`}85<div class="admin-repos">86<nav aria-label="repositories">87${repos.map(88 (repo) => html`<a class="issue-row" id="repo-${repo.name}"89href="/admin/repos?repo=${repo.name}#repo-editor"${selected?.name === repo.name ? html` aria-current="page"` : ""}>90<span>${repo.name}</span><span>${repo.public ? "public" : "private"}</span>91<span class="issue-meta mono">${repo.last_sync_at === null ? "never synced" : `last sync ${formatDate(repo.last_sync_at)} UTC`}</span>92${repo.last_sync_error && html`<span class="issues-error">${repo.last_sync_error}</span>`}</a>`,93)}94</nav>95${selected && repoForm(c, ctx, selected)}96</div>`;97 return adminPage(c, ctx, "repos", body);98}99100101function workspace(ctx: AdminContext, repo: AdminRepo) {102 const mirror = openMirror(ctx.mirrorDir, repo.name);103 const stats = repoStats(mirror);104 const releases = operatorReleases(ctx.kepr, mirror, repo.name);105 const downloads = (release: (typeof releases)[number]) =>106 release.downloads.reduce((sum, [, count]) => sum + count, 0);107 const open = operatorIssues(ctx.kepr, "open").filter((issue) => issue.repo === repo.name);108 const vital = (label: string, value: string | number) =>109 html`<div><strong>${String(value)}</strong><span class="mono">${label}</span></div>`;110 const own = repoText(ctx.kepr, repo.name).description !== "";111 return html`<div class="admin-vitals">112${vital("saves", stats.saves)}113${vital("lines", stats.lines)}114${vital("newest version", visibleReleases(ctx.kepr, mirror, repo.name)[0]?.version ?? "none")}115${vital("open issues", open.length)}116${vital(117 "downloads",118 releases.reduce((sum, release) => sum + downloads(release), 0),119)}120</div>121<p class="issue-meta mono">detected languages:122${languageLines(mirror).map(123 ([language, lines], i) =>124 html`${i > 0 ? " · " : " "}${language} <span class="muted">${plural(lines, "line")}</span>`,125)}</p>126<p class="issue-meta mono">description: ${own ? "your own words" : "from the README"} ·127<a href="/${repo.name}">edit on its page ↗</a></p>128${129 releases.length > 0 &&130 html`<ul class="admin-releases mono">${releases.map(131 (release) => html`<li><a href="/admin/releases#${repo.name}-${release.version}">132${release.version}</a> <span class="muted">${release.hidden ? "hidden" : "shown"} · ${plural(133 downloads(release),134 "download",135 )}</span></li>`,136 )}</ul>`137}`;138}139140function repoForm(c: Context, ctx: AdminContext, repo: AdminRepo) {141 const quarantined = quarantinedPaths(openMirror(ctx.mirrorDir, repo.name));142 const input = (name: keyof RepoEdit, label: string) =>143 html`<label class="issues-label" for="${repo.name}-${name}">${label}</label>144<input class="issues-input" id="${repo.name}-${name}" name="${name}" maxlength="${MAX_FIELD}" value="${String(repo[name])}">`;145 const synced =146 repo.last_sync_at === null ? "never synced" : `last sync ${formatDate(repo.last_sync_at)} UTC`;147 return html`<article class="issue-post" id="repo-editor">148<p class="issue-post-head mono">149<span class="issue-author">150${repo.public ? html`<a href="/${repo.name}">${repo.name}</a>` : repo.name}</span>151<span>${repo.public ? "public" : "private"}</span><span class="history-when">${synced}</span></p>152${workspace(ctx, repo)}153${154 repo.last_sync_error && html`<p class="issues-error mono">sync error: ${repo.last_sync_error}</p>`155}156${157 quarantined.length > 0 &&158 html`<p class="issues-error mono">${plural(quarantined.length, "path")} quarantined: ${quarantined.join(", ")}</p>`159}160<form class="issues-form" method="post" action="/admin/repos/${repo.name}">161${formField(c)}162<label class="issues-label">163<input type="checkbox" name="public" value="1"${repo.public ? html` checked` : ""}> public</label>164<label class="issues-label">165<input type="checkbox" name="issues_paused" value="1"${repo.issues_paused ? html` checked` : ""}>166pause new issues</label>167${input("category", "category")}168${input("homepage", "homepage")}169${input("sort_order", "card order")}170<p class="issue-meta mono">Lower numbers appear first. Repositories with the same number sort by name.</p>171${input("install1", "install line 1")}172${input("install2", "install line 2")}173${input("install3", "install line 3")}174${input("languages", "shown languages")}175<p class="issue-meta mono">Up to five, separated by commas. Leave blank to show the detected one.</p>176<button class="issues-submit" type="submit">save ${repo.name}</button>177</form>178</article>`;179}