feat(config): Allow repo config to be specified in server config
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

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};
pub async fn load(details: RepoDetails, addr: Addr<RepoActor>, net: Network) {
let config = match details.forge.forge_type {
#[cfg(feature = "forgejo")]
ForgeType::ForgeJo => forge::forgejo::config::load(&details, &net).await,
#[cfg(test)]
ForgeType::MockForge => forge::mock::config::load(&details, &net).await,
};
match config {
Ok(config) => addr.do_send(LoadedConfig(config)),
Err(err) => {
error!(?err, "Failed to load config");
let config = match details.config {
Some(config) => config,
None => {
let config = match details.forge.forge_type {
#[cfg(feature = "forgejo")]
ForgeType::ForgeJo => forge::forgejo::config::load(&details, &net).await,
#[cfg(test)]
ForgeType::MockForge => forge::mock::config::load(&details, &net).await,
};
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 {
repo: String,
branch: String,
main: Option<String>,
next: Option<String>,
dev: Option<String>,
}
impl Repo {
#[allow(dead_code)]
@ -135,6 +138,19 @@ impl Repo {
pub fn branch(&self) -> BranchName {
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)]
impl AsRef<Self> for Repo {
@ -252,12 +268,14 @@ pub struct RepoDetails {
pub repo: RepoPath,
pub branch: BranchName,
pub forge: ForgeDetails,
pub config: Option<RepoConfig>,
}
impl RepoDetails {
pub fn new(name: &RepoName, repo: &Repo, forge_name: &ForgeName, forge: &Forge) -> Self {
Self {
name: name.clone(),
repo: RepoPath(repo.repo.clone()),
config: repo.repo_config(),
branch: BranchName(repo.branch.clone()),
forge: ForgeDetails {
name: forge_name.clone(),
@ -326,7 +344,14 @@ mod tests {
[forge.default.repos]
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)?;
@ -345,6 +370,9 @@ mod tests {
Repo {
repo: "user/hello".to_string(),
branch: "main".to_string(),
main: None,
next: None,
dev: None,
},
),
(
@ -352,13 +380,40 @@ mod tests {
Repo {
repo: "user/world".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(())
}