Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ The process's GitHub session (#767) sends its own REST requests through gidgethu
- gidgethub is async only. Every synchronous caller goes through the session's portal, and no caller creates an event loop of its own.
- Every `GitHubClient` call (a pull request, its files and diff, a file at a commit, a merge base, an organization's repositories and an issue comment) and SSH key registration (the authentication key and the signing key) are session requests. None builds its own client or reads a `Location` header. A redirect to github.com, which the old diff code followed with the token copied onto it, is now refused.
- Tests fake the session through the repository's HTTP transport seam, the one the `web` fixture uses. The `github_api` fixture installs `GitHubApiFake` (`tests/support/fakes/github.py`), a fake GitHub API on `httpx2.MockTransport`, with `install_transport` in place of the transport the entry point would build, and its contract suite holds it to GitHub's recorded answers. The entry point, its timeouts, the redirect handling and the origin hook stay real, and every request is still vetted at the egress level the entry point was asked for. The plan gave the session constructor a `transport=` argument instead. That was dropped: it needed a path around egress vetting in product code, used only by tests.
- `run_gh` no longer pages. The project's own listings (a pull request's files, reviews, timeline and check runs, the reconcile listings, and the roadmap store's REST listings) read through `paginate()`; the roadmap store keeps gh for its board commands. `devops gh api --paginate` reads a REST GET of a path through `pages()`, which gives each page whole, so each page counts as one request with its status (#4737). Its `--jq` filter runs on each page through jq.py, as gh runs it. gh keeps the other paginated reads of `devops gh api`: GraphQL, a write, a gh placeholder, a full URL, `--template`, `--cache` and any other gh flag, such as `--slurp`. `run_gh` hands gh those flags as given, since gh follows `Link` itself. gh walks the pages in one process, so the whole walk runs under one timeout (`DEFAULT_GH_API_PAGINATE_TIMEOUT_SECONDS`), counts as one request, and a rate-limited walk is retried from page 1. The metrics row reads open issues with `GhCliClient.api(paginate=True)` until #3941 moves that read. `run_gh_async` and its `aiolimiter` limiter are gone.
- `run_gh` no longer pages. The project's own listings (a pull request's files, reviews, timeline and check runs, the reconcile listings, and the roadmap store's REST listings) read through `paginate()`; the roadmap store keeps gh for its board commands. `devops gh api --paginate` reads a REST GET of a path through `pages()`, which gives each page whole, so each page counts as one request with its status (#4737). It adds `per_page=100` when the endpoint does not set `per_page`, as gh adds it, so it reads the pages that gh reads (#5100). Its `--jq` filter runs on each page through jq.py, as gh runs it. gh keeps the other paginated reads of `devops gh api`: GraphQL, a write, a gh placeholder, a full URL, `--template`, `--cache` and any other gh flag, such as `--slurp`. `run_gh` hands gh those flags as given, since gh follows `Link` itself. gh walks the pages in one process, so the whole walk runs under one timeout (`DEFAULT_GH_API_PAGINATE_TIMEOUT_SECONDS`), counts as one request, and a rate-limited walk is retried from page 1. The metrics row reads open issues with `GhCliClient.api(paginate=True)` until #3941 moves that read. `run_gh_async` and its `aiolimiter` limiter are gone.
- A failed gh command is classified in the same module, `classify_gh_failure`, from gh's exit status (4 is "requires authentication") and the gh-output table in `config/constants.py`, matched against what gh wrote to stderr, never GitHub's answer on stdout. The table is the one place gh's failures are classified by gh's text; `_is_branch_unprotected_error`, `create_repository_issue`, `_report_create_failure` and `extract_retry_after` still match gh's text of their own. Its errors carry the status gh names as an int, like an HTTP failure's. `graphql()` waits for the first item that moves a GraphQL call off gh.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-12
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Design

## Decisions

- **Add the page size in `session_path`.** The read (`listing_text`) and the dry-run plan of `devops gh api --paginate` both use `session_path`, so the plan names the URL that the read sends. `GitHubSession.pages()` stays a reader of the path it gets: the rule is a rule of `gh api`, not of the session.
- **Copy each step of `addPerPage` (gh v2.103.1, `pkg/cmd/api/pagination.go`).** The path splits at its first `?`, as `strings.IndexRune` splits it. `urllib.parse.parse_qs` with `keep_blank_values=True` reads the query, and the check takes the first value of `per_page`, as Go's `url.Values.Get` gives it. A first value that is empty, or a `per_page` with no `=`, does not count as set, as in gh. The pair goes after `&` when the path has a `?`, else after `?`, so the path is the path of gh, also for an endpoint that ends in `?`. `urlencode` writes the pair.
- **The page size is `DEFAULT_GH_REST_PER_PAGE` (100).** gh writes the same number (`addPerPage(requestPath, 100, params)` in `pkg/cmd/api/api.go`).
- **The `params` step of gh does not apply.** gh keeps a `per_page` given with `-f` or `-F`. `reads_through_session` gives each read with a gh flag to gh, so the session never gets such a read.

## Rejected alternatives

- **Read and write the path with `urlsplit` and `urlunsplit`.** `urlunsplit(urlsplit('/repos/o/r/labels?'))` gives `/repos/o/r/labels`: the empty query and its `?` go. The path would then be `labels?per_page=100`, not the `labels?&per_page=100` of gh. `urlsplit` also moves a `#` fragment out of the query, which gh does not do.
- **Copy the parse errors of Go's `url.ParseQuery`.** gh adds `per_page=100` also when Go cannot parse the query (a `;` in it, or a `%` with no two hex digits after it), even when the query sets `per_page`. `parse_qs` reads such a query with no error, so the session keeps the `per_page` of the endpoint there. To copy gh, the module needs a written copy of the parse rules of Go. httpx2 sends such a query as it is typed (`httpx2.URL('https://api.github.com').join('/repos/o/r/issues?q=%zz;x&per_page=5')` keeps it), so the only difference is the second `per_page` that gh appends to an endpoint that sets `per_page` and also holds a query that Go refuses. The cost of a written parser is more than the value for such an endpoint.
- **Use `listing_path` of `roadmap/stores/github_store.py`.** It appends `per_page` to every endpoint after a check for `?` in the text, so an endpoint that sets `per_page` gets a second one. Its callers build the endpoint and know that it sets no `per_page`, so it does not need the rule of gh.
- **Add the page size in `GitHubSession.pages()` or `RestTransport.read_page`.** The session reads the path that its caller gives. A caller that wants another page size would have no way to ask for it, and the rule of `gh api` would move into code that is not about `gh api`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Proposal: #5100

## Why

Since #4737, `devops gh api <endpoint> --paginate` reads a REST GET through the GitHub session. gh adds `per_page=100` to a paginated REST read when the endpoint does not set `per_page`, but the session path sends the endpoint as it is. GitHub then answers with its default page size of 30. A read with no `per_page` sends about 3.3 times more requests than gh, the `--jq` output of each page is different from the output of gh, and a listing of more than 3,000 items stops at the cap of 100 pages with `GitHubUnreadError`, where gh read it in pages of 100. Closes #5100.

## What Changes

- `session_path` in `src/devops_cli/github/api_pages.py` adds `per_page=100` (`DEFAULT_GH_REST_PER_PAGE`) to the endpoint when the first `per_page` of its query has no value, with the rule of gh 2.103.1 (`addPerPage` in `pkg/cmd/api/pagination.go`). An endpoint that sets `per_page` keeps it.
- The read and its dry-run plan use the same path, so the plan names the URL that the read sends.

The change alters the requests that a `devops gh api` command sends and the lines that it prints, which a consumer observes, so it has a spec delta.

## Capabilities

- `github-api-metrics` (MODIFIED requirement): a paginated `devops gh api` read of a REST GET counts each page, and asks for pages of 100 when the endpoint sets no `per_page`.

## Impact

- A paginated read with no `per_page` sends the number of requests that gh sent: for example, about 51 requests for 5,000 issues, not about 170.
- A listing of up to 10,000 items fits in the cap of 100 pages again.
- The `--jq` output of each page is the output of gh, because the pages are the pages of gh.
- The Service runs this code. No setting, manifest or dependency changes. The change fits one pull request.

## Feasibility

- Source, gh v2.103.1 `pkg/cmd/api/api.go`: `if opts.Paginate && !isGraphQL { requestPath = addPerPage(requestPath, 100, params) }`.
- Source, gh v2.103.1 `pkg/cmd/api/pagination.go`, `addPerPage`: it returns the path unchanged when `params` holds `per_page`, or when the text after the first `?` parses with `url.ParseQuery` and `Get("per_page")` is not empty. Else it appends `per_page=100` after `&` when the path has a `?`, or after `?`.
- Command output on main at a04e7a94c (v0.119.0 code), 2026-11-12: `devops gh api 'repos/dan-petty/devops-cli/labels' --paginate --jq length` printed `30`, `30`, `30` and `5` (4 requests).
- Command output, same day, on the gh path (the extra gh flag `-H 'Accept: application/vnd.github+json'` keeps the read with the installed gh): the same read printed `95` (1 request).
- Census, `git grep -n 'session_path(\|listing_text(' -- src`: `commands/gh.py` (2: the dry-run plan and the read) and `github/api_pages.py` (3: the two definitions and the call of `pages`). `GitHubSession.pages()` has one caller, `listing_text`. So the change reaches only `devops gh api --paginate`.

## Existing Tool Considered

- gh owns the rule. The session must send the request itself, because a gh walk counts as one request (#4737). The fix copies the rule of gh and cites its source.
- The Python standard library reads the query (`urllib.parse.parse_qs`) and writes the pair (`urllib.parse.urlencode`).
- Built: about 10 lines in `session_path`. Cost: about 60,000 tokens and 1 hour to build, and about 2,000 tokens a year to compare the rule with `addPerPage` when gh changes.
- The change deletes no internal code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## MODIFIED Requirements

### Requirement: A paginated devops gh api read of a REST GET counts each page
`devops gh api --paginate` SHALL read a REST GET of a path through the GitHub session, so that each page adds 1 to `devops_cli_github_requests_total` with the status of its answer. The session SHALL add `per_page=100` when the endpoint sets no `per_page`, as gh does. gh SHALL keep a GraphQL read, a write, an endpoint with a gh placeholder, a full URL, `--template`, `--cache` and any other gh flag. `--jq` SHALL run on each page.

#### Scenario: A listing of 4 pages
- **WHEN** `devops gh api 'repos/<owner>/<repo>/issues?state=open&per_page=1' --paginate` reads 4 linked pages from the GitHub fake
- **THEN** `devops_cli_github_requests_total` has one point with the `resource` `core`, the `status` `200` and the value 4
- **AND** the output is one JSON array that holds the items of the 4 pages

#### Scenario: A jq filter on each page
- **WHEN** `devops gh api ... --paginate --jq 'length, .[].number'` reads a page of 2 items and a page of 1 item
- **THEN** the output is the lines `2`, `1`, `2`, `1` and `3`

#### Scenario: An endpoint with no per_page
- **WHEN** `devops gh api 'repos/<owner>/<repo>/labels' --paginate --jq length` reads a listing of 150 labels from the GitHub fake
- **THEN** the first request URL holds `per_page=100`
- **AND** the session sends 2 requests, and the output is the lines `100` and `50`

#### Scenario: An endpoint that sets per_page
- **WHEN** `devops gh api 'repos/<owner>/<repo>/labels?per_page=50' --paginate` runs
- **THEN** the request URL holds `per_page=50` and no other `per_page`

#### Scenario: A read that gh keeps
- **WHEN** `devops gh api 'repos/:owner/:repo/issues' --paginate` runs
- **THEN** gh gets the argv with `--paginate`, and the GitHub session sends no request
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Tasks

## 1. Behavior: a paginated devops gh api read asks for pages of 100

- [x] 1.1 Write failing tests: `session_path` adds `per_page=100` with the rule of gh `addPerPage` and keeps a `per_page` that the endpoint sets
- Verification: `$ uv run devops ci test tests/github/test_github_api_pages.py`
- [x] 1.2 Write failing command tests: 150 labels with no `per_page` take 2 requests of 100 and `--jq length` prints `100` and `50`, an endpoint with `per_page=50` keeps it, and a dry run plans `per_page=100`
- Verification: `$ uv run devops ci test tests/commands/test_gh_cmd.py`
- [x] 1.3 Add the page size in `session_path`
- Verification: `$ uv run devops ci test tests/github/test_github_api_pages.py tests/commands/test_gh_cmd.py`

## 2. Documents

- [x] 2.1 State the page size of the session read of `devops gh api --paginate` in ADR 0006
- Verification: `$ uv run devops ci docs`

## Workflow follow-up

- Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).
11 changes: 10 additions & 1 deletion openspec/specs/github-api-metrics/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ The rate limiter SHALL log each mandatory pause and each quota window reset at D
- **THEN** the pause record has level DEBUG, and no record of that pause has level INFO.

### Requirement: A paginated devops gh api read of a REST GET counts each page
`devops gh api --paginate` SHALL read a REST GET of a path through the GitHub session, so that each page adds 1 to `devops_cli_github_requests_total` with the status of its answer. gh SHALL keep a GraphQL read, a write, an endpoint with a gh placeholder, a full URL, `--template`, `--cache` and any other gh flag. `--jq` SHALL run on each page.
`devops gh api --paginate` SHALL read a REST GET of a path through the GitHub session, so that each page adds 1 to `devops_cli_github_requests_total` with the status of its answer. The session SHALL add `per_page=100` when the endpoint sets no `per_page`, as gh does. gh SHALL keep a GraphQL read, a write, an endpoint with a gh placeholder, a full URL, `--template`, `--cache` and any other gh flag. `--jq` SHALL run on each page.

#### Scenario: A listing of 4 pages
- **WHEN** `devops gh api 'repos/<owner>/<repo>/issues?state=open&per_page=1' --paginate` reads 4 linked pages from the GitHub fake
Expand All @@ -104,6 +104,15 @@ The rate limiter SHALL log each mandatory pause and each quota window reset at D
- **WHEN** `devops gh api ... --paginate --jq 'length, .[].number'` reads a page of 2 items and a page of 1 item
- **THEN** the output is the lines `2`, `1`, `2`, `1` and `3`

#### Scenario: An endpoint with no per_page
- **WHEN** `devops gh api 'repos/<owner>/<repo>/labels' --paginate --jq length` reads a listing of 150 labels from the GitHub fake
- **THEN** the first request URL holds `per_page=100`
- **AND** the session sends 2 requests, and the output is the lines `100` and `50`

#### Scenario: An endpoint that sets per_page
- **WHEN** `devops gh api 'repos/<owner>/<repo>/labels?per_page=50' --paginate` runs
- **THEN** the request URL holds `per_page=50` and no other `per_page`

#### Scenario: A read that gh keeps
- **WHEN** `devops gh api 'repos/:owner/:repo/issues' --paginate` runs
- **THEN** gh gets the argv with `--paginate`, and the GitHub session sends no request
25 changes: 22 additions & 3 deletions src/devops_cli/github/api_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
- a read that `--cache` keeps in the response cache of `run_gh`;
- any other gh flag, such as `--slurp`, `-H` or `-f`.

`session_path` asks for pages of 100 items when the endpoint does not set `per_page`, as gh
asks for them (#5100). So the session reads the pages that gh reads, in the same number of
requests.

`printed_pages` prints the pages as gh 2.103.1 prints them. With no filter, the items of array
pages print as one JSON array, and each object page prints whole, on its own line. A `--jq`
filter runs on each page through jq.py, the Python binding of jq, and each value prints on its
Expand All @@ -30,14 +34,17 @@
import re
from collections.abc import Iterable, Iterator, Sequence
from typing import Any, Final
from urllib.parse import urlsplit
from urllib.parse import parse_qs, urlencode, urlsplit

from devops_cli.config.constants import CONST_GH_API_DEFAULT_METHOD, CONST_GH_API_GRAPHQL_ENDPOINT
from devops_cli.config.defaults import DEFAULT_GH_REST_PER_PAGE
from devops_cli.exceptions.validation import ValidationError

_GH_PLACEHOLDER: Final = re.compile(r":(?:owner|repo|branch)\b|\{[a-z]+\}")
"""The placeholders gh fills from its base repository (gh 2.103.1, `fillPlaceholders`)."""

_PER_PAGE: Final = "per_page"

# gh prints a number that a filter selects with no decimals when it is whole, else with two.
_WHOLE_NUMBER_FORMAT: Final = ".0f"
_FRACTION_FORMAT: Final = ".2f"
Expand Down Expand Up @@ -68,8 +75,20 @@ def reads_through_session(

def session_path(endpoint: str) -> str:
"""The path of `endpoint` under GitHub's API, with or without its leading slash, as gh
reads it."""
return "/" + endpoint.lstrip("/")
reads it, with the page size gh asks for.

gh 2.103.1 adds `per_page=100` to a paginated REST read when the first `per_page` of its
query has no value (`addPerPage` in `pkg/cmd/api/pagination.go`, #5100): after `&` when the
endpoint has a `?`, else after `?`. gh also keeps a `per_page` given with `-f` or `-F`, but
such a read has a gh flag, so gh sends it.
"""
path = "/" + endpoint.lstrip("/")
_, question_mark, query = path.partition("?")
# The first value, as Go's `url.Values.Get` gives it to gh.
if parse_qs(query, keep_blank_values=True).get(_PER_PAGE, [""])[0]:
return path
page_size = urlencode({_PER_PAGE: DEFAULT_GH_REST_PER_PAGE})
return f"{path}{'&' if question_mark else '?'}{page_size}"


def listing_text(endpoint: str, jq_filter: str | None) -> Iterator[str]:
Expand Down
Loading