From e5a8051a3160902b78f29b0e4fdf6dfbc1bbb295 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 8 May 2024 07:34:35 +0100 Subject: [PATCH] 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 --- src/server/actors/repo/branch.rs | 10 +++++++--- src/server/config/mod.rs | 20 ++++++++++++++++---- src/server/config/tests.rs | 5 +++-- src/server/gitforge/tests/common.rs | 13 ++++++------- src/server/gitforge/tests/forgejo.rs | 6 +++--- 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/server/actors/repo/branch.rs b/src/server/actors/repo/branch.rs index aa14181..b771964 100644 --- a/src/server/actors/repo/branch.rs +++ b/src/server/actors/repo/branch.rs @@ -5,8 +5,9 @@ use actix::prelude::*; use tracing::{info, warn}; use crate::server::{ - actors::repo::{RepoActor, ValidateRepo}, - config, gitforge, + actors::repo::{LoadConfigFromRepo, RepoActor, ValidateRepo}, + config::{self, RepoConfigSource}, + gitforge, }; // 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") }; - 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)] diff --git a/src/server/config/mod.rs b/src/server/config/mod.rs index b12ba13..3b1e263 100644 --- a/src/server/config/mod.rs +++ b/src/server/config/mod.rs @@ -95,20 +95,25 @@ impl ServerStorage { #[derive(Clone, Debug, PartialEq, Eq, Deserialize)] pub struct RepoConfig { branches: RepoBranches, + source: RepoConfigSource, } impl RepoConfig { #[cfg(test)] - pub const fn new(branches: RepoBranches) -> Self { - Self { branches } + pub const fn new(branches: RepoBranches, source: RepoConfigSource) -> Self { + Self { branches, source } } - // #[cfg(test)] + pub fn load(toml: &str) -> Result { - 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 { &self.branches } + + pub const fn source(&self) -> RepoConfigSource { + self.source + } } impl Display for RepoConfig { 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` #[derive(Clone, Debug, PartialEq, Eq, Deserialize)] pub struct RepoBranches { @@ -232,6 +243,7 @@ impl ServerRepoConfig { next: next.to_string(), dev: dev.to_string(), }, + source: RepoConfigSource::Server, }), _ => None, } diff --git a/src/server/config/tests.rs b/src/server/config/tests.rs index cc83fdb..284dd60 100644 --- a/src/server/config/tests.rs +++ b/src/server/config/tests.rs @@ -105,6 +105,7 @@ fn load_should_parse_server_config() -> Result<()> { next: "upcoming".to_string(), dev: "sam-dev".to_string(), }, + source: RepoConfigSource::Server, }); assert_eq!(repo_config, expected, "RepoConfig"); } @@ -115,8 +116,7 @@ fn load_should_parse_server_config() -> Result<()> { #[test] fn test_repo_config_load() -> Result<()> { - let toml = r#" - [branches] + let toml = r#"[branches] main = "main" next = "next" dev = "dev" @@ -133,6 +133,7 @@ fn test_repo_config_load() -> Result<()> { next: "next".to_string(), dev: "dev".to_string(), }, + source: RepoConfigSource::Repo } ); diff --git a/src/server/gitforge/tests/common.rs b/src/server/gitforge/tests/common.rs index be7242a..238108e 100644 --- a/src/server/gitforge/tests/common.rs +++ b/src/server/gitforge/tests/common.rs @@ -1,7 +1,7 @@ use crate::server::{ config::{ ApiToken, BranchName, ForgeDetails, ForgeName, ForgeType, GitDir, Hostname, RepoAlias, - RepoBranches, RepoConfig, RepoDetails, RepoPath, User, + RepoBranches, RepoConfig, RepoConfigSource, RepoDetails, RepoPath, User, }, types::ServerGeneration, }; @@ -61,10 +61,9 @@ pub fn repo_alias(n: u32) -> RepoAlias { RepoAlias(format!("repo-alias-{}", n)) } -pub fn repo_config(n: u32) -> RepoConfig { - RepoConfig::new(RepoBranches::new( - format!("main-{n}"), - format!("next-{n}"), - format!("dev-{n}"), - )) +pub fn repo_config(n: u32, source: RepoConfigSource) -> RepoConfig { + RepoConfig::new( + RepoBranches::new(format!("main-{n}"), format!("next-{n}"), format!("dev-{n}")), + source, + ) } diff --git a/src/server/gitforge/tests/forgejo.rs b/src/server/gitforge/tests/forgejo.rs index 0815770..67fa5e8 100644 --- a/src/server/gitforge/tests/forgejo.rs +++ b/src/server/gitforge/tests/forgejo.rs @@ -3,7 +3,7 @@ use assert2::let_assert; use kxio::network::{MockNetwork, StatusCode}; use crate::server::{ - config::{BranchName, ForgeType}, + config::{BranchName, ForgeType, RepoConfigSource}, types::ServerGeneration, }; @@ -19,7 +19,7 @@ fn test_name() { 1, ServerGeneration::new(), common::forge_details(1, ForgeType::MockForge), - Some(common::repo_config(1)), + Some(common::repo_config(1, RepoConfigSource::Repo)), GitDir::new(fs.base()), ); let forge = Forge::new_forgejo(repo_details, net); @@ -46,7 +46,7 @@ async fn test_branches_get() { 1, ServerGeneration::new(), common::forge_details(1, ForgeType::MockForge), - Some(common::repo_config(1)), + Some(common::repo_config(1, RepoConfigSource::Repo)), GitDir::new(fs.base()), );