feat(server): reload .git-next.toml from repo when main branch updated

Only does this if the repo config (i.e. the main, next and dev branches) are detailed in the
.git-next.toml file within the repo, rather than in the git-next-server.toml file.

Closes kemitix/git-next#74
This commit is contained in:
Paul Campbell 2024-05-08 07:34:35 +01:00
parent ec2ebe70cf
commit e5a8051a31
5 changed files with 35 additions and 19 deletions

View file

@ -5,8 +5,9 @@ use actix::prelude::*;
use tracing::{info, warn}; use tracing::{info, warn};
use crate::server::{ use crate::server::{
actors::repo::{RepoActor, ValidateRepo}, actors::repo::{LoadConfigFromRepo, RepoActor, ValidateRepo},
config, gitforge, config::{self, RepoConfigSource},
gitforge,
}; };
// advance next to the next commit towards the head of the dev branch // advance next to the next commit towards the head of the dev branch
@ -89,7 +90,10 @@ pub async fn advance_main(
) { ) {
warn!(?err, "Failed") warn!(?err, "Failed")
}; };
addr.do_send(ValidateRepo { message_token }) match repo_config.source() {
RepoConfigSource::Repo => addr.do_send(LoadConfigFromRepo),
RepoConfigSource::Server => addr.do_send(ValidateRepo { message_token }),
}
} }
#[cfg(test)] #[cfg(test)]

View file

@ -95,20 +95,25 @@ impl ServerStorage {
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct RepoConfig { pub struct RepoConfig {
branches: RepoBranches, branches: RepoBranches,
source: RepoConfigSource,
} }
impl RepoConfig { impl RepoConfig {
#[cfg(test)] #[cfg(test)]
pub const fn new(branches: RepoBranches) -> Self { pub const fn new(branches: RepoBranches, source: RepoConfigSource) -> Self {
Self { branches } Self { branches, source }
} }
// #[cfg(test)]
pub fn load(toml: &str) -> Result<Self> { pub fn load(toml: &str) -> Result<Self> {
toml::from_str(toml).map_err(Into::into) toml::from_str(format!("source = \"Repo\"\n{}", toml).as_str()).map_err(Into::into)
} }
pub const fn branches(&self) -> &RepoBranches { pub const fn branches(&self) -> &RepoBranches {
&self.branches &self.branches
} }
pub const fn source(&self) -> RepoConfigSource {
self.source
}
} }
impl Display for RepoConfig { impl Display for RepoConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
@ -116,6 +121,12 @@ impl Display for RepoConfig {
} }
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
pub enum RepoConfigSource {
Repo,
Server,
}
/// Mapped from `.git-next.toml` file at `branches` /// Mapped from `.git-next.toml` file at `branches`
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct RepoBranches { pub struct RepoBranches {
@ -232,6 +243,7 @@ impl ServerRepoConfig {
next: next.to_string(), next: next.to_string(),
dev: dev.to_string(), dev: dev.to_string(),
}, },
source: RepoConfigSource::Server,
}), }),
_ => None, _ => None,
} }

View file

@ -105,6 +105,7 @@ fn load_should_parse_server_config() -> Result<()> {
next: "upcoming".to_string(), next: "upcoming".to_string(),
dev: "sam-dev".to_string(), dev: "sam-dev".to_string(),
}, },
source: RepoConfigSource::Server,
}); });
assert_eq!(repo_config, expected, "RepoConfig"); assert_eq!(repo_config, expected, "RepoConfig");
} }
@ -115,8 +116,7 @@ fn load_should_parse_server_config() -> Result<()> {
#[test] #[test]
fn test_repo_config_load() -> Result<()> { fn test_repo_config_load() -> Result<()> {
let toml = r#" let toml = r#"[branches]
[branches]
main = "main" main = "main"
next = "next" next = "next"
dev = "dev" dev = "dev"
@ -133,6 +133,7 @@ fn test_repo_config_load() -> Result<()> {
next: "next".to_string(), next: "next".to_string(),
dev: "dev".to_string(), dev: "dev".to_string(),
}, },
source: RepoConfigSource::Repo
} }
); );

View file

@ -1,7 +1,7 @@
use crate::server::{ use crate::server::{
config::{ config::{
ApiToken, BranchName, ForgeDetails, ForgeName, ForgeType, GitDir, Hostname, RepoAlias, ApiToken, BranchName, ForgeDetails, ForgeName, ForgeType, GitDir, Hostname, RepoAlias,
RepoBranches, RepoConfig, RepoDetails, RepoPath, User, RepoBranches, RepoConfig, RepoConfigSource, RepoDetails, RepoPath, User,
}, },
types::ServerGeneration, types::ServerGeneration,
}; };
@ -61,10 +61,9 @@ pub fn repo_alias(n: u32) -> RepoAlias {
RepoAlias(format!("repo-alias-{}", n)) RepoAlias(format!("repo-alias-{}", n))
} }
pub fn repo_config(n: u32) -> RepoConfig { pub fn repo_config(n: u32, source: RepoConfigSource) -> RepoConfig {
RepoConfig::new(RepoBranches::new( RepoConfig::new(
format!("main-{n}"), RepoBranches::new(format!("main-{n}"), format!("next-{n}"), format!("dev-{n}")),
format!("next-{n}"), source,
format!("dev-{n}"), )
))
} }

View file

@ -3,7 +3,7 @@ use assert2::let_assert;
use kxio::network::{MockNetwork, StatusCode}; use kxio::network::{MockNetwork, StatusCode};
use crate::server::{ use crate::server::{
config::{BranchName, ForgeType}, config::{BranchName, ForgeType, RepoConfigSource},
types::ServerGeneration, types::ServerGeneration,
}; };
@ -19,7 +19,7 @@ fn test_name() {
1, 1,
ServerGeneration::new(), ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge), common::forge_details(1, ForgeType::MockForge),
Some(common::repo_config(1)), Some(common::repo_config(1, RepoConfigSource::Repo)),
GitDir::new(fs.base()), GitDir::new(fs.base()),
); );
let forge = Forge::new_forgejo(repo_details, net); let forge = Forge::new_forgejo(repo_details, net);
@ -46,7 +46,7 @@ async fn test_branches_get() {
1, 1,
ServerGeneration::new(), ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge), common::forge_details(1, ForgeType::MockForge),
Some(common::repo_config(1)), Some(common::repo_config(1, RepoConfigSource::Repo)),
GitDir::new(fs.base()), GitDir::new(fs.base()),
); );