Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show gh rate limit reset time #3221

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mise.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ version = "1.10.13"
cargo-binstall-linux-x86_64 = "sha256:53a7157f56e1698d42d3cd50b1d08f02159638e3a73edf362e14215337942e86"
cargo-binstall-macos-aarch64 = "sha256:f67dd4fc8ea01b4c55fe041a0fbd6d81a361947f1dbf2bd226490c641cc167b3"
"cargo-binstall-x86_64-pc-windows-msvc.zip" = "sha256:6c5272909b50bf93758a294c32195e8874494e5fa1ac5c741a103070c8d1d599"
"cargo-binstall-x86_64-unknown-linux-musl.tgz" = "sha256:0e649badc8b9ade5f32be2afe1df9144fc4c1bb548b92eca71f0709baa240633"
"cargo-binstall.exe-windows-x86_64" = "sha256:32a28a1e98ad23e2c7ac360b815001181d7feffd1283a20c6ef92c3c032d6ba6"

[tools."cargo:cargo-edit"]
Expand Down
47 changes: 36 additions & 11 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::time::Duration;
use eyre::{bail, Report, Result};
use once_cell::sync::Lazy;
use reqwest::header::HeaderMap;
use reqwest::{ClientBuilder, IntoUrl, Response};
use reqwest::{ClientBuilder, IntoUrl, RequestBuilder, Response};
use url::Url;

use crate::cli::version;
Expand Down Expand Up @@ -57,13 +57,10 @@ impl Client {
let get = |url: Url| async move {
debug!("GET {}", &url);
let mut req = self.reqwest.get(url.clone());
if url.host_str() == Some("api.github.com") {
if let Some(token) = &*env::GITHUB_TOKEN {
req = req.header("authorization", format!("token {}", token));
}
}
req = with_github_auth(&url, req);
let resp = req.send().await?;
debug!("GET {url} {}", resp.status());
display_github_rate_limit(&resp);
resp.error_for_status_ref()?;
Ok(resp)
};
Expand Down Expand Up @@ -91,13 +88,10 @@ impl Client {
let head = |url: Url| async move {
debug!("HEAD {}", &url);
let mut req = self.reqwest.head(url.clone());
if url.host_str() == Some("api.github.com") {
if let Some(token) = &*env::GITHUB_TOKEN {
req = req.header("authorization", format!("token {}", token));
}
}
req = with_github_auth(&url, req);
let resp = req.send().await?;
debug!("HEAD {url} {}", resp.status());
display_github_rate_limit(&resp);
resp.error_for_status_ref()?;
Ok(resp)
};
Expand Down Expand Up @@ -195,3 +189,34 @@ pub fn error_code(e: &Report) -> Option<u16> {
None
}
}

fn with_github_auth(url: &Url, mut req: RequestBuilder) -> RequestBuilder {
if url.host_str() == Some("api.github.com") {
if let Some(token) = &*env::GITHUB_TOKEN {
req = req.header("authorization", format!("token {}", token));
}
}
req
}

fn display_github_rate_limit(resp: &Response) {
let status = resp.status().as_u16();
if status == 403 || status == 429 {
if !resp
.headers()
.get("x-ratelimit-remaining")
.is_some_and(|r| r == "0")
{
return;
}
if let Some(reset) = resp.headers().get("x-ratelimit-reset") {
let reset = reset.to_str().map(|r| r.to_string()).unwrap_or_default();
if let Some(reset) = chrono::DateTime::from_timestamp(reset.parse().unwrap(), 0) {
warn!(
"GitHub rate limit exceeded. Resets at {}",
reset.naive_local().to_string()
);
}
}
}
}