From 498af684c9b371c475cf9c0f89319a88be41d8cf Mon Sep 17 00:00:00 2001 From: Bruno Charest Date: Sun, 16 Aug 2026 21:30:31 -0400 Subject: [PATCH] v0.2.14 : support GITHUB_TOKEN/GH_TOKEN pour l API GitHub (limite 60 -> 5000 requetes/h) et message clair sur le rate limit 403 --- src/download.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/download.rs b/src/download.rs index 9dab046..e13cc9b 100644 --- a/src/download.rs +++ b/src/download.rs @@ -139,17 +139,31 @@ pub struct GhRelease { pub assets: Vec, } +/// Authorization header for api.github.com when GITHUB_TOKEN or GH_TOKEN +/// is set (raises the rate limit from 60 to 5000 requests/hour). +fn github_auth_header() -> Option { + let token = std::env::var("GITHUB_TOKEN") + .or_else(|_| std::env::var("GH_TOKEN")) + .ok() + .filter(|t| !t.trim().is_empty())?; + Some(format!("Bearer {}", token.trim())) +} + pub fn latest_release(repo: &str) -> Result { let url = format!("https://api.github.com/repos/{repo}/releases/latest"); - let resp = ureq::get(&url) - .set("User-Agent", USER_AGENT) - .call() - .map_err(|e| match e { - ureq::Error::Status(404, _) => { - anyhow!("no GitHub releases found for repository '{repo}'") - } - other => anyhow!("GitHub API request failed for '{repo}': {other}"), - })?; + let mut req = ureq::get(&url).set("User-Agent", USER_AGENT); + if let Some(auth) = github_auth_header() { + req = req.set("Authorization", &auth); + } + let resp = req.call().map_err(|e| match e { + ureq::Error::Status(404, _) => { + anyhow!("no GitHub releases found for repository '{repo}'") + } + ureq::Error::Status(403, _) => anyhow!( + "GitHub API request failed for '{repo}': rate limit exceeded — set the GITHUB_TOKEN environment variable to raise the limit, or retry in a few minutes" + ), + other => anyhow!("GitHub API request failed for '{repo}': {other}"), + })?; let text = resp .into_string() .context("reading GitHub API response")?;