2024-05-11 19:46:20 +01:00
|
|
|
use git_next_config::{BranchName, RepoConfig};
|
|
|
|
use git_next_git::RepoDetails;
|
2024-04-09 10:44:01 +01:00
|
|
|
use terrors::OneOf;
|
2024-04-16 22:21:55 +01:00
|
|
|
use tracing::error;
|
2024-04-09 10:44:01 +01:00
|
|
|
|
2024-05-11 19:46:20 +01:00
|
|
|
use crate::gitforge::{self, ForgeFileError};
|
2024-04-09 14:52:12 +01:00
|
|
|
|
2024-04-09 10:44:01 +01:00
|
|
|
pub async fn load(
|
2024-04-09 14:52:12 +01:00
|
|
|
details: &RepoDetails,
|
2024-04-16 22:21:55 +01:00
|
|
|
forge: &gitforge::Forge,
|
2024-04-28 18:13:45 +01:00
|
|
|
) -> Result<
|
|
|
|
RepoConfig,
|
|
|
|
OneOf<(
|
|
|
|
ForgeFileError,
|
2024-05-11 18:57:18 +01:00
|
|
|
crate::config::Error,
|
2024-04-28 18:13:45 +01:00
|
|
|
toml::de::Error,
|
|
|
|
RepoConfigValidationErrors,
|
|
|
|
)>,
|
|
|
|
> {
|
2024-04-16 22:21:55 +01:00
|
|
|
let contents = forge
|
|
|
|
.file_contents_get(&details.branch, ".git-next.toml")
|
2024-04-09 14:52:12 +01:00
|
|
|
.await
|
2024-04-16 22:21:55 +01:00
|
|
|
.map_err(OneOf::new)?;
|
|
|
|
let config = RepoConfig::load(&contents).map_err(OneOf::new)?;
|
|
|
|
let config = validate(config, forge).await.map_err(OneOf::new)?;
|
2024-04-09 14:52:12 +01:00
|
|
|
Ok(config)
|
2024-04-09 10:44:01 +01:00
|
|
|
}
|
|
|
|
|
2024-04-09 14:52:12 +01:00
|
|
|
#[derive(Debug)]
|
2024-04-16 22:21:55 +01:00
|
|
|
pub enum RepoConfigValidationErrors {
|
|
|
|
Forge(gitforge::ForgeBranchError),
|
|
|
|
BranchNotFound(BranchName),
|
|
|
|
}
|
2024-04-09 14:52:12 +01:00
|
|
|
|
|
|
|
pub async fn validate(
|
|
|
|
config: RepoConfig,
|
2024-04-16 22:21:55 +01:00
|
|
|
forge: &gitforge::Forge,
|
|
|
|
) -> Result<RepoConfig, RepoConfigValidationErrors> {
|
|
|
|
let branches = forge.branches_get_all().await.map_err(|e| {
|
2024-04-09 14:52:12 +01:00
|
|
|
error!(?e, "Failed to list branches");
|
2024-04-16 22:21:55 +01:00
|
|
|
RepoConfigValidationErrors::Forge(e)
|
2024-04-09 14:52:12 +01:00
|
|
|
})?;
|
|
|
|
if !branches
|
|
|
|
.iter()
|
2024-05-11 19:46:20 +01:00
|
|
|
.any(|branch| branch == &config.branches().main())
|
2024-04-09 14:52:12 +01:00
|
|
|
{
|
2024-04-16 22:21:55 +01:00
|
|
|
return Err(RepoConfigValidationErrors::BranchNotFound(
|
2024-04-09 14:52:12 +01:00
|
|
|
config.branches().main(),
|
2024-04-16 22:21:55 +01:00
|
|
|
));
|
2024-04-09 14:52:12 +01:00
|
|
|
}
|
|
|
|
if !branches
|
|
|
|
.iter()
|
2024-05-11 19:46:20 +01:00
|
|
|
.any(|branch| branch == &config.branches().next())
|
2024-04-09 14:52:12 +01:00
|
|
|
{
|
2024-04-16 22:21:55 +01:00
|
|
|
return Err(RepoConfigValidationErrors::BranchNotFound(
|
2024-04-09 14:52:12 +01:00
|
|
|
config.branches().next(),
|
2024-04-16 22:21:55 +01:00
|
|
|
));
|
2024-04-09 14:52:12 +01:00
|
|
|
}
|
|
|
|
if !branches
|
|
|
|
.iter()
|
2024-05-11 19:46:20 +01:00
|
|
|
.any(|branch| branch == &config.branches().dev())
|
2024-04-09 14:52:12 +01:00
|
|
|
{
|
2024-04-16 22:21:55 +01:00
|
|
|
return Err(RepoConfigValidationErrors::BranchNotFound(
|
2024-04-09 14:52:12 +01:00
|
|
|
config.branches().dev(),
|
2024-04-16 22:21:55 +01:00
|
|
|
));
|
2024-04-09 14:52:12 +01:00
|
|
|
}
|
|
|
|
Ok(config)
|
|
|
|
}
|