feat(config): Allow repo config to be specified in server config

Closes kemitix/git-next#28
This commit is contained in:
Paul Campbell 2024-04-13 11:44:40 +01:00
parent 229d47f7c7
commit 0105631e3a
2 changed files with 75 additions and 13 deletions

View file

@ -10,16 +10,23 @@ use crate::server::{
use super::{LoadedConfig, RepoActor}; use super::{LoadedConfig, RepoActor};
pub async fn load(details: RepoDetails, addr: Addr<RepoActor>, net: Network) { pub async fn load(details: RepoDetails, addr: Addr<RepoActor>, net: Network) {
let config = match details.forge.forge_type { let config = match details.config {
#[cfg(feature = "forgejo")] Some(config) => config,
ForgeType::ForgeJo => forge::forgejo::config::load(&details, &net).await, None => {
#[cfg(test)] let config = match details.forge.forge_type {
ForgeType::MockForge => forge::mock::config::load(&details, &net).await, #[cfg(feature = "forgejo")]
}; ForgeType::ForgeJo => forge::forgejo::config::load(&details, &net).await,
match config { #[cfg(test)]
Ok(config) => addr.do_send(LoadedConfig(config)), ForgeType::MockForge => forge::mock::config::load(&details, &net).await,
Err(err) => { };
error!(?err, "Failed to load config"); match config {
Ok(config) => config,
Err(err) => {
error!(?err, "Failed to load config");
return;
}
}
} }
} };
addr.do_send(LoadedConfig(config));
} }

View file

@ -124,6 +124,9 @@ impl Display for Forge {
pub struct Repo { pub struct Repo {
repo: String, repo: String,
branch: String, branch: String,
main: Option<String>,
next: Option<String>,
dev: Option<String>,
} }
impl Repo { impl Repo {
#[allow(dead_code)] #[allow(dead_code)]
@ -135,6 +138,19 @@ impl Repo {
pub fn branch(&self) -> BranchName { pub fn branch(&self) -> BranchName {
BranchName(self.branch.clone()) BranchName(self.branch.clone())
} }
/// Returns a RepoConfig from the server configuration if ALL THREE branches were provided
pub fn repo_config(&self) -> Option<RepoConfig> {
match (&self.main, &self.next, &self.dev) {
(Some(main), Some(next), Some(dev)) => Some(RepoConfig {
branches: RepoBranches {
main: main.to_string(),
next: next.to_string(),
dev: dev.to_string(),
},
}),
_ => None,
}
}
} }
#[cfg(test)] #[cfg(test)]
impl AsRef<Self> for Repo { impl AsRef<Self> for Repo {
@ -252,12 +268,14 @@ pub struct RepoDetails {
pub repo: RepoPath, pub repo: RepoPath,
pub branch: BranchName, pub branch: BranchName,
pub forge: ForgeDetails, pub forge: ForgeDetails,
pub config: Option<RepoConfig>,
} }
impl RepoDetails { impl RepoDetails {
pub fn new(name: &RepoName, repo: &Repo, forge_name: &ForgeName, forge: &Forge) -> Self { pub fn new(name: &RepoName, repo: &Repo, forge_name: &ForgeName, forge: &Forge) -> Self {
Self { Self {
name: name.clone(), name: name.clone(),
repo: RepoPath(repo.repo.clone()), repo: RepoPath(repo.repo.clone()),
config: repo.repo_config(),
branch: BranchName(repo.branch.clone()), branch: BranchName(repo.branch.clone()),
forge: ForgeDetails { forge: ForgeDetails {
name: forge_name.clone(), name: forge_name.clone(),
@ -326,7 +344,14 @@ mod tests {
[forge.default.repos] [forge.default.repos]
hello = { repo = "user/hello", branch = "main" } hello = { repo = "user/hello", branch = "main" }
world = { repo = "user/world", branch = "master" } world = { repo = "user/world", branch = "master", main = "main", next = "next", dev = "dev" }
[forge.default.repos.sam]
repo = "user/sam"
branch = "main"
main = "master"
next = "upcoming"
dev = "sam-dev"
"#, "#,
) )
.map_err(OneOf::new)?; .map_err(OneOf::new)?;
@ -345,6 +370,9 @@ mod tests {
Repo { Repo {
repo: "user/hello".to_string(), repo: "user/hello".to_string(),
branch: "main".to_string(), branch: "main".to_string(),
main: None,
next: None,
dev: None,
}, },
), ),
( (
@ -352,13 +380,40 @@ mod tests {
Repo { Repo {
repo: "user/world".to_string(), repo: "user/world".to_string(),
branch: "master".to_string(), branch: "master".to_string(),
main: Some("main".to_string()),
next: Some("next".to_string()),
dev: Some("dev".to_string()),
},
),
(
"sam".to_string(),
Repo {
repo: "user/sam".to_string(),
branch: "main".to_string(),
main: Some("master".to_string()),
next: Some("upcoming".to_string()),
dev: Some("sam-dev".to_string()),
}, },
), ),
]), ]),
}, },
)]), )]),
}; };
assert_eq!(config, expected); assert_eq!(config, expected, "ServerConfig");
if let Some(forge) = config.forge.get("world") {
if let Some(repo) = forge.repos.get("sam") {
let repo_config = repo.repo_config();
let expected = Some(RepoConfig {
branches: RepoBranches {
main: "master".to_string(),
next: "upcoming".to_string(),
dev: "sam-dev".to_string(),
},
});
assert_eq!(repo_config, expected, "RepoConfig");
}
}
Ok(()) Ok(())
} }