From c4d0c1693d9a5ac1056c3082ac3ce5c93ccf3ef8 Mon Sep 17 00:00:00 2001 From: Carter Himmel Date: Thu, 17 Aug 2023 20:45:41 -0600 Subject: [PATCH] fix: review comment codeblock fetching --- Cargo.lock | 1 + chuckle-github/Cargo.toml | 3 +++ chuckle-github/src/lib.rs | 28 ++++++++++++++++++++++------ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7d325d..387cace 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,6 +295,7 @@ dependencies = [ "reqwest", "serde", "serde_json", + "tokio", ] [[package]] diff --git a/chuckle-github/Cargo.toml b/chuckle-github/Cargo.toml index 4dcde82..11e1e9d 100644 --- a/chuckle-github/Cargo.toml +++ b/chuckle-github/Cargo.toml @@ -8,3 +8,6 @@ anyhow = { workspace = true } reqwest = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } + +[dev-dependencies] +tokio = { workspace = true } diff --git a/chuckle-github/src/lib.rs b/chuckle-github/src/lib.rs index a2bc66c..c396f10 100644 --- a/chuckle-github/src/lib.rs +++ b/chuckle-github/src/lib.rs @@ -5,18 +5,16 @@ pub async fn fetch_raw_file( token: String, owner: String, name: String, - r#ref: String, + commit: String, path: String, ) -> anyhow::Result { - let file_url = format!( - "https://raw.githubusercontent.com/{}/{}/{}/{}", - owner, name, r#ref, path - ); + let file_url = + format!("https://api.github.com/repos/{owner}/{name}/contents/{path}?ref={commit}",); let client = reqwest::Client::new(); let resp = client .get(&file_url) - .header("Authorization", format!("Token {token}")) + .header("Authorization", format!("token {token}")) .send() .await?; @@ -24,3 +22,21 @@ pub async fn fetch_raw_file( Ok(body) } + +#[cfg(test)] +mod test { + #[tokio::test] + async fn test_fetch_raw_file() { + let token = std::env::var("GITHUB_TOKEN").unwrap(); + let owner = std::env::var("GITHUB_OWNER").unwrap_or("trufflehq".into()); + let name = std::env::var("GITHUB_REPO").unwrap_or("chuckle".into()); + let commit = std::env::var("GITHUB_COMMIT").unwrap_or("HEAD".into()); + let path = std::env::var("GITHUB_PATH").unwrap_or("README.md".into()); + + let file = super::fetch_raw_file(token, owner, name, commit, path).await; + assert!(file.is_ok()); + let file = file.unwrap(); + + assert!(file.len() > 0); + } +}