fix: handle optional entityNumber for TypeScript

- Add runtime checks in files that require entityNumber
- These files are only used by tag mode which always has entityNumber
- Agent mode (workflow_dispatch/schedule) doesn't use these files
This commit is contained in:
km-anthropic
2025-07-29 11:16:09 -07:00
parent 1c2e7e788d
commit 5da8dc78e6
5 changed files with 37 additions and 25 deletions

View File

@@ -125,8 +125,10 @@ export function prepareContext(
const isPR = context.isPR; const isPR = context.isPR;
// Get PR/Issue number from entityNumber // Get PR/Issue number from entityNumber
const prNumber = isPR ? context.entityNumber.toString() : undefined; const prNumber =
const issueNumber = !isPR ? context.entityNumber.toString() : undefined; 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 // Extract trigger username and comment data based on event type
let triggerUsername: string | undefined; let triggerUsername: string | undefined;
@@ -801,15 +803,18 @@ export async function createPrompt(
context: ParsedGitHubContext, context: ParsedGitHubContext,
) { ) {
try { try {
// Tag mode requires a comment ID // Prepare the context for prompt generation
if (mode.name === "tag" && !modeContext.commentId) { let claudeCommentId: string = "";
throw new Error("Tag mode requires a comment ID for prompt generation"); 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( const preparedContext = prepareContext(
context, context,
modeContext.commentId?.toString() || "", claudeCommentId,
modeContext.baseBranch, modeContext.baseBranch,
modeContext.claudeBranch, modeContext.claudeBranch,
); );

View File

@@ -24,6 +24,13 @@ async function run() {
const context = parseGitHubContext(); const context = parseGitHubContext();
const { owner, repo } = context.repository; 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 octokit = createOctokit(githubToken);
const serverUrl = GITHUB_SERVER_URL; const serverUrl = GITHUB_SERVER_URL;
@@ -73,7 +80,7 @@ async function run() {
const { data: pr } = await octokit.rest.pulls.get({ const { data: pr } = await octokit.rest.pulls.get({
owner, owner,
repo, repo,
pull_number: context.entityNumber, pull_number: entityNumber,
}); });
console.log(`PR state: ${pr.state}`); console.log(`PR state: ${pr.state}`);
console.log(`PR comments count: ${pr.comments}`); console.log(`PR comments count: ${pr.comments}`);

View File

@@ -21,6 +21,12 @@ export async function createInitialComment(
context: ParsedGitHubContext, context: ParsedGitHubContext,
) { ) {
const { owner, repo } = context.repository; 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 jobRunLink = createJobRunLink(owner, repo, context.runId);
const initialBody = createCommentBody(jobRunLink); const initialBody = createCommentBody(jobRunLink);
@@ -36,7 +42,7 @@ export async function createInitialComment(
const comments = await octokit.rest.issues.listComments({ const comments = await octokit.rest.issues.listComments({
owner, owner,
repo, repo,
issue_number: context.entityNumber, issue_number: entityNumber,
}); });
const existingComment = comments.data.find((comment) => { const existingComment = comments.data.find((comment) => {
const idMatch = comment.user?.id === CLAUDE_APP_BOT_ID; const idMatch = comment.user?.id === CLAUDE_APP_BOT_ID;
@@ -59,7 +65,7 @@ export async function createInitialComment(
response = await octokit.rest.issues.createComment({ response = await octokit.rest.issues.createComment({
owner, owner,
repo, repo,
issue_number: context.entityNumber, issue_number: entityNumber,
body: initialBody, body: initialBody,
}); });
} }
@@ -68,7 +74,7 @@ export async function createInitialComment(
response = await octokit.rest.pulls.createReplyForReviewComment({ response = await octokit.rest.pulls.createReplyForReviewComment({
owner, owner,
repo, repo,
pull_number: context.entityNumber, pull_number: entityNumber,
comment_id: context.payload.comment.id, comment_id: context.payload.comment.id,
body: initialBody, body: initialBody,
}); });
@@ -77,7 +83,7 @@ export async function createInitialComment(
response = await octokit.rest.issues.createComment({ response = await octokit.rest.issues.createComment({
owner, owner,
repo, repo,
issue_number: context.entityNumber, issue_number: entityNumber,
body: initialBody, body: initialBody,
}); });
} }
@@ -95,7 +101,7 @@ export async function createInitialComment(
const response = await octokit.rest.issues.createComment({ const response = await octokit.rest.issues.createComment({
owner, owner,
repo, repo,
issue_number: context.entityNumber, issue_number: entityNumber,
body: initialBody, body: initialBody,
}); });

View File

@@ -141,7 +141,7 @@ export async function prepareMcpConfig(
GITHUB_TOKEN: process.env.ACTIONS_TOKEN, GITHUB_TOKEN: process.env.ACTIONS_TOKEN,
REPO_OWNER: owner, REPO_OWNER: owner,
REPO_NAME: repo, REPO_NAME: repo,
PR_NUMBER: context.entityNumber.toString(), PR_NUMBER: context.entityNumber?.toString() || "",
RUNNER_TEMP: process.env.RUNNER_TEMP || "/tmp", RUNNER_TEMP: process.env.RUNNER_TEMP || "/tmp",
}, },
}; };

View File

@@ -56,15 +56,9 @@ export const tagMode: Mode = {
// Check if actor is human // Check if actor is human
await checkHumanActor(octokit.rest, context); await checkHumanActor(octokit.rest, context);
// Create initial tracking comment // Create initial tracking comment (always created for tag mode)
let commentId: number | undefined; const commentData = await createInitialComment(octokit.rest, context);
let commentData: const commentId = commentData.id;
| Awaited<ReturnType<typeof createInitialComment>>
| undefined;
if (this.shouldCreateTrackingComment()) {
commentData = await createInitialComment(octokit.rest, context);
commentId = commentData.id;
}
// Fetch GitHub data - entity events always have entityNumber and isPR // Fetch GitHub data - entity events always have entityNumber and isPR
if (!context.entityNumber || context.isPR === undefined) { if (!context.entityNumber || context.isPR === undefined) {
@@ -85,7 +79,7 @@ export const tagMode: Mode = {
// Configure git authentication if not using commit signing // Configure git authentication if not using commit signing
if (!context.inputs.useCommitSigning) { if (!context.inputs.useCommitSigning) {
try { try {
await configureGitAuth(githubToken, context, commentData?.user || null); await configureGitAuth(githubToken, context, commentData.user);
} catch (error) { } catch (error) {
console.error("Failed to configure git authentication:", error); console.error("Failed to configure git authentication:", error);
throw error; throw error;
@@ -110,7 +104,7 @@ export const tagMode: Mode = {
branch: branchInfo.claudeBranch || branchInfo.currentBranch, branch: branchInfo.claudeBranch || branchInfo.currentBranch,
baseBranch: branchInfo.baseBranch, baseBranch: branchInfo.baseBranch,
additionalMcpConfig, additionalMcpConfig,
claudeCommentId: commentId?.toString() || "", claudeCommentId: commentId.toString(),
allowedTools: context.inputs.allowedTools, allowedTools: context.inputs.allowedTools,
context, context,
}); });