From b0ca8faf6be064b2b18bb6a851c0c9b0bc58cbf7 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Sat, 31 May 2025 05:37:04 +0000 Subject: [PATCH] fix: replace REST API branch listing with GraphQL query for better performance - Replaced REST API listBranches call with GraphQL query that filters branches by prefix - This fixes the issue where repositories with >100 branches could miss existing Claude branches - The GraphQL query directly filters by prefix, making it more efficient and accurate Co-authored-by: ashwin-ant --- src/github/operations/branch.ts | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/github/operations/branch.ts b/src/github/operations/branch.ts index a389410..e73128d 100644 --- a/src/github/operations/branch.ts +++ b/src/github/operations/branch.ts @@ -88,19 +88,36 @@ export async function setupBranch( if (!isPR) { // Check for existing Claude branches for this issue try { - const { data: branches } = await octokits.rest.repos.listBranches({ + // Use GraphQL to efficiently search for branches with a specific prefix + const query = ` + query($owner: String!, $repo: String!, $prefix: String!) { + repository(owner: $owner, name: $repo) { + refs(refPrefix: "refs/heads/", query: $prefix, first: 100) { + nodes { + name + } + } + } + } + `; + + const response = await octokits.graphql<{ + repository: { + refs: { + nodes: Array<{ name: string }>; + }; + }; + }>(query, { owner, repo, - per_page: 100, + prefix: `claude/issue-${entityNumber}-`, }); - // Look for existing branches with pattern claude/issue-{entityNumber}-* - const existingBranch = branches.find(branch => - branch.name.startsWith(`claude/issue-${entityNumber}-`) - ); + const branches = response.repository.refs.nodes; - if (existingBranch) { - branchToUse = existingBranch.name; + if (branches.length > 0) { + // Use the first matching branch (could be sorted by date in future) + branchToUse = branches[0].name; isReusedBranch = true; console.log(`Found existing Claude branch for issue #${entityNumber}: ${branchToUse}`); }