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 <ashwin-ant@users.noreply.github.com>
This commit is contained in:
claude[bot]
2025-05-28 00:17:01 +00:00
committed by GitHub
parent db6d3621d7
commit 1fda575316

View File

@@ -29,16 +29,6 @@ export async function setupBranch(
const entityNumber = context.entityNumber; const entityNumber = context.entityNumber;
const isPR = context.isPR; 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) { if (isPR) {
const prData = githubData.contextData as GitHubPullRequest; const prData = githubData.contextData as GitHubPullRequest;
const prState = prData.state; const prState = prData.state;
@@ -46,7 +36,7 @@ export async function setupBranch(
// Check if PR is closed or merged // Check if PR is closed or merged
if (prState === "CLOSED" || prState === "MERGED") { if (prState === "CLOSED" || prState === "MERGED") {
console.log( 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 // Fall through to create a new branch like we do for issues
} else { } else {
@@ -61,7 +51,13 @@ export async function setupBranch(
console.log(`Successfully checked out PR branch for PR #${entityNumber}`); 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 { return {
defaultBranch, defaultBranch,
currentBranch: branchName, 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 // Creating a new branch for either an issue or closed/merged PR
const entityType = isPR ? "pr" : "issue"; 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}...`);
@@ -110,6 +124,15 @@ export async function setupBranch(
`Successfully created and checked out new branch: ${newBranch}`, `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 // Set outputs for GitHub Actions
core.setOutput("CLAUDE_BRANCH", newBranch); core.setOutput("CLAUDE_BRANCH", newBranch);
core.setOutput("DEFAULT_BRANCH", defaultBranch); core.setOutput("DEFAULT_BRANCH", defaultBranch);