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 <ashwin-ant@users.noreply.github.com>
This commit is contained in:
claude[bot]
2025-05-31 05:37:04 +00:00
committed by GitHub
parent e8d2b8d5df
commit b0ca8faf6b

View File

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