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/ghtoken #394

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ pub enum Error {
CreateDirectory(String),

#[diagnostic(code(espup::toolchain::rust::query_github))]
#[error("Failed to query GitHub API")]
GithubQuery,
#[error("Failed to query GitHub API: Rate Limiting")]
GithubRateLimit,

#[diagnostic(code(espup::toolchain::rust::query_github))]
#[error("Failed to query GitHub API: Invalid Github token")]
GithubTokenInvalid,

#[diagnostic(code(espup::toolchain::rust::install_riscv_target))]
#[error("Failed to Install RISC-V targets for '{0}' toolchain")]
Expand Down
25 changes: 20 additions & 5 deletions src/toolchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,15 @@ pub async fn install(args: InstallOpts, install_mode: InstallMode) -> Result<()>
toolchain_version.clone()
}
} else {
XtensaRust::get_latest_version().await?
// if there was an error getting the newest version from github, use a hardcoded fallback
XtensaRust::get_latest_version().await.unwrap_or_else(|e| {
warn!(
"Error getting newest toolchain version {}. Using fallback version '{}'",
e,
rust::TOOLCHAIN_FALLBACK
);
String::from(rust::TOOLCHAIN_FALLBACK)
})
};
let toolchain_dir = get_rustup_home().join("toolchains").join(args.name);
let llvm: Llvm = Llvm::new(
Expand Down Expand Up @@ -269,6 +277,7 @@ pub fn github_query(url: &str) -> Result<serde_json::Value, Error> {
header::ACCEPT,
"application/vnd.github+json".parse().unwrap(),
);

headers.insert("X-GitHub-Api-Version", "2022-11-28".parse().unwrap());
if let Some(token) = env::var_os("GITHUB_TOKEN") {
debug!("Auth header added");
Expand All @@ -280,23 +289,29 @@ pub fn github_query(url: &str) -> Result<serde_json::Value, Error> {
);
}
let client = Client::new();
let json = retry(
let json: Result<serde_json::Value, Error> = retry(
Fixed::from_millis(100).take(5),
|| -> Result<serde_json::Value, Error> {
let res = client.get(url).headers(headers.clone()).send()?.text()?;
if res.contains(
"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting",
) {
warn!("GitHub rate limit exceeded");
return Err(Error::GithubQuery);
return Err(Error::GithubRateLimit);
}

if res.contains("Bad credentials") {
warn!("Github token credentials invalid");
return Err(Error::GithubTokenInvalid);
}

let json: serde_json::Value =
serde_json::from_str(&res).map_err(|_| Error::SerializeJson)?;
Ok(json)
},
)
.unwrap();
Ok(json)
.map_err(|err| err.error);
json
}

/// Checks if the directory exists and deletes it if it does.
Expand Down
4 changes: 4 additions & 0 deletions src/toolchain/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ use tokio::fs::{remove_dir_all, remove_file};
/// Xtensa Rust Toolchain repository
const DEFAULT_XTENSA_RUST_REPOSITORY: &str =
"https://github.com/esp-rs/rust-build/releases/download";

/// Xtensa Rust Toolchain fallback version
pub const TOOLCHAIN_FALLBACK: &str = "1.57.0.2";

/// Xtensa Rust Toolchain API URL
const XTENSA_RUST_LATEST_API_URL: &str =
"https://api.github.com/repos/esp-rs/rust-build/releases/latest";
Expand Down