An unread or stale view held no entry for a watched repository, so every
member would have merged plainly with the note no delivery: a merge that
reports success and delivers nothing. The registrations holder now keeps
the time of its last read and of the last registration-changed heard; the
merge call refuses every member ("the watched branches are not read yet")
while the view is not read since that change, and a read that fails is
said and asked again with the module's growing wait until it answers.
Builds: design 47 H2 H-A-471 (second addendum); review finding H-A-471 addendum (2)
405 lines
26 KiB
TypeScript
405 lines
26 KiB
TypeScript
// The forge module holds the seat `git` (novox/hq design 47 *The seat git*, ADR 0596 §1, §2): every read and act a
|
|
// delivery part makes of the forge is a verb of the seat, served here as a tool `git.<verb>` against the seat's generated
|
|
// contract, and no part names this module's own tools.
|
|
//
|
|
// **Who may call which verb** is the seat definition's `callers`, which the grants part grants by: the bus refuses a caller a
|
|
// definition does not name (design 47 §68, §313, §321), so this holder judges no caller and keeps no list of its own.
|
|
//
|
|
// **A failed read answers an error, never an empty answer**: the forge's 404 is the kind `not-found`, its refusal (another
|
|
// 4xx, or a refusal of this holder) the kind `refused`, a forge that failed or did not answer the kind `failed`; an empty list or an absent file is answered
|
|
// only when the forge said so. A 401, 502, 503 or connection fault whose version read fails, or names the forge's store
|
|
// unavailable, is the kind `away` (design 47 H2 §776; failureOf).
|
|
//
|
|
// **The acts carry the caller's id** (status, comment, view, note): an id the pull request or the note already holds is
|
|
// answered `held` and nothing is done again. `merge-pull` is one merge call of one or more pull requests, every member
|
|
// judged before any is merged, no branch protection judged (mergecall.ts; design 47 H §734, §735; ADR 0682 §13).
|
|
|
|
import type { ToolDefinition } from "@novox/mesh-sdk/tools";
|
|
import {
|
|
seat as GIT, verbs as GIT_VERBS,
|
|
type BranchAnswer, type ContainsAnswer, type CommentAnswer, type ErrorKind, type FileAnswer, type MergePullAnswer, type Merging, type NoteAnswer,
|
|
type OpenPull, type OpenPullsAnswer, type Pull, type PullAnswer, type Status, type StatusAnswer, type StatusesAnswer,
|
|
type TagAnswer, type TagsAnswer, type TreesAnswer, type ViewAnswer,
|
|
} from "@novox/mesh-delivery-contracts-git";
|
|
import type { CommitStatus, GiteaClient, GiteaComment, GiteaPull } from "./client.js";
|
|
import { DirectoryNotInTree, MAX_FILE_BYTES } from "./client.js";
|
|
import { containedFrom, newestByContext } from "./checks.js";
|
|
import { Refused, checkName } from "./guard.js";
|
|
import { AdminRefused } from "./token.js";
|
|
import { awayAnswer } from "@novox/mesh-sdk/callers";
|
|
import { appendNote, noteHolds, viewBody, viewComment, type Runner } from "./delivery.js";
|
|
import type { Judged } from "./failedsteps.js";
|
|
import { mergeCall, membersOf } from "./mergecall.js";
|
|
import type { Calls, Say } from "./calls.js";
|
|
import type { Partials } from "./partials.js";
|
|
import type { Once } from "./taken.js";
|
|
import { askIdOf } from "@novox/mesh-sdk/callers";
|
|
|
|
/** The verbs of the seat, in its definition's order. */
|
|
export const VERBS: readonly string[] = Object.values(GIT_VERBS);
|
|
|
|
/** How many tags a page of `tags` holds. */
|
|
export const TAGS_PAGE = 50;
|
|
|
|
/** The forge's container, where its repositories and git are: the note is written there (novox/hq ADR 0239). Its name
|
|
* is the manifest's container's; no variable names it (a MESH_* word nothing sets is refused, checks/environment). */
|
|
export const FORGE_CONTAINER = "gitea";
|
|
|
|
/** What the holder reaches beside the forge's API: git in the forge's container (the note), and whether the first
|
|
* registrations are written. */
|
|
export interface SeatDeps {
|
|
run: Runner;
|
|
container: string;
|
|
/** Whether the first registrations were written (registrations.ts): until then merge-pull refuses every merge (design 47 §95). */
|
|
registered: () => boolean;
|
|
/** The failed `required` step that refuses a head, judged from the module's own state `failed-steps` (failedsteps.ts), or null. */
|
|
judge: (head: string) => Promise<Judged | null>;
|
|
/** The watched branches the registrations name, by their origin repository (registrations.ts): a member's base is one;
|
|
* null while they are not read since the last registration-changed heard (H-A-471, second addendum). */
|
|
watched: () => ReadonlyMap<string, ReadonlySet<string>> | null;
|
|
/** The state calls (calls.ts): each merge call written before its first merge (design 47 H §766). */
|
|
calls?: Calls;
|
|
/** Says merge-called on the seat (mesh-sdk runtime.seatPublish; design 47 H §736). */
|
|
say?: Say;
|
|
/** The findings merge-partial (partials.ts; design 47 H §767). */
|
|
partials?: Partials;
|
|
/** Takes each act once by its ask id (taken.ts; design 47 H2 §775): absent, an act acts at every call. */
|
|
once?: Once;
|
|
}
|
|
|
|
/** A failure as the contract says it: the forge's or this holder's refusal, or a forge that failed. */
|
|
export function failure(err: unknown): { error: string; "error-kind": ErrorKind } {
|
|
const text = err instanceof Error ? err.message : String(err);
|
|
// The forge's 404: what was asked about (a repository, a commit, a pull request, a branch, a tag) does not exist.
|
|
if (/^Gitea API [^:]*: 404 /.test(text)) return { error: text, "error-kind": "not-found" };
|
|
// A directory of `files` the commit does not hold (client.ts commitTrees): not-found, the error naming the directory, so the
|
|
// indexer reads the others alone and leaves the gone one out (novox/hq design 47 H H-A-232).
|
|
if (err instanceof DirectoryNotInTree) return { error: text, "error-kind": "not-found" };
|
|
const refused = err instanceof Refused || /^Gitea API [^:]*: 4\d\d /.test(text) || /^Gitea API: /.test(text);
|
|
return { error: text, "error-kind": refused ? "refused" : "failed" };
|
|
}
|
|
|
|
/** The forge's own version read, which tells the forge away from its refusal (client.ts forgeVersion). */
|
|
export type VersionRead = Pick<GiteaClient, "forgeVersion">;
|
|
|
|
/**
|
|
* Which failure may mean the forge or its store is away (design 47 H2 §776): a 401 (or the admin account refused at the
|
|
* mint the 401 asked), a 502 or 503, or a connection fault (no answer, or an answer cut at its limit); else null.
|
|
*/
|
|
function awayCause(err: unknown): "unauthorized" | "unavailable" | null {
|
|
if (err instanceof AdminRefused) return "unauthorized";
|
|
const text = err instanceof Error ? err.message : String(err);
|
|
if (/^Gitea [^:]*: 401 /.test(text)) return "unauthorized";
|
|
if (/^Gitea [^:]*: 50[23] /.test(text)) return "unavailable";
|
|
if (/^Gitea API [^:]*: no answer in /.test(text)) return "unavailable";
|
|
if (err instanceof TypeError && /fetch failed/.test(text)) return "unavailable";
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* A failure as the contract says it, the forge's own version read deciding a 401, a 502, a 503 or a connection fault
|
|
* (design 47 H2 §776): the read failing, or naming the forge's store unavailable, answers `away`, which every caller reads
|
|
* as a transport fault; the read passing, a 401 is the forge refusing the mesh's admin account (the client minted a
|
|
* fresh token on the first 401 and was refused again, token.ts), never worded as an account to create; else as failure.
|
|
*/
|
|
export async function failureOf(forge: VersionRead, err: unknown): Promise<{ error: string; "error-kind": ErrorKind; part?: string; words?: string }> {
|
|
const cause = awayCause(err);
|
|
if (cause === null) return failure(err);
|
|
const text = err instanceof Error ? err.message : String(err);
|
|
const read = await forge.forgeVersion();
|
|
if (read.ok === false) return awayAnswer(read.store ? "the forge's store" : "the forge", `${text}; ${read.words}`);
|
|
if (cause === "unauthorized") return { error: `the forge refuses the mesh's admin account: ${text}`, "error-kind": "refused" };
|
|
return failure(err);
|
|
}
|
|
|
|
/** owner/name of a `repository` argument, each checked as a name (guard.ts). */
|
|
export function repositoryOf(raw: unknown): { owner: string; repo: string; full: string } {
|
|
const parts = String(raw ?? "").split("/");
|
|
if (parts.length !== 2) throw new Refused(`repository ${JSON.stringify(raw)} is not <owner>/<name>`);
|
|
const owner = checkName("owner", parts[0]), repo = checkName("name", parts[1]);
|
|
return { owner, repo, full: `${owner}/${repo}` };
|
|
}
|
|
|
|
const SHA = /^[0-9a-f]{40}$/;
|
|
/** The git tag of a released version, <directory>/vMAJOR.MINOR.PATCH (design 47 H2 §796; design 47 §370). */
|
|
const VERSION_TAG = /^(?:[A-Za-z0-9_][A-Za-z0-9._-]*\/)+v\d+\.\d+\.\d+$/;
|
|
/** The form of the seat git's values and answers this holder answers in (design 47 H2 §780, §796). */
|
|
const GIT_FORM = 3;
|
|
const STATES = new Set(["pending", "success", "failure", "error", "warning"]);
|
|
function commitOf(name: string, raw: unknown): string {
|
|
const sha = String(raw ?? "").trim();
|
|
if (!SHA.test(sha)) throw new Refused(`${name} is a commit's full 40-hex sha`);
|
|
return sha;
|
|
}
|
|
function numberOf(raw: unknown): number {
|
|
const n = Number(raw);
|
|
if (!Number.isInteger(n) || n < 1) throw new Refused(`${JSON.stringify(raw)} is not a pull request's number`);
|
|
return n;
|
|
}
|
|
function idOf(raw: unknown): string {
|
|
const id = String(raw ?? "").trim();
|
|
if (!id || /[\s<>]/.test(id)) throw new Refused("id is the act's id: one word");
|
|
return id;
|
|
}
|
|
|
|
/** The marker an act's id leaves in a comment, so an id said again is found and skipped. */
|
|
export function actMarker(id: string): string {
|
|
return `<!-- mesh-act:${id} -->`;
|
|
}
|
|
|
|
/** A pull request as the contract says it; never its description (ADR 0659 §1). */
|
|
export function pullOf(full: string, p: GiteaPull): Pull {
|
|
const state = p.merged ? "merged" : p.state === "open" ? "open" : "closed";
|
|
return {
|
|
repository: full, number: p.number, base: String(p.base ?? ""), branch: String(p.head ?? ""), head: String(p.head_sha ?? ""),
|
|
state, ...(p.user ? { author: p.user } : {}), ...(p.merge_commit_sha ? { "merge-commit": p.merge_commit_sha } : {}),
|
|
...(p.merged_at ? { "merged-at": p.merged_at } : {}), ...(p.title ? { title: p.title } : {}),
|
|
...(p.html_url ? { url: p.html_url } : {}),
|
|
};
|
|
}
|
|
|
|
function statusOf(s: { context: string; state: string; description?: string; target_url?: string; updated_at?: string; creator?: string }): Status {
|
|
return { context: s.context, state: s.state, ...(s.description ? { description: s.description } : {}),
|
|
...(s.target_url ? { target: s.target_url } : {}), ...(s.updated_at ? { at: s.updated_at } : {}),
|
|
...(s.creator ? { by: s.creator } : {}) };
|
|
}
|
|
|
|
/** What the forge would merge now, for an open pull request: whole, or the verb's error. */
|
|
async function mergingOf(gitea: GiteaClient, owner: string, repo: string, p: GiteaPull): Promise<Merging> {
|
|
if (p.mergeable === undefined) throw new Error(`the forge did not say whether ${owner}/${repo}#${p.number} merges now`);
|
|
const rule = await gitea.appliedProtection(owner, repo, String(p.base ?? ""));
|
|
const writes = (await gitea.getRepo(owner, repo)).writes;
|
|
if (writes === undefined) throw new Error(`the forge did not say whether its account writes ${owner}/${repo}`);
|
|
const required = rule?.enable_status_check ? [...(rule.status_check_contexts ?? [])] : [];
|
|
return { mergeable: p.mergeable, protected: !!rule, required, writes };
|
|
}
|
|
|
|
/** The seat's verbs as tools `git.<verb>`, each answering in the seat's contract. */
|
|
export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[] {
|
|
const answered = async (args: Record<string, unknown>, act: (args: Record<string, unknown>) => Promise<object>): Promise<object> => {
|
|
try {
|
|
return await act(args);
|
|
} catch (err) {
|
|
return failureOf(gitea, err);
|
|
}
|
|
};
|
|
// An act given `standing`, what it did read afresh, is taken once by its ask id (§775, H-A-381, H-A-400).
|
|
const verb = (name: string, description: string, act: (args: Record<string, unknown>) => Promise<object>,
|
|
standing?: (args: Record<string, unknown>) => Promise<object>): ToolDefinition => ({
|
|
name: `${GIT}.${name}`,
|
|
description,
|
|
input: {},
|
|
run: async (args: Record<string, unknown>) => standing && deps.once
|
|
? deps.once.run(askIdOf(args), () => answered(args, act), () => answered(args, standing))
|
|
: answered(args, act),
|
|
});
|
|
const s = standingActs(gitea, deps);
|
|
|
|
return [
|
|
verb(GIT_VERBS.pull, "The seat git's pull: one pull request by its repository and number or by its merge commit, and for an open one what the forge would merge now.",
|
|
async (args): Promise<PullAnswer> => {
|
|
const { owner, repo, full } = repositoryOf(args.repository);
|
|
const p = args["merge-commit"] !== undefined
|
|
? await gitea.pullOfCommit(owner, repo, commitOf("merge-commit", args["merge-commit"]))
|
|
: await gitea.getPullRequest(owner, repo, numberOf(args.number));
|
|
const pull = pullOf(full, p);
|
|
return pull.state === "open" ? { pull, merging: await mergingOf(gitea, owner, repo, p) } : { pull };
|
|
}),
|
|
verb(GIT_VERBS.branch, "The seat git's branch: a branch's head.",
|
|
async (args): Promise<BranchAnswer> => {
|
|
const { owner, repo, full } = repositoryOf(args.repository);
|
|
// No branch named is the repository's default branch, read from the forge, as the verb file reads it (A-399).
|
|
let branch = String(args.branch ?? "");
|
|
if (branch === "") branch = String((await gitea.getRepo(owner, repo)).default_branch ?? "");
|
|
if (branch === "") throw new Error(`the forge names no default branch of ${full}`);
|
|
// The head first: a branch the forge does not hold is its 404, the kind not-found, never an unprotected branch.
|
|
const head = await gitea.branchHead(owner, repo, branch);
|
|
return { repository: full, branch, head };
|
|
}),
|
|
verb(GIT_VERBS.openPulls, "The seat git's open-pulls: every open pull request of every repository, with the statuses its base requires and those its head has.",
|
|
async (): Promise<OpenPullsAnswer> => {
|
|
const pulls: OpenPull[] = (await gitea.openPulls()).map((o) => ({
|
|
pull: { repository: `${o.owner}/${o.repo}`, number: o.number, base: o.base, branch: String(o.head ?? ""), head: o.head_sha,
|
|
state: "open", ...(o.html_url ? { url: o.html_url } : {}) },
|
|
required: [...o.required],
|
|
checks: Object.fromEntries(Object.entries(o.checks ?? {}).map(([context, c]) =>
|
|
[context, statusOf({ context, state: c.state, description: c.description, updated_at: c.updated_at })])),
|
|
}));
|
|
return { pulls };
|
|
}),
|
|
verb(GIT_VERBS.tags, "The seat git's tags: a repository's tags, each with the commit it names, a page at a time, newest first; more when another page exists.",
|
|
async (args): Promise<TagsAnswer> => {
|
|
const { owner, repo, full } = repositoryOf(args.repository);
|
|
const page = args.page === undefined ? 1 : Number(args.page);
|
|
const prefix = String(args.prefix ?? "");
|
|
// One more than the page is asked, so whether another page exists is the forge's answer, not a guess.
|
|
const got = await gitea.tags(owner, repo, page, TAGS_PAGE + 1);
|
|
const more = got.length > TAGS_PAGE;
|
|
const tags = got.slice(0, TAGS_PAGE).filter((t) => t.tag.startsWith(prefix));
|
|
return { form: GIT_FORM, repository: full, page, bound: TAGS_PAGE, tags, ...(more ? { more } : {}) };
|
|
}),
|
|
verb(GIT_VERBS.trees, "The seat git's trees: the trees of one commit, with files and paths when asked; a listing the forge cut is refused.",
|
|
async (args): Promise<TreesAnswer> => {
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const commit = commitOf("commit", args.commit);
|
|
const got = await gitea.commitTrees(owner, repo, commit, args.paths as Record<string, string[]> | undefined,
|
|
args.files as string[] | undefined);
|
|
return { commit, ...got };
|
|
}),
|
|
verb(GIT_VERBS.file, "The seat git's file: one file at one commit, branch or tag, decoded; over 512 KiB refused; absent only when the forge said so.",
|
|
async (args): Promise<FileAnswer> => {
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const path = String(args.path ?? "");
|
|
const ref = args.ref === undefined ? undefined : String(args.ref);
|
|
try {
|
|
const f = await gitea.getFile(owner, repo, path, ref);
|
|
if (f.size > MAX_FILE_BYTES) throw new Refused(`${path} is ${f.size} bytes, over ${MAX_FILE_BYTES / 1024} KiB`);
|
|
return { path: f.path, ...(ref ? { ref } : {}), blob: f.sha, size: f.size, content: f.content };
|
|
} catch (err) {
|
|
if (err instanceof Error && /^Gitea API [^:]*\/contents\/[^:]*: 404 /.test(err.message)) return { path, ...(ref ? { ref } : {}), absent: true };
|
|
throw err;
|
|
}
|
|
}),
|
|
verb(GIT_VERBS.contains, "The seat git's contains: whether one commit contains another, an ancestor of it or the same.",
|
|
async (args): Promise<ContainsAnswer> => {
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const commit = commitOf("commit", args.commit), within = commitOf("in", args.in);
|
|
const { contains, ahead } = containedFrom(await gitea.compare(owner, repo, within, commit));
|
|
return { commit, in: within, contains, ahead };
|
|
}),
|
|
verb(GIT_VERBS.statuses, "The seat git's statuses: the statuses a commit carries, the newest of each context.",
|
|
async (args): Promise<StatusesAnswer> => {
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const commit = commitOf("commit", args.commit);
|
|
const combined = await gitea.combinedStatus(owner, repo, commit);
|
|
return { commit, combined: combined.state, statuses: newestByContext(combined.statuses).map(statusOf) };
|
|
}),
|
|
verb(GIT_VERBS.status, "The seat git's status: an act of the forge bridge, a commit status set on a commit; the same id said again sets it again to the same.",
|
|
async (args): Promise<StatusAnswer> => {
|
|
const id = idOf(args.id);
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const commit = commitOf("commit", args.commit);
|
|
const state = String(args.state ?? "");
|
|
if (!STATES.has(state)) throw new Refused(`state ${JSON.stringify(state)} is none of ${[...STATES].join(", ")}`);
|
|
await gitea.setCommitStatus(owner, repo, commit, { state: state as CommitStatus["state"], context: String(args.context ?? ""),
|
|
description: String(args.description ?? ""), ...(args.target ? { target_url: String(args.target) } : {}) });
|
|
return { id, performed: true };
|
|
}, s.status),
|
|
verb(GIT_VERBS.comment, "The seat git's comment: an act of the forge bridge, a comment on a pull request carrying the act's id; an id it already holds is held.",
|
|
async (args): Promise<CommentAnswer> => {
|
|
const id = idOf(args.id);
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const number = numberOf(args.number);
|
|
if (holds(await gitea.listComments(owner, repo, number), id)) return { id, held: true, performed: true };
|
|
await gitea.addComment(owner, repo, number, `${String(args.text ?? "")}\n\n${actMarker(id)}`);
|
|
return { id, performed: true };
|
|
}, s.comment),
|
|
verb(GIT_VERBS.view, "The seat git's view: an act of the forge bridge, the delivery's one view comment kept on a pull request; an id it already holds is held.",
|
|
async (args): Promise<ViewAnswer> => {
|
|
const id = idOf(args.id);
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const number = numberOf(args.number);
|
|
const existing = viewComment(await gitea.listComments(owner, repo, number));
|
|
if (existing && existing.body.includes(actMarker(id))) return { id, held: true, performed: true };
|
|
const body = `${viewBody(String(args.text ?? ""))}\n${actMarker(id)}`;
|
|
if (existing) await gitea.editComment(owner, repo, existing.id, body);
|
|
else await gitea.addComment(owner, repo, number, body);
|
|
return { id, performed: true };
|
|
}, s.view),
|
|
verb(GIT_VERBS.note, "The seat git's note: an act of the forge bridge, one line on a commit's note under refs/notes/mesh-plan carrying the act's id; an id it already holds is held.",
|
|
async (args): Promise<NoteAnswer> => {
|
|
const id = idOf(args.id);
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const commit = commitOf("commit", args.commit);
|
|
const said = await appendNote(deps.run, deps.container, owner, repo, commit, "mesh-plan", `${String(args.line ?? "")} [${id}]`, id);
|
|
return said.added ? { id, performed: true } : { id, held: true, performed: true };
|
|
}, s.note),
|
|
verb(GIT_VERBS.mergePull, "The seat git's merge-pull: one merge call, a delivery's start and its group: the pull requests named (pulls, each {repository, number, head}) and by (who@node); every member is judged before any is merged (still its head, its base a watched branch, no required step failed at its fingerprint), none merged when one is refused, then each merged in the order named; it answers the call and each member's outcome.",
|
|
async (args): Promise<MergePullAnswer> => mergeCall(gitea, args, deps), s.mergePull),
|
|
verb(GIT_VERBS.tag, "The seat git's tag: the git tag <directory>/vMAJOR.MINOR.PATCH of a released version, written at the commit its build was made from; a tag that is at that commit is performed.",
|
|
async (args): Promise<TagAnswer> => {
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const tag = String(args.tag ?? "");
|
|
if (!VERSION_TAG.test(tag)) throw new Refused(`${JSON.stringify(tag)} is not <directory>/vMAJOR.MINOR.PATCH`);
|
|
const commit = commitOf("commit", args.commit);
|
|
const at = await gitea.tagCommit(owner, repo, tag);
|
|
if (at === commit) return { tag, performed: true };
|
|
if (at !== null) throw new Refused(`${tag} is at ${at.slice(0, 12)} already, not at ${commit.slice(0, 12)}; a tag is never moved`);
|
|
await gitea.createTag(owner, repo, tag, commit);
|
|
return { tag, performed: true };
|
|
}, s.tag),
|
|
];
|
|
}
|
|
|
|
function holds(comments: GiteaComment[], id: string): boolean {
|
|
return comments.some((c) => String(c.body ?? "").includes(actMarker(id)));
|
|
}
|
|
|
|
/**
|
|
* What each act did, read afresh from the forge, for an ask given again after its act ended (design 47 H2 §775; H-A-381
|
|
* (1)): the act is never performed twice, and what the forge holds is what the act answers when done. A repeat for which
|
|
* the forge holds nothing of the act is a refusal naming it.
|
|
*/
|
|
function standingActs(gitea: GiteaClient, deps: SeatDeps) {
|
|
const never = (ask: string, what: string) => new Refused(`the ask ${ask} was taken and its act acted, but ${what}: answered as it stands, never acted again`);
|
|
const askOf = (args: Record<string, unknown>) => askIdOf(args);
|
|
return {
|
|
status: async (args: Record<string, unknown>): Promise<StatusAnswer> => {
|
|
const id = idOf(args.id);
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const commit = commitOf("commit", args.commit);
|
|
const context = String(args.context ?? ""), state = String(args.state ?? "");
|
|
const now = newestByContext((await gitea.combinedStatus(owner, repo, commit)).statuses).find((x) => x.context === context);
|
|
if (!now || now.state !== state) throw never(askOf(args), `${commit.slice(0, 12)} carries no ${context} ${state}`);
|
|
return { id, held: true, performed: true };
|
|
},
|
|
comment: async (args: Record<string, unknown>): Promise<CommentAnswer> => {
|
|
const id = idOf(args.id);
|
|
const { owner, repo, full } = repositoryOf(args.repository);
|
|
const number = numberOf(args.number);
|
|
if (!holds(await gitea.listComments(owner, repo, number), id)) throw never(askOf(args), `${full}#${number} holds no comment of the act ${id}`);
|
|
return { id, held: true, performed: true };
|
|
},
|
|
view: async (args: Record<string, unknown>): Promise<ViewAnswer> => {
|
|
const id = idOf(args.id);
|
|
const { owner, repo, full } = repositoryOf(args.repository);
|
|
const number = numberOf(args.number);
|
|
const existing = viewComment(await gitea.listComments(owner, repo, number));
|
|
if (!existing?.body.includes(actMarker(id))) throw never(askOf(args), `${full}#${number}'s view is not the act ${id}'s`);
|
|
return { id, held: true, performed: true };
|
|
},
|
|
note: async (args: Record<string, unknown>): Promise<NoteAnswer> => {
|
|
const id = idOf(args.id);
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const commit = commitOf("commit", args.commit);
|
|
if (!(await noteHolds(deps.run, deps.container, owner, repo, commit, "mesh-plan", id))) {
|
|
throw never(askOf(args), `the note on ${commit.slice(0, 12)} holds no line of the act ${id}`);
|
|
}
|
|
return { id, held: true, performed: true };
|
|
},
|
|
mergePull: async (args: Record<string, unknown>): Promise<MergePullAnswer> => {
|
|
// Each member as the forge holds it: merged, with its merge commit; the call that merged them, when one is kept.
|
|
const members = membersOf(args.pulls);
|
|
const out = await Promise.all(members.map(async (m) => {
|
|
const p = await gitea.getPullRequest(m.owner, m.repo, m.number);
|
|
return p.merged && p.merge_commit_sha
|
|
? { repository: m.repository, number: m.number, state: "merged" as const, commit: p.merge_commit_sha }
|
|
: { repository: m.repository, number: m.number, state: "refused" as const, why: `not merged: the forge holds it ${p.state === "open" ? "open" : "closed"}` };
|
|
}));
|
|
if (!out.some((m) => m.state === "merged")) throw never(askOf(args), "the forge merged none of its pull requests");
|
|
const named = (c: { members: { repository: string; number: number; state: string }[] }) =>
|
|
out.every((m) => m.state !== "merged" || c.members.some((k) => k.repository === m.repository && k.number === m.number && k.state === "merged"));
|
|
const kept = (await deps.calls?.all())?.find(named);
|
|
return { ...(kept ? { call: kept.call } : {}), members: out };
|
|
},
|
|
tag: async (args: Record<string, unknown>): Promise<TagAnswer> => {
|
|
const { owner, repo } = repositoryOf(args.repository);
|
|
const tag = String(args.tag ?? "");
|
|
const commit = commitOf("commit", args.commit);
|
|
const at = await gitea.tagCommit(owner, repo, tag);
|
|
if (at !== commit) throw never(askOf(args), at === null ? `the forge holds no tag ${tag}` : `${tag} is at ${at.slice(0, 12)}, not ${commit.slice(0, 12)}`);
|
|
return { tag, performed: true };
|
|
},
|
|
};
|
|
}
|