Repository navigation
feat(repo-stats)!: read PR data through gh/glab and never handle a token - #3
Merged
Merged
Conversation
… not per config dir
API data now comes from `gh api` (GitHub GraphQL) and `glab api` (GitLab REST), which use their own login: the OS keychain, GH_TOKEN/GITLAB_TOKEN, or the GitLab CI job token. repo_stats.py no longer reads, holds or sends a credential. - Remove http_json, the redirect guard, the loopback proxy bypass and resolve_token. A Cli class runs every request and refuses anything outside a read-only allowlist: GraphQL queries (no mutations) for gh, and GET on the MR list, commits and notes endpoints for glab. - Keep the host allowlist. gh sends GH_ENTERPRISE_TOKEN and glab sends GITLAB_TOKEN to whatever host they are given, and glab without --hostname follows the working directory's remote. So the host is github.com/gitlab.com from the remote or the new --hostname flag, it is always passed as --hostname, and the CLI runs from an empty temp dir with telemetry, prompts, update checks and debug dumps off. - In GitLab CI, a remote on CI_SERVER_HOST is trusted without --hostname. When the login cannot read MR commits (a job token), one probe stops further lookups and first-commit times come from local merge commits, marked as inferred. - git runs without credential variables in its environment. - --api-url and --token-env are replaced by --hostname; using them fails with a message that names the replacement. Tests: fake gh/glab (tests/fake_cli) forward each call to the existing mock servers, find tokens like the real CLIs and reject disallowed flags; clean_env() always puts them first on PATH. New tests cover the allowlist, --hostname, the temp working directory, the CLI environment, lookalike hosts, CI host trust, the job-token fallback and git's environment. The HTTP redirect and proxy tests are removed with that code. Checked end to end with real gh 2.103.2 and glab 1.124.0. BREAKING CHANGE: --api-url and --token-env are removed, and GITHUB_TOKEN/GITLAB_TOKEN are no longer read by repo_stats.py. Install and log in to gh or glab (or set GH_TOKEN/GITLAB_TOKEN for the CLI), and use --provider with --hostname for self-hosted servers.
…hain `run.sh --store github` opens GitHub's fine-grained token page with read-only Contents and Pull requests (30 days) filled in, for the remote's owner, and saves the pasted token. `run.sh --store gitlab --project grp/proj` asks glab for a read_api Reporter project token and falls back to the personal token page with only read_api selected. `run.sh [options]` then hands the saved token to gh/glab (GH_TOKEN, GH_ENTERPRISE_TOKEN for that --hostname, or GITLAB_TOKEN) for that run only; without one, gh/glab use their own login. `--forget` deletes it. The token is read at a hidden prompt and reaches the keychain on stdin through shell builtins, so it never appears in shell history, a file, or any process's arguments. Uses the macOS Keychain or secret-tool on Linux. Tests use a fake secret-tool and skip on macOS.
README: quick start without tokens; a "Logging in" section for local use, run.sh, GitHub Actions, GitLab CI and scheduled jobs; and a Security section on the read-only allowlist, host trust and what remains. The plan records how the implementation differs from it.
husam-e
force-pushed
the
claude/repo-stats-cli-auth
branch
from
October 27, 2026 02:01
1f60e17 to
7aecb13
Compare
husam-e
marked this pull request as ready for review
October 27, 2026 02:10
husam-e
marked this pull request as draft
October 27, 2026 02:20
Run the CLI with no gh on PATH and assert it prints "warning: gh is not installed", says PRs are inferred from git history, and ends with "source: git". The existing test only checked the warning in-process.
--since and --until now take a UTC date (2025-01-15), month (2025-01) or year (2025), or a duration back from now (90d, 6w, 6m, 1y). --until covers the whole day, month or year it names, so `--since 2025-01 --until 2025-07` is January through July, and `--since 1y` is the last year. Months are calendar months, clamped to the month's last day. A window whose start is not before its end is an error.
…rrors Keep the script simple: the removed flags are no longer parsed at all, so argparse rejects them like any unknown option.
run.sh was more than the tool needs: repo-stats already works from an existing gh/glab login or, without one, from local git history with a warning. Remove the script, its tests and the fake `glab token create`. README: a "Time window" section lists every accepted --since/--until value (UTC day, month, year, or Nd/Nw/Nm/Ny back from now) with examples, and "On your machine" now says to use the existing CLI login, with a one-line way to run with a read-only token instead.
- Delete docs/plan-cli-auth.adoc; the README describes the design. - Cli is a plain object: no context manager or per-run temporary directory (the CLI runs from the system temp directory; the explicit --hostname is what stops glab following a remote), no internal tool and hostname re-checks (collect() validates --hostname), no one-use helper. - Of the environment changes, keep only GLAB_SEND_TELEMETRY=false (glab telemetry sends its own request with the credential) and removing the debug switches. The prompt, update-check, pager and colour variables were not needed: run without a terminal, both CLIs print clean --include output and send banners to stderr.
husam-e
marked this pull request as ready for review
October 28, 2026 01:09
g-husam
added a commit
that referenced
this pull request
Oct 28, 2026
…ned CI, 404 probe) (#5) Follow-ups from the #3 review. They fix GitHub Enterprise logins, run the CLI from a private directory, pin the CI examples, and stop a single 404 from turning off GitLab commit lookups for every MR. The tests no longer reach a real `gh` or depend on timing. ### What changed ```diff Cli(tool, host) - hint: "in CI set GH_TOKEN" # wrong for GitHub Enterprise Server + hint: "in CI set " + ci_login_hint(tool, host) # GH_TOKEN for github.com / *.ghe.com, else GH_ENTERPRISE_TOKEN - _run: cwd = tempfile.gettempdir() # shared /tmp: another user can plant a .git + _run: cwd = private TemporaryDirectory # new, empty, 0700, one per run, removed after it + close() / with-block collect - cli = Cli(...) + with Cli(...) as cli fetch_gitlab - probe refused on 401 / 403 / 404 # one missing MR turned off commits for all MRs + probe refused on 401 / 403 # a 404 only affects that MR retries - time.sleep(...) + _backoff(...) # tests patch this instead of time.sleep for everyone ``` ### Docs (`README.adoc`) ```diff Logging in + NOTE: GitHub Enterprise Server uses GH_ENTERPRISE_TOKEN, not GH_TOKEN GitHub Actions + ref: <full commit SHA> # pin the tool: it runs next to the token + GHES: set GH_ENTERPRISE_TOKEN instead GitLab CI - git clone --depth 1 …scripts.git # unpinned HEAD + git fetch --depth 1 …scripts.git "$REPO_STATS_SHA" && git checkout FETCH_HEAD Security - runs from the system temp directory + runs from a new, empty, private temp directory ``` ### Tests ```text tests/test_repo_stats.py ├── B3 login hint names GH_ENTERPRISE_TOKEN for a GHES host (and GH_TOKEN alone is not enough) ├── B3 glab with no login → anonymous 401 → git fallback naming `glab auth login` ├── B3 "gh missing" test puts only git on PATH (/usr/bin also holds a real gh, which it used to run) ├── B4/F2/… patch rs._backoff instead of time.sleep (subprocess's own sleeps made the retry tests flaky) ├── G1 every CLI call runs in one private repo-stats-cli-* dir, mode 0700, removed after the run ├── G2 a 404 on the probed MR's commits does not stop lookups for the other MRs └── review cycles (#4) close the Cli the GitLab helper creates ``` ### Not changed - The review also said the "no PRs in the selected date range" warning ignores `--until`. That was wrong: the API filters only by `--since`, so an empty result with only `--until` means the repository has no PRs. The message is correct as it is. - No code left over from #3 needed deleting: no unused imports, functions or constants were found. ### Testing - `python3 -m unittest test_repo_stats`: 222 tests pass on Python 3.13 (rebased on main with #4), with no `ResourceWarning`. Run with `GIT_CONFIG_GLOBAL=/dev/null`, because a global `tag.gpgSign` breaks the fixtures' annotated tags. - Not run: the Playwright UI tests (no node here) and Python 3.8. `template.html` is unchanged. - The real `glab` no-login behaviour and the job-token permissions are still checked only against the fakes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
repo-stats stops handling API tokens. PR data comes from
gh api/glab api, which use their own login (keychain, a token variable, or the GitLab CI job token). If the CLI is missing or not logged in, repo-stats warns and infers PRs from local git history.--since/--untilalso accept months, years and relative durations.How a request flows
sequenceDiagram participant RS as repo_stats.py participant W as Cli wrapper participant CLI as gh / glab participant API as GitHub / GitLab RS->>RS: pick the API host (see Host rules) RS->>W: graphql(query) or get(endpoint) W->>W: allowlist, GraphQL query only or GET on 3 MR endpoints W->>CLI: api --hostname HOST --include, from the temp dir, debug and glab telemetry off CLI->>API: request with the CLI's own login API-->>CLI: status, headers, JSON CLI-->>W: --include output W->>W: retry 429, 5xx and timeouts with backoff W-->>RS: JSON and headers Note over RS,W: no CLI, no login, or an API error → warning, PRs from git history (--source api exits instead)Host rules
The CLIs send environment tokens to any
--hostnamethey get, so the host is never taken from an arbitrary remote:What changed
Time window
--since--until2025-01-15/2025-07/202590d,6w,6m,1y--since 2025-01 --until 2025-07is January–July 2025.--since 1yis the last year.--sincemust be before--until.Breaking
--api-urland--token-envare removed (use--provider+--hostname).repo_stats.pyreads no token. The CLIs read their own:gh:GH_TOKEN/GITHUB_TOKENfor github.com and*.ghe.com;GH_ENTERPRISE_TOKEN/GITHUB_ENTERPRISE_TOKENfor GitHub Enterprise Server.glab:GITLAB_TOKEN/GITLAB_ACCESS_TOKEN/OAUTH_TOKEN, orCI_JOB_TOKENwithGLAB_ENABLE_CI_AUTOLOGIN=true.GITLAB_PRIVATE_TOKEN, andGITHUB_TOKENfor GitHub Enterprise Server (it used to work with--api-url). Those setups now fall back to git history with a warning.Testing
gh2.103.2 (this repo) andglab1.124.0 (gitlab-org/cli), both with personal logins.repo_stats.pyhas no POSIX-only code, but the test suite does (fake CLIs,sh).Still open
gh auth token, and defaultgh/glablogins can write. A read-only token given asGH_TOKEN/GITLAB_TOKENlimits the damage.--hostnameis trusted.Follow-up PR (after merge)
CLI_HELPand the README Actions example sayGH_TOKEN, butghneedsGH_ENTERPRISE_TOKENthere, so the run falls back to git history.mkdtemp()dir instead.glabnot-logged-in detection and the job-token permissions are not tested against real GitLab.--until-only window.