git-next/crates/server/src/config/load.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

use git_next_config::{BranchName, RepoConfig};
use git_next_git::RepoDetails;
use tracing::error;
2024-04-09 10:44:01 +01:00
use crate::gitforge::{self, ForgeFileError};
2024-05-18 22:16:17 +01:00
pub async fn load(details: &RepoDetails, forge: &gitforge::Forge) -> Result<RepoConfig, Error> {
let contents = forge
.file_contents_get(&details.branch, ".git-next.toml")
2024-05-18 22:16:17 +01:00
.await?;
let config = RepoConfig::load(&contents)?;
let config = validate(config, forge).await?;
Ok(config)
2024-04-09 10:44:01 +01:00
}
2024-05-18 22:16:17 +01:00
#[derive(Debug, derive_more::From, derive_more::Display)]
pub enum Error {
2024-05-18 22:16:17 +01:00
File(ForgeFileError),
Config(crate::config::Error),
Toml(toml::de::Error),
Forge(gitforge::ForgeBranchError),
BranchNotFound(BranchName),
}
pub async fn validate(config: RepoConfig, forge: &gitforge::Forge) -> Result<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)
}