Skip to content

perf(github): read the scanned repository in one GraphQL query - #646

Closed
Exypte wants to merge 1 commit into
feature/637-github-graphql-analyzefrom
feature/637-github-graphql-scanned-repo
Closed

Exypte wants to merge 1 commit into
feature/637-github-graphql-analyzefrom
feature/637-github-graphql-scanned-repo

Conversation

@Exypte

@Exypte Exypte commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

perf

What this PR does / why we need it:

Step 2 of #637. A GitHub analyze read the scanned repository over REST with one request per question: the repository itself, each branch branchMustBeProtected names (main, master, dev, production by default), and the releases for the default token check. On github.com these now come from one GraphQL query, sent before the first read and kept for the run: default branch, visibility, full name, the named branches with their protected flag, and the authors of the latest 20 releases.

REST's protected flag is true for a classic rule or a ruleset, and no single GraphQL field gives it. The query reads refUpdateRule (the classic rule a non-admin token can see) and rules (the rulesets that apply). Together they matched REST on about 620 branches of the 11 corpus repositories; refUpdateRule alone missed ruleset-only branches such as every branch of vercel/next.js.

Measured on the 11 repositories of the #619 corpus, against a build of step 1 (#639):

step 1 this PR
Requests 939 876
REST core requests 477 403 (−16%)
GraphQL queries (points) 86 (86) 97 (97)
Total time 420 s 318 s

The JSON reports are identical on all 11 apart from cliVersion, with the same exit codes. Each repository saves 5 to 12 REST requests for one GraphQL query. Both sides ran back to back against the live API, so part of the time difference is noise.

Which issue(s) this PR is related to:

Refs #637 (step 3 follows in its own PR)

This PR is stacked on #639 (step 1), which builds on #619. It stays a draft until those are merged, then gets rebased on main.

Special notes for your reviewer:

  • The branch listing (used for wildcard patterns) stays on REST. With the protected flag, a 10-page GraphQL listing took 35 s on next.js and 69 s on terraform, against 9 s and 12 s over REST, for the same number of round trips.
  • The protection details of a protected branch (/protection, /rules/branches/{b}) stay on REST, as do environments and the Actions workflows and runs, as the issue says.
  • GitHub Enterprise Server keeps all these reads on REST, since its schema may not have Ref.rules. The existing REST tests now run against an Enterprise host so they keep covering that path.
  • Errors: a repository GitHub answers null for reads like REST's 404, a FORBIDDEN one like a 403, and a query that fails as a whole keeps each read's REST behavior for the same status. A branch GitHub could not answer for is reported as an error, never as absent.
  • A branch the prefetch did not ask costs one more query for the missing names only.
  • New code is in github/scanned_repo.go; control/task_github.go sends the query with the configured branch names. The protection-details code is now one helper shared by the targeted and listing paths.

Does this PR introduce a user-facing change?

Analyzing a GitHub repository on github.com reads the repository's default branch, visibility, named branches and latest releases in one GraphQL query instead of several REST requests. Findings and scores are unchanged.

How was this tested?

  • Unit tests: github/scanned_repo_test.go (one query answers every read, values sent as variables, lazy load and the extra query for a missing branch, not found, unauthorized, server error, a failed branch alias, no token, GHES staying on REST) and TestRunGitHubAnalysis_PrefetchesTheScannedRepository in control/task_github_test.go. A test that would send this query to github.com without a stub fails instead of reaching the network.
  • make build, make test, and golangci-lint on ./github/... and ./control/.... Lint reports one govet finding in control/catalog.go that is already on main.
  • plumber analyze --debug on the 11 corpus repositories with this branch and with step 1, comparing the JSON reports, exit codes and request reports (table above).

AI usage disclosure:

YES. Claude Code wrote the code and tests under my direction, checked the GraphQL fields against REST, ran the corpus comparison and drafted this description. I reviewed the changes.

🤖 Generated with Claude Code

On github.com, the default branch, visibility and full name of the
scanned repository, the branches branchMustBeProtected names with their
protected flag, and the authors of its latest releases now come from one
GraphQL query, sent before the first read and kept for the run. The
protected flag is a classic rule or an applying ruleset, which matches
REST's protected flag on the 11 corpus repositories.

The branch listing and each protected branch's protection details stay
on REST: listing with the protected flag over GraphQL is several times
slower. GitHub Enterprise Server keeps all these reads on REST, since its
schema may lack Ref.rules.

Refs #637

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant