From 1fda575316d1dc221c8407c310234b7f6529e802 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Wed, 28 May 2025 00:17:01 +0000 Subject: [PATCH] perf: optimize setupBranch to avoid unnecessary default branch fetch Only fetch repository default branch when actually needed: - Skip initial fetch when baseBranch is provided - Fetch default branch at end only for return value and GitHub Actions output - Eliminates unnecessary API call when users specify base branch Co-authored-by: ashwin-ant --- src/github/operations/branch.ts | 47 ++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/github/operations/branch.ts b/src/github/operations/branch.ts index c456f96..29414ac 100644 --- a/src/github/operations/branch.ts +++ b/src/github/operations/branch.ts @@ -29,16 +29,6 @@ export async function setupBranch( const entityNumber = context.entityNumber; const isPR = context.isPR; - // Get the default branch first - const repoResponse = await octokits.rest.repos.get({ - owner, - repo, - }); - const defaultBranch = repoResponse.data.default_branch; - - // Use the provided base branch or fall back to default branch - const sourceBranch = baseBranch || defaultBranch; - if (isPR) { const prData = githubData.contextData as GitHubPullRequest; const prState = prData.state; @@ -46,7 +36,7 @@ export async function setupBranch( // Check if PR is closed or merged if (prState === "CLOSED" || prState === "MERGED") { console.log( - `PR #${entityNumber} is ${prState}, creating new branch from default...`, + `PR #${entityNumber} is ${prState}, creating new branch from source...`, ); // Fall through to create a new branch like we do for issues } else { @@ -61,7 +51,13 @@ export async function setupBranch( console.log(`Successfully checked out PR branch for PR #${entityNumber}`); - // For open PRs, return branch info + // For open PRs, we need the actual default branch for the return value + const repoResponse = await octokits.rest.repos.get({ + owner, + repo, + }); + const defaultBranch = repoResponse.data.default_branch; + return { defaultBranch, currentBranch: branchName, @@ -69,6 +65,24 @@ export async function setupBranch( } } + // Determine source branch - use baseBranch if provided, otherwise fetch default + let sourceBranch: string; + let defaultBranch: string; + + if (baseBranch) { + // Use provided base branch for source + sourceBranch = baseBranch; + // We'll fetch default branch later only when needed for return/output + } else { + // No base branch provided, fetch the default branch to use as source + const repoResponse = await octokits.rest.repos.get({ + owner, + repo, + }); + defaultBranch = repoResponse.data.default_branch; + sourceBranch = defaultBranch; + } + // 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}...`); @@ -110,6 +124,15 @@ export async function setupBranch( `Successfully created and checked out new branch: ${newBranch}`, ); + // Fetch default branch only now if we haven't already (when baseBranch was provided) + if (baseBranch) { + const repoResponse = await octokits.rest.repos.get({ + owner, + repo, + }); + defaultBranch = repoResponse.data.default_branch; + } + // Set outputs for GitHub Actions core.setOutput("CLAUDE_BRANCH", newBranch); core.setOutput("DEFAULT_BRANCH", defaultBranch);