Resolve a GitHub token for authenticated public API access. Order: `GH_TOKEN`/`GITHUB_TOKEN` env vars, then a `gh auth token` subprocess lookup cached once per process (including a cached miss, so the subprocess is spawned at most once). Returns `None` when no token is available; callers should proceed unauthenticated in that case. Only send this token to `api.github.com` public read endpoints.
()
| 19 | /// |
| 20 | /// Only send this token to `api.github.com` public read endpoints. |
| 21 | pub fn github_public_api_token() -> Option<String> { |
| 22 | let env_token = ["GH_TOKEN", "GITHUB_TOKEN"].iter().find_map(|name| { |
| 23 | std::env::var(name) |
| 24 | .ok() |
| 25 | .map(|token| token.trim().to_string()) |
| 26 | .filter(|token| !token.is_empty()) |
| 27 | }); |
| 28 | if env_token.is_some() { |
| 29 | return env_token; |
| 30 | } |
| 31 | |
| 32 | static GH_CLI_TOKEN: OnceLock<Option<String>> = OnceLock::new(); |
| 33 | GH_CLI_TOKEN.get_or_init(gh_cli_token).clone() |
| 34 | } |
| 35 | |
| 36 | fn gh_cli_token() -> Option<String> { |
| 37 | if !crate::auth::command_exists("gh") { |
no test coverage detected