Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Repo Assist] Batch open-PR fetch in submit: N API calls β†’ 1 - #12

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/perf-batch-submit-pr-fetch-2026-04-18-73f48e3b4e4d8cbd
Draft

github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/perf-batch-submit-pr-fetch-2026-04-18-73f48e3b4e4d8cbd

Conversation

@github-actions

Copy link
Copy Markdown

πŸ€– This is an automated pull request from Repo Assist, an AI assistant.

Summary

gh stack submit called FindPRForBranch once per active branch to check whether a PR already exists β€” an N sequential GraphQL round-trips for an N-branch stack.

This PR adds FindOpenPRsForBranches that sends a single batched GraphQL query using field aliases (one aliased pullRequests per branch, filtered to OPEN state), then pre-fetches all results before the submit loop.

Impact

Stack size Before After
3 active branches 3 API calls 1 API call
10 active branches 10 API calls 1 API call

All gh stack submit runs benefit.

How it works

FindOpenPRsForBranches constructs a raw GraphQL query:

{
  repository(owner: "owner", name: "repo") {
    b0: pullRequests(headRefName: "feat/01", states: [OPEN], first: 1) { nodes { id number ... } }
    b1: pullRequests(headRefName: "feat/02", states: [OPEN], first: 1) { nodes { id number ... } }
  }
}

This uses gql.Do(rawQuery, nil, &resp) to send a raw query string, bypassing the struct-based query builder which doesn't support dynamic aliases. Results are distributed back to a map[branch]β†’*PullRequest.

Graceful fallback

If FindOpenPRsForBranches returns an error (e.g. API version mismatch), submit falls back to the original per-branch FindPRForBranch calls β€” no user-visible degradation, just slower.

Changes

  • internal/github/github.go: New FindOpenPRsForBranches([]string) (map[string]*PullRequest, error) using gql.Do with dynamically-constructed aliased query (OPEN state filter)
  • internal/github/client_interface.go: Added FindOpenPRsForBranches to ClientOps interface
  • internal/github/mock_client.go: Added FindOpenPRsForBranchesFn; default returns nil, nil to trigger graceful fallback to FindPRForBranch in existing tests
  • cmd/submit.go: Pre-fetch with FindOpenPRsForBranches, fall back gracefully on error
  • cmd/submit_test.go: TestSubmit_BatchFetchesOpenPRs (verifies batch is called once, per-branch not called); TestSubmit_BatchFetchFallback (verifies fallback works on error)

Relationship to PR #7

PR #7 adds a similar batch optimization for syncStackPRs (fetching any-state PRs). This PR is independent β€” it targets the submit PR-lookup path (open PRs only) and can be reviewed and merged separately.

Test Status

Tests could not be run in this environment because the Go toolchain requires network access to proxy.golang.org / sum.golang.org (blocked by firewall). The CI workflow will run the full test suite. The fallback path (openPRs == nil β†’ FindPRForBranch) ensures existing tests continue to work without modification.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@11c9a2c442e519ff2b427bf58679f5a525353f76

gh stack submit called FindPRForBranch once per active branch β€”
an N sequential GraphQL round-trips for an N-branch stack.

Add FindOpenPRsForBranches that constructs a single batched GraphQL
query using field aliases (one aliased pullRequests field per branch,
filtered to OPEN state), matching FindPRForBranch semantics.

Pre-fetch all open PRs in one call before the submit loop, then look
up each branch's result from the map. If the batch call fails, submit
falls back to per-branch FindPRForBranch so the command still works.

For a 5-branch stack, this reduces 5 sequential API calls to 1 during
the PR create/update phase of submit.

Changes:
- github.Client: add FindOpenPRsForBranches([]string) using gql.Do
  with a dynamically-constructed aliased query (OPEN state only)
- ClientOps interface: add FindOpenPRsForBranches
- MockClient: add FindOpenPRsForBranchesFn; default returns nil to
  trigger fallback to FindPRForBranch in existing tests
- submit.go: pre-fetch with FindOpenPRsForBranches, fall back gracefully
- submit_test.go: add TestSubmit_BatchFetchesOpenPRs (verifies batch
  called once, per-branch not called) and TestSubmit_BatchFetchFallback
  (verifies fallback on error)

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants