91 lines
2.7 KiB
TypeScript
91 lines
2.7 KiB
TypeScript
/**
|
|
* Single source of truth for reading, updating, deleting, and creating "progress comments".
|
|
*/
|
|
|
|
import type { Gitea } from "./gitea.ts";
|
|
|
|
export type ProgressCommentType = "issue";
|
|
|
|
export type ProgressComment = {
|
|
id: number;
|
|
type: ProgressCommentType;
|
|
};
|
|
|
|
export function parseProgressComment(
|
|
raw: { id: string; type: ProgressCommentType } | null | undefined
|
|
): ProgressComment | undefined {
|
|
if (!raw?.id) return undefined;
|
|
const id = parseInt(raw.id, 10);
|
|
if (Number.isNaN(id) || id <= 0) return undefined;
|
|
return { id, type: raw.type };
|
|
}
|
|
|
|
interface ApiCtx {
|
|
gitea: Gitea;
|
|
owner: string;
|
|
repo: string;
|
|
}
|
|
|
|
interface GiteaComment {
|
|
id: number;
|
|
body?: string | null;
|
|
html_url?: string;
|
|
node_id?: string;
|
|
}
|
|
|
|
export async function getProgressComment(
|
|
ctx: ApiCtx,
|
|
comment: ProgressComment
|
|
): Promise<{ id: number; body: string | undefined; html_url: string }> {
|
|
const r = await ctx.gitea.request("GET /repos/{owner}/{repo}/issues/comments/{id}", {
|
|
owner: ctx.owner, repo: ctx.repo, id: comment.id,
|
|
});
|
|
const data = r.data as GiteaComment;
|
|
return { id: data.id, body: data.body ?? undefined, html_url: data.html_url ?? "" };
|
|
}
|
|
|
|
export async function updateProgressComment(
|
|
ctx: ApiCtx,
|
|
comment: ProgressComment,
|
|
body: string
|
|
): Promise<{ id: number; body: string | undefined; html_url: string; node_id: string | undefined }> {
|
|
const r = await ctx.gitea.request("PATCH /repos/{owner}/{repo}/issues/comments/{id}", {
|
|
owner: ctx.owner, repo: ctx.repo, id: comment.id, body,
|
|
});
|
|
const data = r.data as GiteaComment;
|
|
return { id: data.id, body: data.body ?? undefined, html_url: data.html_url ?? "", node_id: undefined };
|
|
}
|
|
|
|
export async function deleteProgressCommentApi(ctx: ApiCtx, comment: ProgressComment): Promise<void> {
|
|
await ctx.gitea.request("DELETE /repos/{owner}/{repo}/issues/comments/{id}", {
|
|
owner: ctx.owner, repo: ctx.repo, id: comment.id,
|
|
});
|
|
}
|
|
|
|
export type CreateProgressCommentTarget =
|
|
| { kind: "issue"; issueNumber: number }
|
|
| { kind: "reviewReply"; pullNumber: number; replyToCommentId: number };
|
|
|
|
export interface CreatedProgressComment {
|
|
comment: ProgressComment;
|
|
body: string | undefined;
|
|
html_url: string;
|
|
}
|
|
|
|
export async function createLeapingProgressComment(
|
|
ctx: ApiCtx,
|
|
target: CreateProgressCommentTarget,
|
|
body: string
|
|
): Promise<CreatedProgressComment> {
|
|
const issueNumber = target.kind === "issue" ? target.issueNumber : target.pullNumber;
|
|
const r = await ctx.gitea.request("POST /repos/{owner}/{repo}/issues/{index}/comments", {
|
|
owner: ctx.owner, repo: ctx.repo, index: issueNumber, body,
|
|
});
|
|
const data = r.data as GiteaComment;
|
|
return {
|
|
comment: { id: data.id, type: "issue" },
|
|
body: data.body ?? undefined,
|
|
html_url: data.html_url ?? "",
|
|
};
|
|
}
|