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
This commit is contained in:
+23
-9
@@ -139,17 +139,31 @@ pub struct GhRelease {
|
|||||||
pub assets: Vec<GhAsset>,
|
pub assets: Vec<GhAsset>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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<String> {
|
||||||
|
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<GhRelease> {
|
pub fn latest_release(repo: &str) -> Result<GhRelease> {
|
||||||
let url = format!("https://api.github.com/repos/{repo}/releases/latest");
|
let url = format!("https://api.github.com/repos/{repo}/releases/latest");
|
||||||
let resp = ureq::get(&url)
|
let mut req = ureq::get(&url).set("User-Agent", USER_AGENT);
|
||||||
.set("User-Agent", USER_AGENT)
|
if let Some(auth) = github_auth_header() {
|
||||||
.call()
|
req = req.set("Authorization", &auth);
|
||||||
.map_err(|e| match e {
|
}
|
||||||
ureq::Error::Status(404, _) => {
|
let resp = req.call().map_err(|e| match e {
|
||||||
anyhow!("no GitHub releases found for repository '{repo}'")
|
ureq::Error::Status(404, _) => {
|
||||||
}
|
anyhow!("no GitHub releases found for repository '{repo}'")
|
||||||
other => anyhow!("GitHub API request failed for '{repo}': {other}"),
|
}
|
||||||
})?;
|
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
|
let text = resp
|
||||||
.into_string()
|
.into_string()
|
||||||
.context("reading GitHub API response")?;
|
.context("reading GitHub API response")?;
|
||||||
|
|||||||
Reference in New Issue
Block a user