From 2b941dd4bfcda23fb5a7291dc774a21026b5d476 Mon Sep 17 00:00:00 2001 From: sunerpy Date: Thu, 9 Jul 2026 12:27:34 +0800 Subject: [PATCH] fix(cli): authenticate self-update with GITHUB_TOKEN/GH_TOKEN and hint on rate limit self-update never passed an auth token to the self_update crate (its builder defaults to None and does not read the environment), so codegraph self-update had no way to lift the anonymous GitHub API rate limit and failed with an opaque HTTP 403. Resolve a token from GITHUB_TOKEN then GH_TOKEN (gh CLI convention) via an injectable env-getter, rejecting empty/whitespace values, and apply it to every Update builder. On any GitHub-API call failure, surface the underlying error and append an actionable hint showing the exact reauth command. Behavior is unchanged when no token is set (anonymous). CLI-only and additive; extraction and golden byte-stability are untouched. --- crates/codegraph-cli/src/main.rs | 107 ++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 8 deletions(-) diff --git a/crates/codegraph-cli/src/main.rs b/crates/codegraph-cli/src/main.rs index 88241ac5..12af3e11 100644 --- a/crates/codegraph-cli/src/main.rs +++ b/crates/codegraph-cli/src/main.rs @@ -813,6 +813,30 @@ fn should_skip_update(current: &str, latest: &str, force: bool, has_explicit_tag !self_update::version::bump_is_greater(current, latest).unwrap_or(false) } +/// Resolve a GitHub API token via injected env-getter: `GITHUB_TOKEN` then +/// `GH_TOKEN` (the `gh` CLI convention). Empty/whitespace values are rejected so +/// `GITHUB_TOKEN=` sends no broken auth header; `None` preserves anonymous mode. +/// Getter is injected (not `std::env` directly) to stay testable without +/// `set_var`, which is unsafe in edition 2024. +fn resolve_github_token(get: impl Fn(&str) -> Option) -> Option { + for key in ["GITHUB_TOKEN", "GH_TOKEN"] { + if let Some(value) = get(key) { + let trimmed = value.trim(); + if !trimmed.is_empty() { + return Some(trimmed.to_owned()); + } + } + } + None +} + +/// Actionable hint appended on any GitHub-API failure. A 403 is almost always +/// rate-limit exhaustion; we always surface this (rather than fragile-parsing +/// the status out of the error string) so the user learns the exact remedy. +fn self_update_rate_limit_hint() -> &'static str { + "if this is a GitHub API rate limit, authenticate with:\n GITHUB_TOKEN=$(gh auth token) codegraph self-update" +} + fn cmd_self_update(check: bool, force: bool, tag: Option) -> Result<()> { use self_update::cargo_crate_version; @@ -825,6 +849,10 @@ fn cmd_self_update(check: bool, force: bool, tag: Option) -> Result<()> .current_version(cargo_crate_version!()) .show_download_progress(true) .no_confirm(force); + // Auth all builders uniformly (self_update ignores GITHUB_TOKEN/GH_TOKEN). + if let Some(token) = resolve_github_token(|k| std::env::var(k).ok()) { + builder.auth_token(&token); + } builder }; @@ -833,9 +861,12 @@ fn cmd_self_update(check: bool, force: bool, tag: Option) -> Result<()> let updater = configure() .build() .context("configuring the self-update backend")?; - let latest = updater - .get_latest_release() - .context("querying the latest GitHub release")?; + let latest = updater.get_latest_release().with_context(|| { + format!( + "querying the latest GitHub release\n\nhint: {}", + self_update_rate_limit_hint() + ) + })?; let current = cargo_crate_version!(); if self_update::version::bump_is_greater(current, &latest.version).unwrap_or(false) { println!("codegraph {current} -> {} available", latest.version); @@ -860,9 +891,12 @@ fn cmd_self_update(check: bool, force: bool, tag: Option) -> Result<()> let probe = configure() .build() .context("configuring the self-update backend")?; - let latest = probe - .get_latest_release() - .context("querying the latest GitHub release")?; + let latest = probe.get_latest_release().with_context(|| { + format!( + "querying the latest GitHub release\n\nhint: {}", + self_update_rate_limit_hint() + ) + })?; let current = cargo_crate_version!(); if should_skip_update(current, &latest.version, force, false) { println!("codegraph {current} is already up to date"); @@ -878,7 +912,12 @@ fn cmd_self_update(check: bool, force: bool, tag: Option) -> Result<()> .build() .context("configuring the self-update backend")?; - let status = updater.update().context("performing the self-update")?; + let status = updater.update().with_context(|| { + format!( + "performing the self-update\n\nhint: {}", + self_update_rate_limit_hint() + ) + })?; if status.updated() { println!("Updated codegraph to {}", status.version()); } else { @@ -4079,7 +4118,18 @@ fn print_files_tree(files: &[FileRecord], max_depth: Option) { #[cfg(test)] mod self_update_tests { - use super::{latest_update_tag, should_skip_update}; + use super::{ + latest_update_tag, resolve_github_token, self_update_rate_limit_hint, should_skip_update, + }; + use std::collections::HashMap; + + fn getter(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option { + let map: HashMap = pairs + .iter() + .map(|(k, v)| ((*k).to_owned(), (*v).to_owned())) + .collect(); + move |k: &str| map.get(k).cloned() + } #[test] fn formats_bare_semver_as_v_prefixed_tag() { @@ -4111,6 +4161,47 @@ mod self_update_tests { fn explicit_tag_never_skips() { assert!(!should_skip_update("0.23.0", "0.23.0", false, true)); } + + #[test] + fn github_token_wins_over_gh_token() { + let get = getter(&[("GITHUB_TOKEN", "primary"), ("GH_TOKEN", "fallback")]); + assert_eq!(resolve_github_token(get), Some("primary".to_owned())); + } + + #[test] + fn gh_token_used_when_github_token_absent() { + let get = getter(&[("GH_TOKEN", "fallback")]); + assert_eq!(resolve_github_token(get), Some("fallback".to_owned())); + } + + #[test] + fn empty_or_whitespace_token_treated_as_absent() { + let get = getter(&[("GITHUB_TOKEN", " "), ("GH_TOKEN", "real")]); + assert_eq!(resolve_github_token(get), Some("real".to_owned())); + + let empty = getter(&[("GITHUB_TOKEN", ""), ("GH_TOKEN", "")]); + assert_eq!(resolve_github_token(empty), None); + } + + #[test] + fn no_token_set_resolves_to_none() { + let get = getter(&[]); + assert_eq!(resolve_github_token(get), None); + } + + #[test] + fn token_value_is_trimmed() { + let get = getter(&[("GITHUB_TOKEN", " padded\t")]); + assert_eq!(resolve_github_token(get), Some("padded".to_owned())); + } + + #[test] + fn hint_contains_authenticate_command() { + assert!( + self_update_rate_limit_hint() + .contains("GITHUB_TOKEN=$(gh auth token) codegraph self-update") + ); + } } #[cfg(test)]