git-next/crates/repo-actor/src/load.rs

56 lines
1.5 KiB
Rust
Raw Normal View History

2024-05-23 16:50:36 +01:00
use git_next_config as config;
use git_next_forge as forge;
2024-05-23 16:50:36 +01:00
use git_next_git as git;
use tracing::error;
2024-04-09 10:44:01 +01:00
2024-05-23 16:50:36 +01:00
pub async fn load(
details: &git::RepoDetails,
forge: &forge::Forge,
) -> Result<config::RepoConfig, Error> {
let contents = forge
.file_contents_get(&details.branch, ".git-next.toml")
2024-05-18 22:16:17 +01:00
.await?;
2024-05-23 16:50:36 +01:00
let config = config::RepoConfig::load(&contents)?;
2024-05-18 22:16:17 +01:00
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-23 16:50:36 +01:00
File(git::file::Error),
Config(config::server::Error),
2024-05-18 22:16:17 +01:00
Toml(toml::de::Error),
2024-05-23 16:50:36 +01:00
Forge(git::branch::Error),
BranchNotFound(config::BranchName),
}
2024-05-23 16:50:36 +01:00
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)
}