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;
// 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,
);

View File

@@ -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}`);

View File

@@ -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,
});

View File

@@ -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",
},
};

View File

@@ -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<ReturnType<typeof createInitialComment>>
| 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,
});