diff --git a/src/create-prompt/index.ts b/src/create-prompt/index.ts index 27b32816..884e36b9 100644 --- a/src/create-prompt/index.ts +++ b/src/create-prompt/index.ts @@ -125,8 +125,10 @@ export function prepareContext( const isPR = context.isPR; // Get PR/Issue number from entityNumber - const prNumber = isPR ? context.entityNumber.toString() : undefined; - const issueNumber = !isPR ? context.entityNumber.toString() : undefined; + const prNumber = + isPR && context.entityNumber ? context.entityNumber.toString() : undefined; + const issueNumber = + !isPR && context.entityNumber ? context.entityNumber.toString() : undefined; // Extract trigger username and comment data based on event type let triggerUsername: string | undefined; @@ -801,15 +803,18 @@ export async function createPrompt( context: ParsedGitHubContext, ) { try { - // Tag mode requires a comment ID - if (mode.name === "tag" && !modeContext.commentId) { - throw new Error("Tag mode requires a comment ID for prompt generation"); + // Prepare the context for prompt generation + let claudeCommentId: string = ""; + if (mode.name === "tag") { + if (!modeContext.commentId) { + throw new Error("Tag mode requires a comment ID for prompt generation"); + } + claudeCommentId = modeContext.commentId.toString(); } - // Prepare the context for prompt generation const preparedContext = prepareContext( context, - modeContext.commentId?.toString() || "", + claudeCommentId, modeContext.baseBranch, modeContext.claudeBranch, ); diff --git a/src/entrypoints/update-comment-link.ts b/src/entrypoints/update-comment-link.ts index 85b24552..f3fef4aa 100644 --- a/src/entrypoints/update-comment-link.ts +++ b/src/entrypoints/update-comment-link.ts @@ -24,6 +24,13 @@ async function run() { const context = parseGitHubContext(); const { owner, repo } = context.repository; + + // This script is only called for entity-based events + if (!context.entityNumber) { + throw new Error("update-comment-link requires an entity number"); + } + const entityNumber = context.entityNumber; + const octokit = createOctokit(githubToken); const serverUrl = GITHUB_SERVER_URL; @@ -73,7 +80,7 @@ async function run() { const { data: pr } = await octokit.rest.pulls.get({ owner, repo, - pull_number: context.entityNumber, + pull_number: entityNumber, }); console.log(`PR state: ${pr.state}`); console.log(`PR comments count: ${pr.comments}`); diff --git a/src/github/operations/comments/create-initial.ts b/src/github/operations/comments/create-initial.ts index 1243035b..9e2dd88a 100644 --- a/src/github/operations/comments/create-initial.ts +++ b/src/github/operations/comments/create-initial.ts @@ -21,6 +21,12 @@ export async function createInitialComment( context: ParsedGitHubContext, ) { const { owner, repo } = context.repository; + + // This function is only called for entity-based events + if (!context.entityNumber) { + throw new Error("createInitialComment requires an entity number"); + } + const entityNumber = context.entityNumber; const jobRunLink = createJobRunLink(owner, repo, context.runId); const initialBody = createCommentBody(jobRunLink); @@ -36,7 +42,7 @@ export async function createInitialComment( const comments = await octokit.rest.issues.listComments({ owner, repo, - issue_number: context.entityNumber, + issue_number: entityNumber, }); const existingComment = comments.data.find((comment) => { const idMatch = comment.user?.id === CLAUDE_APP_BOT_ID; @@ -59,7 +65,7 @@ export async function createInitialComment( response = await octokit.rest.issues.createComment({ owner, repo, - issue_number: context.entityNumber, + issue_number: entityNumber, body: initialBody, }); } @@ -68,7 +74,7 @@ export async function createInitialComment( response = await octokit.rest.pulls.createReplyForReviewComment({ owner, repo, - pull_number: context.entityNumber, + pull_number: entityNumber, comment_id: context.payload.comment.id, body: initialBody, }); @@ -77,7 +83,7 @@ export async function createInitialComment( response = await octokit.rest.issues.createComment({ owner, repo, - issue_number: context.entityNumber, + issue_number: entityNumber, body: initialBody, }); } @@ -95,7 +101,7 @@ export async function createInitialComment( const response = await octokit.rest.issues.createComment({ owner, repo, - issue_number: context.entityNumber, + issue_number: entityNumber, body: initialBody, }); diff --git a/src/mcp/install-mcp-server.ts b/src/mcp/install-mcp-server.ts index c8cb1253..95f08664 100644 --- a/src/mcp/install-mcp-server.ts +++ b/src/mcp/install-mcp-server.ts @@ -141,7 +141,7 @@ export async function prepareMcpConfig( GITHUB_TOKEN: process.env.ACTIONS_TOKEN, REPO_OWNER: owner, REPO_NAME: repo, - PR_NUMBER: context.entityNumber.toString(), + PR_NUMBER: context.entityNumber?.toString() || "", RUNNER_TEMP: process.env.RUNNER_TEMP || "/tmp", }, }; diff --git a/src/modes/tag/index.ts b/src/modes/tag/index.ts index 5731c94a..c19d181f 100644 --- a/src/modes/tag/index.ts +++ b/src/modes/tag/index.ts @@ -56,15 +56,9 @@ export const tagMode: Mode = { // Check if actor is human await checkHumanActor(octokit.rest, context); - // Create initial tracking comment - let commentId: number | undefined; - let commentData: - | Awaited> - | undefined; - if (this.shouldCreateTrackingComment()) { - commentData = await createInitialComment(octokit.rest, context); - commentId = commentData.id; - } + // Create initial tracking comment (always created for tag mode) + const commentData = await createInitialComment(octokit.rest, context); + const commentId = commentData.id; // Fetch GitHub data - entity events always have entityNumber and isPR if (!context.entityNumber || context.isPR === undefined) { @@ -85,7 +79,7 @@ export const tagMode: Mode = { // Configure git authentication if not using commit signing if (!context.inputs.useCommitSigning) { try { - await configureGitAuth(githubToken, context, commentData?.user || null); + await configureGitAuth(githubToken, context, commentData.user); } catch (error) { console.error("Failed to configure git authentication:", error); throw error; @@ -110,7 +104,7 @@ export const tagMode: Mode = { branch: branchInfo.claudeBranch || branchInfo.currentBranch, baseBranch: branchInfo.baseBranch, additionalMcpConfig, - claudeCommentId: commentId?.toString() || "", + claudeCommentId: commentId.toString(), allowedTools: context.inputs.allowedTools, context, });