75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import { Octokit } from "@octokit/rest";
|
|
import { fetchWorkflowRunInfo } from "./api.ts";
|
|
import { getGitHubInstallationToken, parseRepoContext } from "./github.ts";
|
|
|
|
/**
|
|
* Get progress comment ID from environment variable or database.
|
|
*/
|
|
function getProgressCommentIdFromEnv(): number | null {
|
|
const envCommentId = process.env.PULLFROG_PROGRESS_COMMENT_ID;
|
|
if (envCommentId) {
|
|
const parsed = parseInt(envCommentId, 10);
|
|
if (!Number.isNaN(parsed)) {
|
|
return parsed;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export async function reportErrorToComment({
|
|
error,
|
|
title,
|
|
}: {
|
|
error: string;
|
|
title?: string;
|
|
}): Promise<void> {
|
|
const formattedError = title ? `${title}\n\n${error}` : `❌ ${error}`;
|
|
|
|
// try to get comment ID from env var first, then from database if needed
|
|
let commentId = getProgressCommentIdFromEnv();
|
|
|
|
// if not in env var, try fetching from database using run ID
|
|
if (!commentId) {
|
|
const runId = process.env.GITHUB_RUN_ID;
|
|
if (runId) {
|
|
try {
|
|
const workflowRunInfo = await fetchWorkflowRunInfo(runId);
|
|
if (workflowRunInfo.progressCommentId) {
|
|
const parsed = parseInt(workflowRunInfo.progressCommentId, 10);
|
|
if (!Number.isNaN(parsed)) {
|
|
commentId = parsed;
|
|
// cache it in env var for future use
|
|
process.env.PULLFROG_PROGRESS_COMMENT_ID = workflowRunInfo.progressCommentId;
|
|
}
|
|
}
|
|
} catch {
|
|
// database fetch failed, continue without comment ID
|
|
}
|
|
}
|
|
}
|
|
|
|
// if no comment ID available, try using reportProgress (requires MCP context)
|
|
if (!commentId) {
|
|
try {
|
|
const { reportProgress } = await import("../mcp/comment.ts");
|
|
await reportProgress({ body: formattedError });
|
|
return;
|
|
} catch {
|
|
// MCP context not available, can't create/update comment
|
|
return;
|
|
}
|
|
}
|
|
|
|
// update comment directly using GitHub API
|
|
const repoContext = parseRepoContext();
|
|
const token = getGitHubInstallationToken();
|
|
const octokit = new Octokit({ auth: token });
|
|
|
|
await octokit.rest.issues.updateComment({
|
|
owner: repoContext.owner,
|
|
repo: repoContext.name,
|
|
comment_id: commentId,
|
|
body: formattedError,
|
|
});
|
|
}
|