Files
coa-server-manager/src/lib/report.ts
T

75 lines
3.2 KiB
TypeScript

import type { ReportTarget, ReportTargetId } from "./api";
/** The text of a problem report, always in English so whoever reads it on GitHub can follow it; the person's own words go in as typed. */
export interface ReportInput {
managerVersion: string;
windows: string;
installKind: "new" | "imported";
serverVersion: string | null;
/** where the report goes; a bot repository is told which Manager and bot release it came from */
target: ReportTargetId;
botsVersion: string | null;
what: string;
expected: string;
steps: string;
/** a diagnostics file was created for this report */
diagnostics: boolean;
}
/** The "new issue" page of a repository given as `owner/name`. */
export function newIssueUrl(repo: string): string {
return `https://github.com/${repo}/issues/new`;
}
const PRIVACY = "> Please remove passwords, tokens, private IP information or other sensitive data before posting logs.";
export function reportBody(r: ReportInput): string {
const text = (s: string, fallback: string) => (s.trim() ? s.trim() : fallback);
return [
"## Bug Report",
"",
`**Manager version:** ${r.managerVersion || "unknown"} `,
`**Windows version:** ${r.windows || "unknown"} `,
`**Fresh install or imported server:** ${r.installKind === "new" ? "Fresh install" : "Imported server"} `,
...(r.serverVersion ? [`**Server version:** ${r.serverVersion} `] : []),
...(r.target === "squid" || r.target === "companions" ? [`**Bot system:** ${r.target === "squid" ? "SQUID Playerbots" : "CoA Companions"}${r.botsVersion ? ` ${r.botsVersion}` : ""} `] : []),
...(r.target === "custom-races" ? ["**Component:** Custom Races "] : []),
...(r.target === "content-scaling" ? ["**Component:** Content Scaling "] : []),
"",
"**What happened?** ",
text(r.what, "Describe the problem."),
"",
"**What did you expect to happen?** ",
text(r.expected, "Describe the expected behaviour."),
"",
"**Steps to reproduce:** ",
text(r.steps, "1.\n2.\n3."),
"",
"**Screenshot / video:** ",
"Attach it if possible.",
"",
"**Logs:** ",
r.diagnostics ? "The diagnostics file from the Manager is attached below (it has passwords and addresses removed)." : "Attach relevant Manager/server logs if available.",
"",
PRIVACY,
"",
].join("\n");
}
/** Percent-encode everything that is not a plain letter, digit or one of . _ - so the link carries nothing the browser could misread. */
export function encodeQuery(s: string): string {
return Array.from(new TextEncoder().encode(s))
.map((b) => {
const c = String.fromCharCode(b);
return /[A-Za-z0-9._-]/.test(c) ? c : "%" + b.toString(16).toUpperCase().padStart(2, "0");
})
.join("");
}
/** A link that opens GitHub's "new issue" page of `target` with the title and text filled in. The bug label is only asked for in our own repositories. */
export function issueUrl(target: Pick<ReportTarget, "repo" | "ours">, title: string, body: string): string {
return `${newIssueUrl(target.repo)}?${target.ours ? "labels=bug&" : ""}title=${encodeQuery(title)}&body=${encodeQuery(body)}`;
}
/** Links longer than this are not reliable in every browser; the full text is then copied to the clipboard instead. */
export const MAX_LINK = 2000;