2024-06-09 10:21:09 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2024-05-23 17:53:36 +01:00
|
|
|
//
|
|
|
|
use actix::prelude::*;
|
|
|
|
|
|
|
|
use tracing::{error, info};
|
|
|
|
|
2024-05-23 16:50:36 +01:00
|
|
|
use git_next_config as config;
|
|
|
|
use git_next_git as git;
|
|
|
|
|
2024-05-23 17:53:36 +01:00
|
|
|
use super::{LoadedConfig, RepoActor};
|
|
|
|
|
|
|
|
/// Loads the [RepoConfig] from the `.git-next.toml` file in the repository
|
|
|
|
#[tracing::instrument(skip_all, fields(branch = %repo_details.branch))]
|
2024-05-26 16:51:51 +01:00
|
|
|
pub async fn load_file(
|
|
|
|
repo_details: git::RepoDetails,
|
|
|
|
addr: Addr<RepoActor>,
|
|
|
|
open_repository: git::OpenRepository,
|
|
|
|
) {
|
2024-05-23 17:53:36 +01:00
|
|
|
info!("Loading .git-next.toml from repo");
|
2024-05-27 07:33:06 +01:00
|
|
|
let repo_config = match load(&repo_details, &open_repository).await {
|
2024-05-23 17:53:36 +01:00
|
|
|
Ok(repo_config) => repo_config,
|
|
|
|
Err(err) => {
|
|
|
|
error!(?err, "Failed to load config");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
info!("Loaded .git-next.toml from repo");
|
|
|
|
addr.do_send(LoadedConfig(repo_config));
|
|
|
|
}
|
2024-04-09 10:44:01 +01:00
|
|
|
|
2024-05-23 17:53:36 +01:00
|
|
|
async fn load(
|
2024-05-23 16:50:36 +01:00
|
|
|
details: &git::RepoDetails,
|
2024-05-27 07:33:06 +01:00
|
|
|
open_repository: &git::OpenRepository,
|
2024-05-23 16:50:36 +01:00
|
|
|
) -> Result<config::RepoConfig, Error> {
|
2024-06-09 10:21:09 +01:00
|
|
|
let contents = open_repository.read_file(&details.branch, &PathBuf::from(".git-next.toml"))?;
|
2024-06-19 07:02:18 +01:00
|
|
|
let config = config::RepoConfig::parse(&contents)?;
|
2024-05-27 07:33:06 +01:00
|
|
|
let config = validate(config, open_repository).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 16:50:36 +01:00
|
|
|
File(git::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-06-08 20:43:20 +01:00
|
|
|
Branch(git::push::Error),
|
2024-05-23 16:50:36 +01:00
|
|
|
BranchNotFound(config::BranchName),
|
2024-04-16 22:21:55 +01:00
|
|
|
}
|
2024-04-09 14:52:12 +01:00
|
|
|
|
2024-05-23 16:50:36 +01:00
|
|
|
pub async fn validate(
|
|
|
|
config: config::RepoConfig,
|
2024-05-27 07:33:06 +01:00
|
|
|
open_repository: &git::OpenRepository,
|
2024-05-23 16:50:36 +01:00
|
|
|
) -> Result<config::RepoConfig, Error> {
|
2024-05-27 07:33:06 +01:00
|
|
|
let branches = open_repository.remote_branches()?;
|
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)
|
|
|
|
}
|