Skip to content

Commit

Permalink
fix: review comment codeblock fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyko committed Aug 18, 2023
1 parent 88daa57 commit c4d0c16
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions chuckle-github/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ anyhow = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

[dev-dependencies]
tokio = { workspace = true }
28 changes: 22 additions & 6 deletions chuckle-github/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,38 @@ pub async fn fetch_raw_file(
token: String,
owner: String,
name: String,
r#ref: String,
commit: String,
path: String,
) -> anyhow::Result<String> {
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?;

let body = resp.text().await?;

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);
}
}

0 comments on commit c4d0c16

Please sign in to comment.