Fetch the in-repo config file from the remote repo.

This commit is contained in:
Tom Alexander
2024-09-28 16:51:27 -04:00
parent c32a8650f5
commit 66228f83f2
6 changed files with 97 additions and 16 deletions

View File

@@ -1,4 +1,6 @@
use base64::{engine::general_purpose, Engine as _};
use serde::Deserialize;
use tracing::debug;
use self::error::GiteaClientError;
pub(crate) mod error;
@@ -30,7 +32,7 @@ impl GiteaClient {
let mut num_responses: u64 = 0;
loop {
let url = format!(
"{api_root}/v1/repos/{owner}/{repo}/git/trees/{commit}?recursive=true&per_page=10{page}",
"{api_root}/v1/repos/{owner}/{repo}/git/trees/{commit}?recursive=true&per_page=100{page}",
api_root = self.api_root,
owner = owner.as_ref(),
repo = repo.as_ref(),
@@ -70,6 +72,27 @@ impl GiteaClient {
}
Ok(Tree::new(files))
}
pub(crate) async fn read_file(
&self,
file_reference: &TreeFileReference,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let response = self
.http_client
.get(&file_reference.url)
.header("Authorization", format!("token {}", self.token))
.send()
.await?;
let response = response.error_for_status()?;
let body = response.text().await?;
debug!("read_file response: {}", body);
let parsed_body: ResponseReadFile = serde_json::from_str(body.as_str())?;
assert!(
parsed_body.encoding == "base64",
"We currently only support base64 file encoding from gitea."
);
Ok(general_purpose::STANDARD.decode(parsed_body.content)?)
}
}
/// A single API response for GetTree containing only one page.
@@ -101,13 +124,13 @@ pub(crate) struct Tree {
#[derive(Debug)]
pub(crate) struct TreeFileReference {
path: String,
url: String,
pub(crate) path: String,
pub(crate) url: String,
}
impl Tree {
pub(crate) fn new(files: Vec<TreeFileReference>) -> Tree {
Tree { files: Vec::new() }
Tree { files }
}
}
@@ -119,3 +142,12 @@ impl TreeFileReference {
}
}
}
#[derive(Debug, Deserialize)]
struct ResponseReadFile {
content: String,
encoding: String,
url: String,
sha: String,
size: u64,
}