git-next/crates/repo-actor/src/load.rs
Paul Campbell f259179274
All checks were successful
Rust / build (push) Successful in 2m19s
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/cron/cron-docker-builder Pipeline was successful
ci/woodpecker/cron/push-next Pipeline was successful
ci/woodpecker/cron/tag-created Pipeline was successful
refactor: move git::read_file implementation to git crate
2024-05-26 18:22:15 +01:00

79 lines
2.2 KiB
Rust

//
use actix::prelude::*;
use tracing::{error, info};
use git_next_config as config;
use git_next_forge as forge;
use git_next_git as git;
use super::{LoadedConfig, RepoActor};
/// Loads the [RepoConfig] from the `.git-next.toml` file in the repository
#[tracing::instrument(skip_all, fields(branch = %repo_details.branch))]
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");
let repo_config = match load(&repo_details, &forge, open_repository).await {
Ok(repo_config) => repo_config,
Err(err) => {
error!(?err, "Failed to load config");
return;
}
};
info!("Loaded .git-next.toml from repo");
addr.do_send(LoadedConfig(repo_config));
}
async fn load(
details: &git::RepoDetails,
forge: &forge::Forge,
open_repository: git::OpenRepository,
) -> Result<config::RepoConfig, Error> {
let contents = open_repository.read_file(&details.branch, ".git-next.toml")?;
let config = config::RepoConfig::load(&contents)?;
let config = validate(config, forge).await?;
Ok(config)
}
#[derive(Debug, derive_more::From, derive_more::Display)]
pub enum Error {
File(git::file::Error),
Config(config::server::Error),
Toml(toml::de::Error),
Forge(git::branch::Error),
BranchNotFound(config::BranchName),
}
pub async fn validate(
config: config::RepoConfig,
forge: &forge::Forge,
) -> Result<config::RepoConfig, Error> {
let branches = forge.branches_get_all().await.map_err(|e| {
error!(?e, "Failed to list branches");
Error::Forge(e)
})?;
if !branches
.iter()
.any(|branch| branch == &config.branches().main())
{
return Err(Error::BranchNotFound(config.branches().main()));
}
if !branches
.iter()
.any(|branch| branch == &config.branches().next())
{
return Err(Error::BranchNotFound(config.branches().next()));
}
if !branches
.iter()
.any(|branch| branch == &config.branches().dev())
{
return Err(Error::BranchNotFound(config.branches().dev()));
}
Ok(config)
}