2024-05-22 08:41:30 +01:00
|
|
|
use git_next_config::{self as config, BranchName, RepoConfig};
|
2024-05-11 19:46:20 +01:00
|
|
|
use git_next_git::RepoDetails;
|
2024-04-16 22:21:55 +01:00
|
|
|
use tracing::error;
|
2024-04-09 10:44:01 +01:00
|
|
|
|
2024-05-23 08:01:16 +01:00
|
|
|
use crate::gitforge;
|
2024-04-09 14:52:12 +01:00
|
|
|
|
2024-05-18 22:16:17 +01:00
|
|
|
pub async fn load(details: &RepoDetails, forge: &gitforge::Forge) -> Result<RepoConfig, Error> {
|
2024-04-16 22:21:55 +01:00
|
|
|
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?;
|
2024-04-09 14:52:12 +01:00
|
|
|
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)]
|
2024-05-15 20:46:29 +01:00
|
|
|
pub enum Error {
|
2024-05-23 08:01:16 +01:00
|
|
|
File(gitforge::file::Error),
|
2024-05-22 08:41:30 +01:00
|
|
|
Config(config::server::Error),
|
2024-05-18 22:16:17 +01:00
|
|
|
Toml(toml::de::Error),
|
2024-05-23 08:01:16 +01:00
|
|
|
Forge(gitforge::branch::Error),
|
2024-04-16 22:21:55 +01:00
|
|
|
BranchNotFound(BranchName),
|
|
|
|
}
|
2024-04-09 14:52:12 +01:00
|
|
|
|
2024-05-15 20:46:29 +01:00
|
|
|
pub async fn validate(config: RepoConfig, forge: &gitforge::Forge) -> Result<RepoConfig, Error> {
|
2024-04-16 22:21:55 +01:00
|
|
|
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-05-15 20:46:29 +01:00
|
|
|
Error::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-05-15 20:46:29 +01:00
|
|
|
return Err(Error::BranchNotFound(config.branches().main()));
|
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-05-15 20:46:29 +01:00
|
|
|
return Err(Error::BranchNotFound(config.branches().next()));
|
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-05-15 20:46:29 +01:00
|
|
|
return Err(Error::BranchNotFound(config.branches().dev()));
|
2024-04-09 14:52:12 +01:00
|
|
|
}
|
|
|
|
Ok(config)
|
|
|
|
}
|