forked from kemitix/git-next
refactor: move git::read_file implementation to git crate
This commit is contained in:
parent
d0638fdbc4
commit
f259179274
12 changed files with 130 additions and 123 deletions
|
@ -42,6 +42,7 @@ base64 = "0.22"
|
||||||
# git
|
# git
|
||||||
# gix = "0.62"
|
# gix = "0.62"
|
||||||
gix = { version = "0.63", features = [
|
gix = { version = "0.63", features = [
|
||||||
|
"dirwalk",
|
||||||
"blocking-http-transport-reqwest-rust-tls",
|
"blocking-http-transport-reqwest-rust-tls",
|
||||||
] }
|
] }
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
|
|
|
@ -1,87 +0,0 @@
|
||||||
use git_next_config as config;
|
|
||||||
use git_next_git as git;
|
|
||||||
|
|
||||||
use kxio::network::{self, Network};
|
|
||||||
use tracing::{error, warn};
|
|
||||||
|
|
||||||
pub async fn contents_get(
|
|
||||||
repo_details: &git::RepoDetails,
|
|
||||||
net: &Network,
|
|
||||||
branch: &config::BranchName,
|
|
||||||
file_path: &str,
|
|
||||||
) -> Result<String, git::file::Error> {
|
|
||||||
let hostname = &repo_details.forge.hostname();
|
|
||||||
let repo_path = &repo_details.repo_path;
|
|
||||||
let api_token = &repo_details.forge.token();
|
|
||||||
use secrecy::ExposeSecret;
|
|
||||||
let token = api_token.expose_secret();
|
|
||||||
let url = network::NetUrl::new(format!(
|
|
||||||
"https://{hostname}/api/v1/repos/{repo_path}/contents/{file_path}?ref={branch}&token={token}"
|
|
||||||
));
|
|
||||||
|
|
||||||
// info!("Loading config");
|
|
||||||
let request = network::NetRequest::new(
|
|
||||||
network::RequestMethod::Get,
|
|
||||||
url,
|
|
||||||
network::NetRequestHeaders::new(),
|
|
||||||
network::RequestBody::None,
|
|
||||||
network::ResponseType::Json,
|
|
||||||
None,
|
|
||||||
network::NetRequestLogging::None,
|
|
||||||
);
|
|
||||||
let result = net.get::<ForgeContentsResponse>(request).await;
|
|
||||||
let response = result.map_err(|e| {
|
|
||||||
warn!(?e, "");
|
|
||||||
git::file::Error::NotFound(file_path.to_string())
|
|
||||||
})?;
|
|
||||||
let status = response.status_code();
|
|
||||||
let contents = match response.response_body() {
|
|
||||||
Some(body) => {
|
|
||||||
// we need to decode (see encoding field) the value of 'content' from the response
|
|
||||||
match body.content_type {
|
|
||||||
ForgeContentsType::File => decode_config(body),
|
|
||||||
_ => Err(git::file::Error::NotFile(file_path.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
error!(%status, "Failed to fetch repo config file");
|
|
||||||
Err(git::file::Error::Unknown(status.to_string()))
|
|
||||||
}
|
|
||||||
}?;
|
|
||||||
Ok(contents)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decode_config(body: ForgeContentsResponse) -> Result<String, git::file::Error> {
|
|
||||||
use base64::Engine;
|
|
||||||
match body.encoding.as_str() {
|
|
||||||
"base64" => {
|
|
||||||
let decoded = base64::engine::general_purpose::STANDARD
|
|
||||||
.decode(body.content)
|
|
||||||
.map_err(|_| git::file::Error::DecodeFromBase64)?;
|
|
||||||
let decoded =
|
|
||||||
String::from_utf8(decoded).map_err(|_| git::file::Error::DecodeFromUtf8)?;
|
|
||||||
|
|
||||||
Ok(decoded)
|
|
||||||
}
|
|
||||||
encoding => Err(git::file::Error::UnknownEncoding(encoding.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, serde::Deserialize)]
|
|
||||||
struct ForgeContentsResponse {
|
|
||||||
#[serde(rename = "type")]
|
|
||||||
pub content_type: ForgeContentsType,
|
|
||||||
pub content: String,
|
|
||||||
pub encoding: String,
|
|
||||||
}
|
|
||||||
#[derive(Clone, Debug, serde::Deserialize)]
|
|
||||||
enum ForgeContentsType {
|
|
||||||
#[serde(rename = "file")]
|
|
||||||
File,
|
|
||||||
#[serde(rename = "dir")]
|
|
||||||
Dir,
|
|
||||||
#[serde(rename = "symlink")]
|
|
||||||
Symlink,
|
|
||||||
#[serde(rename = "submodule")]
|
|
||||||
Submodule,
|
|
||||||
}
|
|
|
@ -1,5 +1,4 @@
|
||||||
pub mod branch;
|
pub mod branch;
|
||||||
mod file;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
@ -30,14 +29,6 @@ impl git::ForgeLike for ForgeJo {
|
||||||
branch::get_all(&self.repo_details, &self.net).await
|
branch::get_all(&self.repo_details, &self.net).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn file_contents_get(
|
|
||||||
&self,
|
|
||||||
branch: &config::BranchName,
|
|
||||||
file_path: &str,
|
|
||||||
) -> Result<String, git::file::Error> {
|
|
||||||
file::contents_get(&self.repo_details, &self.net, branch, file_path).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status {
|
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status {
|
||||||
let repo_details = &self.repo_details;
|
let repo_details = &self.repo_details;
|
||||||
let hostname = &repo_details.forge.hostname();
|
let hostname = &repo_details.forge.hostname();
|
||||||
|
|
|
@ -22,14 +22,6 @@ impl git::ForgeLike for MockForgeEnv {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn file_contents_get(
|
|
||||||
&self,
|
|
||||||
_branch: &config::BranchName,
|
|
||||||
_file_path: &str,
|
|
||||||
) -> Result<String, git::file::Error> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status {
|
async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,74 @@
|
||||||
|
//
|
||||||
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
#[derive(Debug, derive_more::Display)]
|
#[derive(Debug, derive_more::Display)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
Lock,
|
||||||
|
|
||||||
#[display("File not found: {}", 0)]
|
#[display("File not found: {}", 0)]
|
||||||
NotFound(String),
|
NotFound(String),
|
||||||
|
|
||||||
#[display("Unable to parse file contents")]
|
#[display("Unable to parse file contents")]
|
||||||
ParseContent,
|
ParseContent,
|
||||||
|
|
||||||
#[display("Unable to decode from base64")]
|
#[display("Unable to decode from base64")]
|
||||||
DecodeFromBase64,
|
DecodeFromBase64,
|
||||||
#[display("Unable to decoce from UTF-8")]
|
|
||||||
|
#[display("Unable to decode from UTF-8")]
|
||||||
DecodeFromUtf8,
|
DecodeFromUtf8,
|
||||||
|
|
||||||
#[display("Unknown file encoding: {}", 0)]
|
#[display("Unknown file encoding: {}", 0)]
|
||||||
UnknownEncoding(String),
|
UnknownEncoding(String),
|
||||||
|
|
||||||
#[display("Not a file: {}", 0)]
|
#[display("Not a file: {}", 0)]
|
||||||
NotFile(String),
|
NotFile(String),
|
||||||
|
|
||||||
#[display("Unknown error (status: {})", 0)]
|
#[display("Unknown error (status: {})", 0)]
|
||||||
Unknown(String),
|
Unknown(String),
|
||||||
|
|
||||||
|
CommitLog(crate::commit::log::Error),
|
||||||
|
|
||||||
|
CommitNotFound,
|
||||||
|
|
||||||
|
NoTreeInCommit(String),
|
||||||
|
|
||||||
|
NoGitNextToml,
|
||||||
|
|
||||||
|
FindReference(String),
|
||||||
|
|
||||||
|
FindObject(String),
|
||||||
|
|
||||||
|
NonUtf8Blob(String),
|
||||||
|
|
||||||
|
TryId,
|
||||||
}
|
}
|
||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {}
|
||||||
|
|
||||||
|
impl From<crate::commit::log::Error> for Error {
|
||||||
|
fn from(value: crate::commit::log::Error) -> Self {
|
||||||
|
Self::CommitLog(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<gix::reference::find::existing::Error> for Error {
|
||||||
|
fn from(value: gix::reference::find::existing::Error) -> Self {
|
||||||
|
Self::FindReference(value.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<gix::object::commit::Error> for Error {
|
||||||
|
fn from(value: gix::object::commit::Error) -> Self {
|
||||||
|
Self::NoTreeInCommit(value.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<gix::object::find::existing::Error> for Error {
|
||||||
|
fn from(value: gix::object::find::existing::Error) -> Self {
|
||||||
|
Self::FindObject(value.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::string::FromUtf8Error> for Error {
|
||||||
|
fn from(value: std::string::FromUtf8Error) -> Self {
|
||||||
|
Self::NonUtf8Blob(value.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,13 +8,6 @@ pub trait ForgeLike {
|
||||||
/// Returns a list of all branches in the repo.
|
/// Returns a list of all branches in the repo.
|
||||||
async fn branches_get_all(&self) -> Result<Vec<config::BranchName>, git::branch::Error>;
|
async fn branches_get_all(&self) -> Result<Vec<config::BranchName>, git::branch::Error>;
|
||||||
|
|
||||||
/// Returns the contents of the file.
|
|
||||||
async fn file_contents_get(
|
|
||||||
&self,
|
|
||||||
branch: &config::BranchName,
|
|
||||||
file_path: &str,
|
|
||||||
) -> Result<String, git::file::Error>;
|
|
||||||
|
|
||||||
/// Checks the results of any (e.g. CI) status checks for the commit.
|
/// Checks the results of any (e.g. CI) status checks for the commit.
|
||||||
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status;
|
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,4 +22,4 @@ pub enum Error {
|
||||||
}
|
}
|
||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {}
|
||||||
|
|
||||||
pub type Result = core::result::Result<(), Error>;
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
|
@ -143,6 +143,17 @@ impl OpenRepositoryLike for MockOpenRepository {
|
||||||
.map(|inner| inner.commit_log(branch_name, find_commits))
|
.map(|inner| inner.commit_log(branch_name, find_commits))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_file(
|
||||||
|
&self,
|
||||||
|
branch_name: &git_next_config::BranchName,
|
||||||
|
file_name: &str,
|
||||||
|
) -> git::file::Result<String> {
|
||||||
|
self.inner
|
||||||
|
.lock()
|
||||||
|
.map(|inner| inner.read_file(branch_name, file_name))
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl OpenRepositoryLike for InnerMockOpenRepository {
|
impl OpenRepositoryLike for InnerMockOpenRepository {
|
||||||
fn find_default_remote(&self, direction: Direction) -> Option<GitRemote> {
|
fn find_default_remote(&self, direction: Direction) -> Option<GitRemote> {
|
||||||
|
@ -173,4 +184,12 @@ impl OpenRepositoryLike for InnerMockOpenRepository {
|
||||||
) -> core::result::Result<Vec<crate::Commit>, crate::commit::log::Error> {
|
) -> core::result::Result<Vec<crate::Commit>, crate::commit::log::Error> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_file(
|
||||||
|
&self,
|
||||||
|
_branch_name: &git_next_config::BranchName,
|
||||||
|
_file_name: &str,
|
||||||
|
) -> git::file::Result<String> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,14 +36,23 @@ pub trait OpenRepositoryLike {
|
||||||
branch_name: config::BranchName,
|
branch_name: config::BranchName,
|
||||||
to_commit: git::GitRef,
|
to_commit: git::GitRef,
|
||||||
force: git::push::Force,
|
force: git::push::Force,
|
||||||
) -> Result<(), git::push::Error>;
|
) -> git::push::Result<()>;
|
||||||
|
|
||||||
/// List of commits in a branch, optionally up-to any specified commit.
|
/// List of commits in a branch, optionally up-to any specified commit.
|
||||||
fn commit_log(
|
fn commit_log(
|
||||||
&self,
|
&self,
|
||||||
branch_name: &config::BranchName,
|
branch_name: &config::BranchName,
|
||||||
find_commits: &[git::Commit],
|
find_commits: &[git::Commit],
|
||||||
) -> Result<Vec<git::Commit>, git::commit::log::Error>;
|
) -> git::commit::log::Result<Vec<git::Commit>>;
|
||||||
|
|
||||||
|
/// Read the contents of a file as a string.
|
||||||
|
///
|
||||||
|
/// Only handles files in the root of the repo.
|
||||||
|
fn read_file(
|
||||||
|
&self,
|
||||||
|
branch_name: &config::BranchName,
|
||||||
|
file_name: &str,
|
||||||
|
) -> git::file::Result<String>;
|
||||||
}
|
}
|
||||||
impl std::ops::Deref for OpenRepository {
|
impl std::ops::Deref for OpenRepository {
|
||||||
type Target = dyn OpenRepositoryLike;
|
type Target = dyn OpenRepositoryLike;
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
use crate as git;
|
use crate as git;
|
||||||
use git_next_config as config;
|
use git_next_config as config;
|
||||||
use gix::bstr::BStr;
|
|
||||||
|
|
||||||
|
use gix::bstr::BStr;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
@ -152,6 +151,32 @@ impl super::OpenRepositoryLike for RealOpenRepository {
|
||||||
Ok(commits)
|
Ok(commits)
|
||||||
})?
|
})?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip_all, fields(%branch_name, %file_name))]
|
||||||
|
fn read_file(
|
||||||
|
&self,
|
||||||
|
branch_name: &config::BranchName,
|
||||||
|
file_name: &str,
|
||||||
|
) -> git::file::Result<String> {
|
||||||
|
self.0
|
||||||
|
.lock()
|
||||||
|
.map_err(|_| git::file::Error::Lock)
|
||||||
|
.and_then(|repo| {
|
||||||
|
let fref = repo.find_reference(format!("origin/{}", branch_name).as_str())?;
|
||||||
|
let id = fref.try_id().ok_or(git::file::Error::TryId)?;
|
||||||
|
let oid = id.detach();
|
||||||
|
let obj = repo.find_object(oid)?;
|
||||||
|
let commit = obj.into_commit();
|
||||||
|
let tree = commit.tree()?;
|
||||||
|
let ent = tree
|
||||||
|
.find_entry(".git-next.toml")
|
||||||
|
.ok_or(git::file::Error::NoGitNextToml)?;
|
||||||
|
let fobj = ent.object()?;
|
||||||
|
let blob = fobj.into_blob().take_data();
|
||||||
|
let content = String::from_utf8(blob)?;
|
||||||
|
Ok(content)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&gix::Url> for git::GitRemote {
|
impl From<&gix::Url> for git::GitRemote {
|
||||||
|
|
|
@ -122,7 +122,11 @@ impl Handler<LoadConfigFromRepo> for RepoActor {
|
||||||
let details = self.repo_details.clone();
|
let details = self.repo_details.clone();
|
||||||
let addr = ctx.address();
|
let addr = ctx.address();
|
||||||
let forge = self.forge.clone();
|
let forge = self.forge.clone();
|
||||||
repo_actor::load::load_file(details, addr, forge)
|
let Some(open_repository) = self.open_repository.clone() else {
|
||||||
|
warn!("missing open repository - can't load configuration");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
repo_actor::load::load_file(details, addr, forge, open_repository)
|
||||||
.in_current_span()
|
.in_current_span()
|
||||||
.into_actor(self)
|
.into_actor(self)
|
||||||
.wait(ctx);
|
.wait(ctx);
|
||||||
|
|
|
@ -11,9 +11,14 @@ use super::{LoadedConfig, RepoActor};
|
||||||
|
|
||||||
/// Loads the [RepoConfig] from the `.git-next.toml` file in the repository
|
/// Loads the [RepoConfig] from the `.git-next.toml` file in the repository
|
||||||
#[tracing::instrument(skip_all, fields(branch = %repo_details.branch))]
|
#[tracing::instrument(skip_all, fields(branch = %repo_details.branch))]
|
||||||
pub async fn load_file(repo_details: git::RepoDetails, addr: Addr<RepoActor>, forge: forge::Forge) {
|
pub async fn load_file(
|
||||||
|
repo_details: git::RepoDetails,
|
||||||
|
addr: Addr<RepoActor>,
|
||||||
|
forge: forge::Forge,
|
||||||
|
open_repository: git::OpenRepository,
|
||||||
|
) {
|
||||||
info!("Loading .git-next.toml from repo");
|
info!("Loading .git-next.toml from repo");
|
||||||
let repo_config = match load(&repo_details, &forge).await {
|
let repo_config = match load(&repo_details, &forge, open_repository).await {
|
||||||
Ok(repo_config) => repo_config,
|
Ok(repo_config) => repo_config,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!(?err, "Failed to load config");
|
error!(?err, "Failed to load config");
|
||||||
|
@ -27,10 +32,9 @@ pub async fn load_file(repo_details: git::RepoDetails, addr: Addr<RepoActor>, fo
|
||||||
async fn load(
|
async fn load(
|
||||||
details: &git::RepoDetails,
|
details: &git::RepoDetails,
|
||||||
forge: &forge::Forge,
|
forge: &forge::Forge,
|
||||||
|
open_repository: git::OpenRepository,
|
||||||
) -> Result<config::RepoConfig, Error> {
|
) -> Result<config::RepoConfig, Error> {
|
||||||
let contents = forge
|
let contents = open_repository.read_file(&details.branch, ".git-next.toml")?;
|
||||||
.file_contents_get(&details.branch, ".git-next.toml")
|
|
||||||
.await?;
|
|
||||||
let config = config::RepoConfig::load(&contents)?;
|
let config = config::RepoConfig::load(&contents)?;
|
||||||
let config = validate(config, forge).await?;
|
let config = validate(config, forge).await?;
|
||||||
Ok(config)
|
Ok(config)
|
||||||
|
|
Loading…
Reference in a new issue