sin-github-issues / src /github.ts
delqhi's picture
Bootstrap SIN-GitHub-Issues runtime
a4dc18c verified
Raw
History Blame Contribute Delete
2.09 kB
import { runCommand, runJsonCommand } from './command.js';
type IssueRow = { number: number; title: string; url: string; state: string };
export async function ensureIssue(input: {
repo: string;
title: string;
body: string;
labels?: string[];
}) {
const rows = await runJsonCommand<IssueRow[]>('gh', ['issue', 'list', '--repo', input.repo, '--state', 'all', '--limit', '200', '--json', 'number,title,url,state']);
const existing = rows.find((row) => row.title.trim().toLowerCase() === input.title.trim().toLowerCase());
if (existing) {
await runCommand('gh', ['issue', 'edit', String(existing.number), '--repo', input.repo, '--body', input.body], undefined);
for (const label of input.labels || []) {
try {
await runCommand('gh', ['issue', 'edit', String(existing.number), '--repo', input.repo, '--add-label', label], undefined);
} catch {
}
}
return { created: false, issue: existing, issueNumber: existing.number, issueUrl: existing.url };
}
let created;
try {
created = await runCommand('gh', ['issue', 'create', '--repo', input.repo, '--title', input.title, '--body', input.body, ...(input.labels || []).flatMap((label) => ['--label', label])], undefined);
} catch {
created = await runCommand('gh', ['issue', 'create', '--repo', input.repo, '--title', input.title, '--body', input.body], undefined);
}
const url = created.stdout.trim();
const issueNumber = Number.parseInt(url.split('/').pop() || '', 10);
return { created: true, issueUrl: url, issueNumber };
}
export async function commentIssue(repo: string, issueNumber: number, body: string) {
await runCommand('gh', ['issue', 'comment', String(issueNumber), '--repo', repo, '--body', body], undefined);
return { ok: true, repo, issueNumber };
}
export async function closeIssue(repo: string, issueNumber: number, comment?: string) {
const args = ['issue', 'close', String(issueNumber), '--repo', repo];
if (comment) args.push('--comment', comment);
await runCommand('gh', args, undefined);
return { ok: true, repo, issueNumber };
}