From aa82b5b5acc70409c4471a6393f7af8cd8f0b00f Mon Sep 17 00:00:00 2001 From: Ashwin Bhat Date: Wed, 28 May 2025 21:22:54 -0700 Subject: [PATCH] fix: properly handle base branch throughout the action workflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix TypeScript error where defaultBranch was used before being assigned - Replace DEFAULT_BRANCH with BASE_BRANCH in subsequent workflow steps - Update PR creation and branch comparison to use the actual base branch - Ensure custom base_branch input is respected in all operations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- action.yml | 2 +- src/entrypoints/prepare.ts | 7 ++++++- src/entrypoints/update-comment-link.ts | 10 +++++----- src/github/operations/branch-cleanup.ts | 4 ++-- src/github/operations/branch.ts | 12 +++++++----- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/action.yml b/action.yml index 3d339ed..dc8f3e7 100644 --- a/action.yml +++ b/action.yml @@ -147,7 +147,7 @@ runs: TRIGGER_COMMENT_ID: ${{ github.event.comment.id }} CLAUDE_BRANCH: ${{ steps.prepare.outputs.CLAUDE_BRANCH }} IS_PR: ${{ github.event.issue.pull_request != null || github.event_name == 'pull_request_review_comment' }} - DEFAULT_BRANCH: ${{ steps.prepare.outputs.DEFAULT_BRANCH }} + BASE_BRANCH: ${{ steps.prepare.outputs.BASE_BRANCH }} CLAUDE_SUCCESS: ${{ steps.claude-code.outputs.conclusion == 'success' }} OUTPUT_FILE: ${{ steps.claude-code.outputs.execution_file || '' }} TRIGGER_USERNAME: ${{ github.event.comment.user.login || github.event.issue.user.login || github.event.pull_request.user.login || github.event.sender.login || github.triggering_actor || github.actor || '' }} diff --git a/src/entrypoints/prepare.ts b/src/entrypoints/prepare.ts index 2708687..c6cbf05 100644 --- a/src/entrypoints/prepare.ts +++ b/src/entrypoints/prepare.ts @@ -63,7 +63,12 @@ async function run() { // Step 8: Setup branch const baseBranch = process.env.BASE_BRANCH; - const branchInfo = await setupBranch(octokit, githubData, context, baseBranch); + const branchInfo = await setupBranch( + octokit, + githubData, + context, + baseBranch, + ); // Step 9: Update initial comment with branch link (only for issues that created a new branch) if (branchInfo.claudeBranch) { diff --git a/src/entrypoints/update-comment-link.ts b/src/entrypoints/update-comment-link.ts index daa2cec..12f2fcf 100644 --- a/src/entrypoints/update-comment-link.ts +++ b/src/entrypoints/update-comment-link.ts @@ -18,7 +18,7 @@ async function run() { const commentId = parseInt(process.env.CLAUDE_COMMENT_ID!); const githubToken = process.env.GITHUB_TOKEN!; const claudeBranch = process.env.CLAUDE_BRANCH; - const defaultBranch = process.env.DEFAULT_BRANCH || "main"; + const baseBranch = process.env.BASE_BRANCH || "main"; const triggerUsername = process.env.TRIGGER_USERNAME; const context = parseGitHubContext(); @@ -92,7 +92,7 @@ async function run() { owner, repo, claudeBranch, - defaultBranch, + baseBranch, ); // Check if we need to add PR URL when we have a new branch @@ -102,7 +102,7 @@ async function run() { // Check if comment already contains a PR URL const serverUrlPattern = serverUrl.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); const prUrlPattern = new RegExp( - `${serverUrlPattern}\\/.+\\/compare\\/${defaultBranch.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\.\\.\\.`, + `${serverUrlPattern}\\/.+\\/compare\\/${baseBranch.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\.\\.\\.`, ); const containsPRUrl = currentBody.match(prUrlPattern); @@ -113,7 +113,7 @@ async function run() { await octokit.rest.repos.compareCommitsWithBasehead({ owner, repo, - basehead: `${defaultBranch}...${claudeBranch}`, + basehead: `${baseBranch}...${claudeBranch}`, }); // If there are changes (commits or file changes), add the PR URL @@ -128,7 +128,7 @@ async function run() { const prBody = encodeURIComponent( `This PR addresses ${entityType.toLowerCase()} #${context.entityNumber}\n\nGenerated with [Claude Code](https://claude.ai/code)`, ); - const prUrl = `${serverUrl}/${owner}/${repo}/compare/${defaultBranch}...${claudeBranch}?quick_pull=1&title=${prTitle}&body=${prBody}`; + const prUrl = `${serverUrl}/${owner}/${repo}/compare/${baseBranch}...${claudeBranch}?quick_pull=1&title=${prTitle}&body=${prBody}`; prLink = `\n[Create a PR](${prUrl})`; } } catch (error) { diff --git a/src/github/operations/branch-cleanup.ts b/src/github/operations/branch-cleanup.ts index 9c1334d..662a474 100644 --- a/src/github/operations/branch-cleanup.ts +++ b/src/github/operations/branch-cleanup.ts @@ -6,7 +6,7 @@ export async function checkAndDeleteEmptyBranch( owner: string, repo: string, claudeBranch: string | undefined, - defaultBranch: string, + baseBranch: string, ): Promise<{ shouldDeleteBranch: boolean; branchLink: string }> { let branchLink = ""; let shouldDeleteBranch = false; @@ -18,7 +18,7 @@ export async function checkAndDeleteEmptyBranch( await octokit.rest.repos.compareCommitsWithBasehead({ owner, repo, - basehead: `${defaultBranch}...${claudeBranch}`, + basehead: `${baseBranch}...${claudeBranch}`, }); // If there are no commits, mark branch for deletion diff --git a/src/github/operations/branch.ts b/src/github/operations/branch.ts index 29414ac..109f54d 100644 --- a/src/github/operations/branch.ts +++ b/src/github/operations/branch.ts @@ -67,8 +67,8 @@ export async function setupBranch( // Determine source branch - use baseBranch if provided, otherwise fetch default let sourceBranch: string; - let defaultBranch: string; - + let defaultBranch: string | undefined; + if (baseBranch) { // Use provided base branch for source sourceBranch = baseBranch; @@ -85,7 +85,9 @@ export async function setupBranch( // Creating a new branch for either an issue or closed/merged PR const entityType = isPR ? "pr" : "issue"; - console.log(`Creating new branch for ${entityType} #${entityNumber} from source branch: ${sourceBranch}...`); + console.log( + `Creating new branch for ${entityType} #${entityNumber} from source branch: ${sourceBranch}...`, + ); const timestamp = new Date() .toISOString() @@ -125,7 +127,7 @@ export async function setupBranch( ); // Fetch default branch only now if we haven't already (when baseBranch was provided) - if (baseBranch) { + if (!defaultBranch) { const repoResponse = await octokits.rest.repos.get({ owner, repo, @@ -135,7 +137,7 @@ export async function setupBranch( // Set outputs for GitHub Actions core.setOutput("CLAUDE_BRANCH", newBranch); - core.setOutput("DEFAULT_BRANCH", defaultBranch); + core.setOutput("BASE_BRANCH", sourceBranch); return { defaultBranch, claudeBranch: newBranch,