2024-04-16 22:21:55 +01:00
|
|
|
use kxio::network;
|
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-04-16 22:21:55 +01:00
|
|
|
use crate::server::{
|
|
|
|
config::{BranchName, RepoConfig, RepoDetails},
|
|
|
|
gitforge::{self, ForgeFileError},
|
|
|
|
};
|
2024-04-09 14:52:12 +01:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RepoConfigFileNotFound;
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RepoConfigIsNotFile;
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RepoConfigDecodeError;
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RepoConfigParseError;
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RepoConfigUnknownError(pub network::StatusCode);
|
|
|
|
|
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,
|
|
|
|
) -> Result<RepoConfig, OneOf<(ForgeFileError, toml::de::Error, RepoConfigValidationErrors)>> {
|
|
|
|
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-04-16 22:21:55 +01:00
|
|
|
.any(|branch| branch.name() == &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-04-16 22:21:55 +01:00
|
|
|
.any(|branch| branch.name() == &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-04-16 22:21:55 +01:00
|
|
|
.any(|branch| branch.name() == &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)
|
|
|
|
}
|