Skip to content

Commit

Permalink
Append header if GITHUB_TOKEN environment variable is present (#128)
Browse files Browse the repository at this point in the history
* feat: ✨ Append header if GITHUB_TOKEN environment variable is present

* build: ⬆️ Add blocking feature of reqwest

* feat: ✨ If a semver version is provided, get the latest subpatch and use that version

* docs: 📝 Update invalid_version error

* perf: ⚡️ Remove unnecesary into_diagnostics calls

* feat: ✨ Even if the regex matches, check that the release exists

* feat: ✨ Update auth

* chore: 🔊 Add log

* chore: ⚡️ Add basic auth

* Add gh api tests

* fix: 🐛 fix typo

* feat: 🎨 Remove permissions

* test: ✅ Add rust test

* test: ✅ Add rust test

* ci: ⚡️ Add checkout action

* ci: 🧪 Test with permissions

* ci: 🧪 Update headers

* ci: 🎨 Avoid triggering other CI

* ci: ✅ Check if GITHUB TOKEN is present

* ci: ✅ Update get call

* ci: 🧪 Update headers

* style: 🎨 Simplify code

* ci: 🧪 Update headers

* ci: 🧪 Add curl test

* feat: ✨ Add retries

* feat: ✨ Add retries

* feat: ✨ Improve retries

* chore: 🔊 Add logs to tests

* feat: ✨ Remove retries

* ci: 🧪 Test api limit

* chore: 🧪 Test with retries

* feat: ✨ Create a github_query fn

* ci: 🧪 Update CI rust test

* feat: 🔊 Update logging

* feat: ✨ Add maximum retries

* chore: 🔥 Remove gh_test.yaml

* style: 🎨 Remove duplicated log

* ci: 👷 Add env to CI

* chore: 🔊 Add debug log

* ci: 🧪 Test without env

* revert: ⏪️ Revert env removal

* ci: 👷 Update environment in CI

* chore: 🔊 Add logging

* chore: 🔥 Remove download test

* feat: ✨ Move github_query fn to mod.rs
  • Loading branch information
SergioGasquez authored Jan 11, 2023
1 parent e8c4ef1 commit 11becfa
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 88 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:

env:
CARGO_TERM_COLOR: always
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
publish-release:
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ on:
- main
paths-ignore:
- "**/README.md"
- "**/cd.yml"
- "**/audit.yaml"
- "**/cd.yaml"
pull_request:
paths-ignore:
- "**/README.md"
- "**/cd.yml"
- "**/audit.yaml"
- "**/cd.yaml"

env:
CARGO_TERM_COLOR: always
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
continuous-integration:
Expand Down
49 changes: 49 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ anyhow = "1.0.68"
clap = { version = "4.0.32", features = ["derive"] }
flate2 = "1.0.25"
guess_host_triple = "0.1.3"
reqwest = "0.11.12"
reqwest = { version = "0.11.12", features = ["blocking"] }
tar = "0.4.37"
zip = "0.6.3"
xz2 = "0.1.6"
Expand All @@ -38,6 +38,8 @@ thiserror = "1.0.38"
update-informer = "0.6.0"
tokio = { version = "1.24.1", features = ["full"] }
async-trait = "0.1.61"
retry = "2.0.0"


[target.'cfg(target_os = "linux")'.dependencies]
openssl = { version = "0.10", features = ["vendored"] }
Expand Down
5 changes: 4 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub enum Error {
#[error("{} Unsuported file extension: '{0}'", emoji::ERROR)]
UnsuportedFileExtension(String),
// Toolchain - Rust
#[diagnostic(code(espup::toolchain::rust::failed_to_query_github))]
#[error("{} Failed To Query GitHub API.", emoji::ERROR)]
FailedGithubQuery,
#[diagnostic(code(espup::toolchain::rust::failed_to_get_latest_version))]
#[error("{} Failed To serialize Json from string.", emoji::ERROR)]
FailedToSerializeJson,
Expand All @@ -46,7 +49,7 @@ pub enum Error {
XtensaToolchainAlreadyInstalled(String),
#[diagnostic(code(espup::toolchain::rust::invalid_version))]
#[error(
"{} Invalid toolchain version '{0}', must be in the form of '<major>.<minor>.<patch>.<subpatch>'",
"{} Invalid toolchain version '{0}'. Verify that the format is correct: '<major>.<minor>.<patch>.<subpatch>' or '<major>.<minor>.<patch>', and that the release exists in https://github.com/esp-rs/rust-build/releases",
emoji::ERROR
)]
InvalidXtensaToolchanVersion(String),
Expand Down
92 changes: 41 additions & 51 deletions src/toolchain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use crate::{emoji, error::Error};
use async_trait::async_trait;
use flate2::bufread::GzDecoder;
use log::info;
use log::{debug, info, warn};
use miette::Result;
use reqwest::blocking::Client;
use reqwest::header;
use retry::{delay::Fixed, retry};
use std::{
env,
fs::{create_dir_all, File},
io::Write,
path::Path,
Expand Down Expand Up @@ -95,55 +99,41 @@ pub async fn download_file(
Ok(format!("{}/{}", output_directory, file_name))
}

#[cfg(test)]
mod tests {
use crate::toolchain::download_file;
use std::{fs::File, io::Write};

#[tokio::test]
async fn test_download_file() {
// Returns the correct file path when the file already exists
let temp_dir = tempfile::TempDir::new().unwrap();
let file_name = "test.txt";
let output_directory = temp_dir.path().to_str().unwrap();
let file_path = format!("{}/{}", output_directory, file_name);
let mut file = File::create(file_path.clone()).unwrap();
file.write_all(b"test content").unwrap();

let url = "https://example.com/test.txt";
let result = download_file(url.to_string(), file_name, output_directory, false).await;
assert!(result.is_ok());
let path = result.unwrap();
assert_eq!(path, file_path);

// Creates the output directory if it does not exist
let temp_dir = tempfile::TempDir::new().unwrap();
let output_directory = temp_dir.path().join("test");
let file_name = "test.txt";

let url = "https://example.com/test.txt";
let result = download_file(
url.to_string(),
file_name,
output_directory.to_str().unwrap(),
false,
)
.await;
assert!(result.is_ok());
let path = result.unwrap();
#[cfg(windows)]
let path = path.replace('/', "\\");
assert_eq!(path, output_directory.join(file_name).to_str().unwrap());
assert!(output_directory.exists());

// Downloads a ZIP file and uncompresses it
let temp_dir = tempfile::TempDir::new().unwrap();
let output_directory = temp_dir.path().to_str().unwrap();
let file_name = "espup.zip";
let url = "https://github.com/esp-rs/espup/releases/latest/download/espup-x86_64-unknown-linux-gnu.zip";
let result = download_file(url.to_string(), file_name, output_directory, true).await;
assert!(result.is_ok());
let extracted_file = temp_dir.path().join("espup");
assert!(extracted_file.exists());
/// Queries the GitHub API and returns the JSON response.
pub fn github_query(url: &str) -> Result<serde_json::Value, Error> {
info!("{} Querying GitHub API: '{}'", emoji::INFO, url);
let mut headers = header::HeaderMap::new();
headers.insert(header::USER_AGENT, "espup".parse().unwrap());
headers.insert(
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.", emoji::DEBUG);
headers.insert(
"Authorization",
format!("Bearer {}", token.to_string_lossy())
.parse()
.unwrap(),
);
}
let client = Client::new();
let json = 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", emoji::WARN);
return Err(Error::FailedGithubQuery);
}
let json: serde_json::Value =
serde_json::from_str(&res).map_err(|_| Error::FailedToSerializeJson)?;
Ok(json)
},
)
.unwrap();
Ok(json)
}
Loading

0 comments on commit 11becfa

Please sign in to comment.