Skip to content

Commit

Permalink
codegen: Avoid using GITHUB_TOKEN with crates.io (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb authored May 19, 2024
1 parent d722dd2 commit 68c5e86
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions tools/codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() -> Result<()> {
// is greater than 100, multiple fetches are needed.
for page in 1.. {
let per_page = 100;
let mut r: github::Releases = download(&format!(
let mut r: github::Releases = download_github(&format!(
"https://api.github.com/repos/{repo}/releases?per_page={per_page}&page={page}"
))?
.into_json()?;
Expand Down Expand Up @@ -232,7 +232,7 @@ fn main() -> Result<()> {
eprintln!("already downloaded");
fs::File::open(download_cache)?.read_to_end(&mut buf)?;
} else {
download(&url)?.into_reader().read_to_end(&mut buf)?;
download_github(&url)?.into_reader().read_to_end(&mut buf)?;
eprintln!("download complete");
fs::write(download_cache, &buf)?;
}
Expand All @@ -254,7 +254,7 @@ fn main() -> Result<()> {
eprintln!("already downloaded");
minisign_verify::Signature::from_file(sig_download_cache)?
} else {
let buf = download(&url)?.into_string()?;
let buf = download_github(&url)?.into_string()?;
eprintln!("download complete");
fs::write(sig_download_cache, &buf)?;
minisign_verify::Signature::decode(&buf)?
Expand Down Expand Up @@ -532,7 +532,9 @@ fn replace_vars(
Ok(s)
}

fn download(url: &str) -> Result<ureq::Response> {
/// Download using GITHUB_TOKEN.
#[allow(clippy::missing_panics_doc)]
fn download_github(url: &str) -> Result<ureq::Response> {
let mut token = env::var("GITHUB_TOKEN").ok().filter(|v| !v.is_empty());
let mut retry = 0;
let max_retry = 6;
Expand All @@ -559,6 +561,28 @@ fn download(url: &str) -> Result<ureq::Response> {
Err(last_error.unwrap().into())
}

/// Download without using GITHUB_TOKEN.
#[allow(clippy::missing_panics_doc)]
pub fn download(url: &str) -> Result<ureq::Response> {
let mut retry = 0;
let max_retry = 6;
let mut last_error;
loop {
let req = ureq::get(url);
match req.call() {
Ok(res) => return Ok(res),
Err(e) => last_error = Some(e),
}
retry += 1;
if retry > max_retry {
break;
}
eprintln!("download of {url} failed; retrying after {}s ({retry}/{max_retry})", retry * 2);
std::thread::sleep(Duration::from_secs(retry * 2));
}
Err(last_error.unwrap().into())
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct Version {
major: Option<u64>,
Expand Down

0 comments on commit 68c5e86

Please sign in to comment.