From d70baa4350f175a32e25eb3689184ac595c98bd6 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 15 May 2024 07:55:05 +0100 Subject: [PATCH] refactor(config): more derive_more replacing boilerplate --- Cargo.toml | 3 +- crates/config/src/api_token.rs | 9 +- crates/config/src/forge_config.rs | 22 ++-- crates/config/src/forge_details.rs | 34 +++++- crates/config/src/forge_name.rs | 4 +- crates/config/src/git_dir.rs | 13 +-- crates/config/src/repo_branches.rs | 10 +- crates/config/src/repo_config.rs | 8 +- crates/config/src/repo_path.rs | 4 +- crates/config/src/server_repo_config.rs | 30 +++-- crates/config/src/tests.rs | 89 ++++++-------- crates/config/src/user.rs | 4 +- crates/git/src/repo_details.rs | 30 ++--- crates/git/src/validate.rs | 2 +- crates/server/src/actors/repo/mod.rs | 6 +- crates/server/src/actors/repo/webhook.rs | 12 +- crates/server/src/config/mod.rs | 2 +- crates/server/src/config/tests.rs | 109 +++++++++--------- .../src/gitforge/forgejo/branch/get_all.rs | 4 +- .../forgejo/branch/validate_positions.rs | 4 +- .../server/src/gitforge/forgejo/file/mod.rs | 4 +- crates/server/src/gitforge/forgejo/mod.rs | 4 +- crates/server/src/gitforge/tests/common.rs | 30 +++-- 23 files changed, 217 insertions(+), 220 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ef8905b..cb52c16 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,9 +62,10 @@ warp = "0.3" # error handling derive_more = { version = "1.0.0-beta.6", features = [ - "from", + "constructor", "display", "deref", + "from", ] } terrors = "0.3" diff --git a/crates/config/src/api_token.rs b/crates/config/src/api_token.rs index 1d2acfb..e07d1af 100644 --- a/crates/config/src/api_token.rs +++ b/crates/config/src/api_token.rs @@ -1,13 +1,8 @@ /// The API Token for the [user] /// ForgeJo: https://{hostname}/user/settings/applications /// Github: https://github.com/settings/tokens -#[derive(Clone, Debug)] -pub struct ApiToken(pub secrecy::Secret); -impl From for ApiToken { - fn from(value: String) -> Self { - Self(value.into()) - } -} +#[derive(Clone, Debug, derive_more::Constructor)] +pub struct ApiToken(secrecy::Secret); /// The API Token is in effect a password, so it must be explicitly exposed to access its value impl secrecy::ExposeSecret for ApiToken { fn expose_secret(&self) -> &String { diff --git a/crates/config/src/forge_config.rs b/crates/config/src/forge_config.rs index a8ec57f..8fb10bd 100644 --- a/crates/config/src/forge_config.rs +++ b/crates/config/src/forge_config.rs @@ -4,14 +4,16 @@ use crate::{ApiToken, ForgeType, Hostname, RepoAlias, ServerRepoConfig, User}; /// Defines a Forge to connect to /// Maps from `git-next-server.toml` at `forge.{forge}` -#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Display)] +#[derive( + Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Constructor, derive_more::Display, +)] #[display("{}:{}@{}", forge_type.to_string().to_lowercase(), user, hostname)] pub struct ForgeConfig { - pub forge_type: ForgeType, - pub hostname: String, - pub user: String, - pub token: String, - pub repos: BTreeMap, + forge_type: ForgeType, + hostname: String, + user: String, + token: String, + repos: BTreeMap, } impl ForgeConfig { pub const fn forge_type(&self) -> ForgeType { @@ -23,11 +25,11 @@ impl ForgeConfig { } pub fn user(&self) -> User { - User(self.user.clone()) + User::new(self.user.clone()) } pub fn token(&self) -> ApiToken { - ApiToken(self.token.clone().into()) + ApiToken::new(self.token.clone().into()) } pub fn repos(&self) -> impl Iterator { @@ -35,4 +37,8 @@ impl ForgeConfig { .iter() .map(|(name, repo)| (RepoAlias::new(name), repo)) } + + pub fn get_repo(&self, arg: &str) -> Option<&ServerRepoConfig> { + self.repos.get(arg) + } } diff --git a/crates/config/src/forge_details.rs b/crates/config/src/forge_details.rs index 1ea26d2..6754ca6 100644 --- a/crates/config/src/forge_details.rs +++ b/crates/config/src/forge_details.rs @@ -1,16 +1,38 @@ use crate::{ApiToken, ForgeConfig, ForgeName, ForgeType, Hostname, User}; /// The derived information about a Forge, used to create interactions with it -#[derive(Clone, Debug)] +#[derive(Clone, Debug, derive_more::Constructor)] pub struct ForgeDetails { - pub forge_name: ForgeName, - pub forge_type: ForgeType, - pub hostname: Hostname, - pub user: User, - pub token: ApiToken, + forge_name: ForgeName, + forge_type: ForgeType, + hostname: Hostname, + user: User, + token: ApiToken, // API Token // Private SSH Key Path } +impl ForgeDetails { + pub const fn forge_name(&self) -> &ForgeName { + &self.forge_name + } + pub const fn forge_type(&self) -> ForgeType { + self.forge_type + } + pub const fn hostname(&self) -> &Hostname { + &self.hostname + } + pub const fn user(&self) -> &User { + &self.user + } + pub const fn token(&self) -> &ApiToken { + &self.token + } + pub fn with_hostname(self, hostname: Hostname) -> Self { + let mut me = self; + me.hostname = hostname; + me + } +} impl From<(&ForgeName, &ForgeConfig)> for ForgeDetails { fn from(forge: (&ForgeName, &ForgeConfig)) -> Self { Self { diff --git a/crates/config/src/forge_name.rs b/crates/config/src/forge_name.rs index 16fd16c..bea4284 100644 --- a/crates/config/src/forge_name.rs +++ b/crates/config/src/forge_name.rs @@ -1,8 +1,8 @@ use std::path::PathBuf; /// The name of a Forge to connect to -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] -pub struct ForgeName(pub String); +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] +pub struct ForgeName(String); impl From<&ForgeName> for PathBuf { fn from(value: &ForgeName) -> Self { Self::from(&value.0) diff --git a/crates/config/src/git_dir.rs b/crates/config/src/git_dir.rs index 424c430..fb5b23a 100644 --- a/crates/config/src/git_dir.rs +++ b/crates/config/src/git_dir.rs @@ -1,6 +1,8 @@ -use std::{ops::Deref, path::PathBuf}; +use std::path::PathBuf; -#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, derive_more::From)] +#[derive( + Debug, Clone, PartialEq, Eq, serde::Deserialize, derive_more::Deref, derive_more::From, +)] pub struct GitDir(PathBuf); impl GitDir { pub fn new(pathbuf: &std::path::Path) -> Self { @@ -11,13 +13,6 @@ impl GitDir { &self.0 } } -impl Deref for GitDir { - type Target = PathBuf; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} impl std::fmt::Display for GitDir { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", &self.0.display()) diff --git a/crates/config/src/repo_branches.rs b/crates/config/src/repo_branches.rs index 2685d6d..637a2dd 100644 --- a/crates/config/src/repo_branches.rs +++ b/crates/config/src/repo_branches.rs @@ -1,12 +1,14 @@ use crate::BranchName; /// Mapped from `.git-next.toml` file at `branches` -#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Display)] +#[derive( + Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Constructor, derive_more::Display, +)] #[display("{},{},{}", main, next, dev)] pub struct RepoBranches { - pub main: String, - pub next: String, - pub dev: String, + main: String, + next: String, + dev: String, } impl RepoBranches { pub fn main(&self) -> BranchName { diff --git a/crates/config/src/repo_config.rs b/crates/config/src/repo_config.rs index d1ccf06..6a649e7 100644 --- a/crates/config/src/repo_config.rs +++ b/crates/config/src/repo_config.rs @@ -4,11 +4,13 @@ use crate::RepoConfigSource; /// Mapped from `.git-next.toml` file in target repo /// Is also derived from the optional parameters in `git-next-server.toml` at /// `forge.{forge}.repos.{repo}.(main|next|dev)` -#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Display)] +#[derive( + Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Constructor, derive_more::Display, +)] #[display("{}", branches)] pub struct RepoConfig { - pub branches: RepoBranches, - pub source: RepoConfigSource, + branches: RepoBranches, + source: RepoConfigSource, } impl RepoConfig { pub fn load(toml: &str) -> Result { diff --git a/crates/config/src/repo_path.rs b/crates/config/src/repo_path.rs index 1632902..832f9be 100644 --- a/crates/config/src/repo_path.rs +++ b/crates/config/src/repo_path.rs @@ -1,5 +1,5 @@ /// The path for the repo within the forge. /// Typically this is composed of the user or organisation and the name of the repo /// e.g. `{user}/{repo}` -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] -pub struct RepoPath(pub String); +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] +pub struct RepoPath(String); diff --git a/crates/config/src/server_repo_config.rs b/crates/config/src/server_repo_config.rs index 13725ad..7e6e37f 100644 --- a/crates/config/src/server_repo_config.rs +++ b/crates/config/src/server_repo_config.rs @@ -4,19 +4,21 @@ use crate::{BranchName, GitDir, RepoBranches, RepoConfig, RepoConfigSource, Repo /// Defines a Repo within a ForgeConfig to be monitored by the server /// Maps from `git-next-server.toml` at `forge.{forge}.repos.{name}` -#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Display)] +#[derive( + Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Constructor, derive_more::Display, +)] #[display("{}@{}", repo, branch)] pub struct ServerRepoConfig { - pub repo: String, - pub branch: String, - pub gitdir: Option, - pub main: Option, - pub next: Option, - pub dev: Option, + repo: String, + branch: String, + gitdir: Option, + main: Option, + next: Option, + dev: Option, } impl ServerRepoConfig { pub fn repo(&self) -> RepoPath { - RepoPath(self.repo.clone()) + RepoPath::new(self.repo.clone()) } pub fn branch(&self) -> BranchName { @@ -30,14 +32,10 @@ impl ServerRepoConfig { /// Returns a RepoConfig from the server configuration if ALL THREE branches were provided pub fn repo_config(&self) -> Option { 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(), - }, - source: RepoConfigSource::Server, - }), + (Some(main), Some(next), Some(dev)) => Some(RepoConfig::new( + RepoBranches::new(main.to_string(), next.to_string(), dev.to_string()), + RepoConfigSource::Server, + )), _ => None, } } diff --git a/crates/config/src/tests.rs b/crates/config/src/tests.rs index 4b3c9ab..5182d00 100644 --- a/crates/config/src/tests.rs +++ b/crates/config/src/tests.rs @@ -9,40 +9,29 @@ mod server_repo_config { #[test] fn should_not_return_repo_config_when_no_branches() { - let src = ServerRepoConfig { - repo: "".to_string(), - branch: "".to_string(), - gitdir: None, - main: None, - next: None, - dev: None, - }; + let src = ServerRepoConfig::new("".to_string(), "".to_string(), None, None, None, None); let_assert!(None = src.repo_config()); } #[test] fn should_return_repo_config_when_branches() { - let src = ServerRepoConfig { - repo: "".to_string(), - branch: "".to_string(), - gitdir: None, - main: Some("main".to_string()), - next: Some("next".to_string()), - dev: Some("dev".to_string()), - }; + let src = ServerRepoConfig::new( + "".to_string(), + "".to_string(), + None, + Some("main".to_string()), + Some("next".to_string()), + Some("dev".to_string()), + ); let_assert!(Some(rc) = src.repo_config()); assert_eq!( rc, - RepoConfig { - branches: RepoBranches { - main: "main".to_string(), - next: "next".to_string(), - dev: "dev".to_string() - }, - source: RepoConfigSource::Server - } + RepoConfig::new( + RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string()), + RepoConfigSource::Server + ) ); } } @@ -65,14 +54,10 @@ mod repo_config { assert_eq!( rc, - RepoConfig { - branches: RepoBranches { - main: "main".to_string(), - next: "next".to_string(), - dev: "dev".to_string(), - }, - source: RepoConfigSource::Repo // reading from repo is the default - } + RepoConfig::new( + RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string(),), + RepoConfigSource::Repo // reading from repo is the default + ) ); Ok(()) @@ -90,32 +75,26 @@ mod forge_config { let hostname = "localhost".to_string(); let user = "bob".to_string(); let token = "alpha".to_string(); - let red = ServerRepoConfig { - repo: "red".to_string(), - branch: "main".to_string(), - main: None, - next: None, - dev: None, - gitdir: None, - }; - let blue = ServerRepoConfig { - repo: "blue".to_string(), - branch: "main".to_string(), - main: None, - next: None, - dev: None, - gitdir: None, - }; + let red = ServerRepoConfig::new( + "red".to_string(), + "main".to_string(), + None, + None, + None, + None, + ); + let blue = ServerRepoConfig::new( + "blue".to_string(), + "main".to_string(), + None, + None, + None, + None, + ); let mut repos = BTreeMap::new(); repos.insert("red".to_string(), red.clone()); repos.insert("blue".to_string(), blue.clone()); - let fc = ForgeConfig { - forge_type, - hostname, - user, - token, - repos, - }; + let fc = ForgeConfig::new(forge_type, hostname, user, token, repos); let returned_repos = fc.repos().collect::>(); diff --git a/crates/config/src/user.rs b/crates/config/src/user.rs index d27e2dc..48eea03 100644 --- a/crates/config/src/user.rs +++ b/crates/config/src/user.rs @@ -1,3 +1,3 @@ /// The user within the forge to connect as -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] -pub struct User(pub String); +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] +pub struct User(String); diff --git a/crates/git/src/repo_details.rs b/crates/git/src/repo_details.rs index e61da58..93e83cc 100644 --- a/crates/git/src/repo_details.rs +++ b/crates/git/src/repo_details.rs @@ -7,8 +7,8 @@ use super::{Generation, GitRemote}; /// The derived information about a repo, used to interact with it #[derive(Clone, Debug, derive_more::Display)] -#[display("gen-{}:{}:{}/{}:{}@{}/{}@{}", generation, forge.forge_type, - forge.forge_name, repo_alias, forge.user, forge.hostname, repo_path, +#[display("gen-{}:{}:{}/{}:{}@{}/{}@{}", generation, forge.forge_type(), + forge.forge_name(), repo_alias, forge.user(), forge.hostname(), repo_path, branch)] pub struct RepoDetails { pub generation: Generation, @@ -31,32 +31,32 @@ impl RepoDetails { Self { generation, repo_alias: repo_alias.clone(), - repo_path: RepoPath(server_repo_config.repo.clone()), + repo_path: server_repo_config.repo(), repo_config: server_repo_config.repo_config(), - branch: BranchName::new(&server_repo_config.branch), + branch: server_repo_config.branch(), gitdir, - forge: ForgeDetails { - forge_name: forge_name.clone(), - forge_type: forge_config.forge_type(), - hostname: forge_config.hostname(), - user: forge_config.user(), - token: forge_config.token(), - }, + forge: ForgeDetails::new( + forge_name.clone(), + forge_config.forge_type(), + forge_config.hostname(), + forge_config.user(), + forge_config.token(), + ), } } pub fn origin(&self) -> secrecy::Secret { let repo_details = self; - let user = &repo_details.forge.user; - let hostname = &repo_details.forge.hostname; + let user = &repo_details.forge.user(); + let hostname = &repo_details.forge.hostname(); let repo_path = &repo_details.repo_path; + let expose_secret = repo_details.forge.token(); use secrecy::ExposeSecret; - let expose_secret = &repo_details.forge.token; let token = expose_secret.expose_secret(); let origin = format!("https://{user}:{token}@{hostname}/{repo_path}.git"); origin.into() } pub fn git_remote(&self) -> GitRemote { - GitRemote::new(self.forge.hostname.clone(), self.repo_path.clone()) + GitRemote::new(self.forge.hostname().clone(), self.repo_path.clone()) } } diff --git a/crates/git/src/validate.rs b/crates/git/src/validate.rs index ae7eedf..e86fb80 100644 --- a/crates/git/src/validate.rs +++ b/crates/git/src/validate.rs @@ -48,7 +48,7 @@ pub fn find_default_remote( info!(%host, %path, "found"); Ok(GitRemote::new( Hostname::new(host), - RepoPath(path.to_string()), + RepoPath::new(path.to_string()), )) } diff --git a/crates/server/src/actors/repo/mod.rs b/crates/server/src/actors/repo/mod.rs index 1a0cbd9..a6ca2e6 100644 --- a/crates/server/src/actors/repo/mod.rs +++ b/crates/server/src/actors/repo/mod.rs @@ -37,7 +37,7 @@ impl RepoActor { generation: Generation, net: Network, ) -> Self { - let forge = match details.forge.forge_type { + let forge = match details.forge.forge_type() { #[cfg(feature = "forgejo")] ForgeType::ForgeJo => gitforge::Forge::new_forgejo(details.clone(), net.clone()), ForgeType::MockForge => gitforge::Forge::new_mock(), @@ -84,7 +84,9 @@ impl std::fmt::Display for RepoActor { write!( f, "{}:{}:{}", - self.generation, self.details.forge.forge_name, self.details.repo_alias + self.generation, + self.details.forge.forge_name(), + self.details.repo_alias ) } } diff --git a/crates/server/src/actors/repo/webhook.rs b/crates/server/src/actors/repo/webhook.rs index 552d10b..4c6808e 100644 --- a/crates/server/src/actors/repo/webhook.rs +++ b/crates/server/src/actors/repo/webhook.rs @@ -60,10 +60,10 @@ impl Deref for WebhookAuth { #[tracing::instrument(skip_all, fields(%webhook_id))] pub async fn unregister(webhook_id: WebhookId, repo_details: RepoDetails, net: network::Network) { - let hostname = &repo_details.forge.hostname; + let hostname = &repo_details.forge.hostname(); let repo_path = repo_details.repo_path; use secrecy::ExposeSecret; - let token = repo_details.forge.token.expose_secret(); + let token = repo_details.forge.token().expose_secret(); let url = network::NetUrl::new(format!( "https://{hostname}/api/v1/repos/{repo_path}/hooks/{webhook_id}?token={token}" )); @@ -101,10 +101,10 @@ pub async fn register( unregister(webhook_id, repo_details.clone(), net.clone()).await; } - let hostname = &repo_details.forge.hostname; + let hostname = &repo_details.forge.hostname(); let repo_path = repo_details.repo_path; use secrecy::ExposeSecret; - let token = repo_details.forge.token.expose_secret(); + let token = repo_details.forge.token().expose_secret(); let url = network::NetUrl::new(format!( "https://{hostname}/api/v1/repos/{repo_path}/hooks?token={token}" )); @@ -149,12 +149,12 @@ async fn find_existing_webhooks( net: &network::Network, ) -> Vec { let mut ids: Vec = vec![]; - let hostname = &repo_details.forge.hostname; + let hostname = &repo_details.forge.hostname(); let repo_path = &repo_details.repo_path; let mut page = 1; loop { use secrecy::ExposeSecret; - let token = &repo_details.forge.token.expose_secret(); + let token = &repo_details.forge.token().expose_secret(); let url = format!("https://{hostname}/api/v1/repos/{repo_path}/hooks?page={page}&token={token}"); let net_url = network::NetUrl::new(url); diff --git a/crates/server/src/config/mod.rs b/crates/server/src/config/mod.rs index 64b80f3..552dc4f 100644 --- a/crates/server/src/config/mod.rs +++ b/crates/server/src/config/mod.rs @@ -45,7 +45,7 @@ impl ServerConfig { pub(crate) fn forges(&self) -> impl Iterator { self.forge .iter() - .map(|(name, forge)| (ForgeName(name.clone()), forge)) + .map(|(name, forge)| (ForgeName::new(name.clone()), forge)) } pub const fn storage(&self) -> &ServerStorage { diff --git a/crates/server/src/config/tests.rs b/crates/server/src/config/tests.rs index 28dff7a..de5572b 100644 --- a/crates/server/src/config/tests.rs +++ b/crates/server/src/config/tests.rs @@ -65,62 +65,62 @@ fn load_should_parse_server_config() -> Result<()> { }, forge: HashMap::from([( "default".to_string(), - ForgeConfig { - forge_type: ForgeType::MockForge, - hostname: "git.example.net".to_string(), - user: "Bob".to_string(), - token: "API-Token".to_string(), - repos: BTreeMap::from([ + ForgeConfig::new( + ForgeType::MockForge, + "git.example.net".to_string(), + "Bob".to_string(), + "API-Token".to_string(), + BTreeMap::from([ ( "hello".to_string(), - ServerRepoConfig { - repo: "user/hello".to_string(), - branch: "main".to_string(), - gitdir: Some("/opt/git/user/hello.git".into()), - main: None, - next: None, - dev: None, - }, + ServerRepoConfig::new( + "user/hello".to_string(), + "main".to_string(), + Some("/opt/git/user/hello.git".into()), + None, + None, + None, + ), ), ( "world".to_string(), - ServerRepoConfig { - repo: "user/world".to_string(), - branch: "master".to_string(), - gitdir: None, - main: Some("main".to_string()), - next: Some("next".to_string()), - dev: Some("dev".to_string()), - }, + ServerRepoConfig::new( + "user/world".to_string(), + "master".to_string(), + None, + Some("main".to_string()), + Some("next".to_string()), + Some("dev".to_string()), + ), ), ( "sam".to_string(), - ServerRepoConfig { - repo: "user/sam".to_string(), - branch: "main".to_string(), - gitdir: None, - main: Some("master".to_string()), - next: Some("upcoming".to_string()), - dev: Some("sam-dev".to_string()), - }, + ServerRepoConfig::new( + "user/sam".to_string(), + "main".to_string(), + None, + Some("master".to_string()), + Some("upcoming".to_string()), + Some("sam-dev".to_string()), + ), ), ]), - }, + ), )]), }; assert_eq!(config, expected, "ServerConfig"); if let Some(forge) = config.forge.get("world") { - if let Some(repo) = forge.repos.get("sam") { + if let Some(repo) = forge.get_repo("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(), - }, - source: RepoConfigSource::Server, - }); + let expected = Some(RepoConfig::new( + RepoBranches::new( + "master".to_string(), + "upcoming".to_string(), + "sam-dev".to_string(), + ), + RepoConfigSource::Server, + )); assert_eq!(repo_config, expected, "RepoConfig"); } } @@ -141,14 +141,10 @@ fn test_repo_config_load() -> Result<()> { assert_eq!( config, - RepoConfig { - branches: RepoBranches { - main: "main".to_string(), - next: "next".to_string(), - dev: "dev".to_string(), - }, - source: RepoConfigSource::Repo - } + RepoConfig::new( + RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string(),), + RepoConfigSource::Repo + ) ); Ok(()) @@ -178,8 +174,10 @@ fn repo_details_find_default_push_remote_finds_correct_remote() -> Result<()> { None, GitDir::new(root), // Server GitDir - should be ignored ); - repo_details.forge.hostname = Hostname::new("git.kemitix.net"); - repo_details.repo_path = RepoPath("kemitix/git-next".to_string()); + repo_details.forge = repo_details + .forge + .with_hostname(Hostname::new("git.kemitix.net")); + repo_details.repo_path = RepoPath::new("kemitix/git-next".to_string()); let gitdir = &repo_details.gitdir; let repository = Repository::open(gitdir)?; let found_git_remote = git::validate::find_default_remote(&repository, Direction::Push)?; @@ -204,8 +202,10 @@ fn gitdir_validate_should_pass_a_valid_git_repo() -> Result<()> { None, GitDir::new(root), // Server GitDir - should be ignored ); - repo_details.forge.hostname = Hostname::new("git.kemitix.net"); - repo_details.repo_path = RepoPath("kemitix/git-next".to_string()); + repo_details.forge = repo_details + .forge + .with_hostname(Hostname::new("git.kemitix.net")); + repo_details.repo_path = RepoPath::new("kemitix/git-next".to_string()); let gitdir = &repo_details.gitdir; let repository = Repository::open(gitdir)?; git::validate(&repository, &repo_details)?; @@ -224,8 +224,7 @@ fn gitdir_validate_should_fail_a_git_repo_with_wrong_remote() -> Result<()> { None, GitDir::new(root), // Server GitDir - should be ignored ); - repo_details.forge.hostname = Hostname::new("localhost"); - repo_details.repo_path = RepoPath("hello/world".to_string()); + repo_details.repo_path = RepoPath::new("hello/world".to_string()); let gitdir = &repo_details.gitdir; let repository = Repository::open(gitdir)?; let_assert!(Err(_) = git::validate(&repository, &repo_details)); @@ -235,7 +234,7 @@ fn gitdir_validate_should_fail_a_git_repo_with_wrong_remote() -> Result<()> { #[test] fn git_remote_to_string_is_as_expected() { - let git_remote = GitRemote::new(Hostname::new("foo"), RepoPath("bar".to_string())); + let git_remote = GitRemote::new(Hostname::new("foo"), RepoPath::new("bar".to_string())); let as_string = git_remote.to_string(); assert_eq!(as_string, "foo:bar"); diff --git a/crates/server/src/gitforge/forgejo/branch/get_all.rs b/crates/server/src/gitforge/forgejo/branch/get_all.rs index d3d88e0..9fa9028 100644 --- a/crates/server/src/gitforge/forgejo/branch/get_all.rs +++ b/crates/server/src/gitforge/forgejo/branch/get_all.rs @@ -9,10 +9,10 @@ pub async fn get_all( repo_details: &RepoDetails, net: &Network, ) -> Result, ForgeBranchError> { - let hostname = &repo_details.forge.hostname; + let hostname = &repo_details.forge.hostname(); let repo_path = &repo_details.repo_path; use secrecy::ExposeSecret; - let token = repo_details.forge.token.expose_secret(); + let token = repo_details.forge.token().expose_secret(); let url = network::NetUrl::new(format!( "https://{hostname}/api/v1/repos/{repo_path}/branches?token={token}" )); diff --git a/crates/server/src/gitforge/forgejo/branch/validate_positions.rs b/crates/server/src/gitforge/forgejo/branch/validate_positions.rs index 190465c..2a6d61d 100644 --- a/crates/server/src/gitforge/forgejo/branch/validate_positions.rs +++ b/crates/server/src/gitforge/forgejo/branch/validate_positions.rs @@ -190,7 +190,7 @@ async fn get_commit_history( find_commits: Vec, net: &kxio::network::Network, ) -> Result, network::NetworkError> { - let hostname = &repo_details.forge.hostname; + let hostname = &repo_details.forge.hostname(); let repo_path = &repo_details.repo_path; let mut page = 1; @@ -201,7 +201,7 @@ async fn get_commit_history( let options = "stat=false&verification=false&files=false"; let mut all_commits = Vec::new(); loop { - let api_token = &repo_details.forge.token; + let api_token = &repo_details.forge.token(); use secrecy::ExposeSecret; let token = api_token.expose_secret(); let url = network::NetUrl::new(format!( diff --git a/crates/server/src/gitforge/forgejo/file/mod.rs b/crates/server/src/gitforge/forgejo/file/mod.rs index 94beaae..874dbf5 100644 --- a/crates/server/src/gitforge/forgejo/file/mod.rs +++ b/crates/server/src/gitforge/forgejo/file/mod.rs @@ -11,9 +11,9 @@ pub(super) async fn contents_get( branch: &BranchName, file_path: &str, ) -> Result { - let hostname = &repo_details.forge.hostname; + let hostname = &repo_details.forge.hostname(); let repo_path = &repo_details.repo_path; - let api_token = &repo_details.forge.token; + let api_token = &repo_details.forge.token(); use secrecy::ExposeSecret; let token = api_token.expose_secret(); let url = network::NetUrl::new(format!( diff --git a/crates/server/src/gitforge/forgejo/mod.rs b/crates/server/src/gitforge/forgejo/mod.rs index eb3765c..bfb821f 100644 --- a/crates/server/src/gitforge/forgejo/mod.rs +++ b/crates/server/src/gitforge/forgejo/mod.rs @@ -93,9 +93,9 @@ impl super::ForgeLike for ForgeJoEnv { async fn commit_status(&self, commit: &git::Commit) -> gitforge::CommitStatus { let repo_details = &self.repo_details; - let hostname = &repo_details.forge.hostname; + let hostname = &repo_details.forge.hostname(); let repo_path = &repo_details.repo_path; - let api_token = &repo_details.forge.token; + let api_token = &repo_details.forge.token(); use secrecy::ExposeSecret; let token = api_token.expose_secret(); let url = network::NetUrl::new(format!( diff --git a/crates/server/src/gitforge/tests/common.rs b/crates/server/src/gitforge/tests/common.rs index 2385846..8354708 100644 --- a/crates/server/src/gitforge/tests/common.rs +++ b/crates/server/src/gitforge/tests/common.rs @@ -6,21 +6,21 @@ use git_next_config::{ use git_next_git::{Generation, RepoDetails}; pub fn forge_details(n: u32, forge_type: ForgeType) -> ForgeDetails { - ForgeDetails { - forge_name: forge_name(n), + ForgeDetails::new( + forge_name(n), forge_type, - hostname: hostname(n), - user: user(n), - token: api_token(n), - } + hostname(n), + user(n), + api_token(n), + ) } pub fn api_token(n: u32) -> ApiToken { - ApiToken::from(format!("api-{}", n)) + ApiToken::new(format!("api-{}", n).into()) } pub fn user(n: u32) -> User { - User(format!("user-{}", n)) + User::new(format!("user-{}", n)) } pub fn hostname(n: u32) -> Hostname { @@ -28,7 +28,7 @@ pub fn hostname(n: u32) -> Hostname { } pub fn forge_name(n: u32) -> ForgeName { - ForgeName(format!("forge-name-{}", n)) + ForgeName::new(format!("forge-name-{}", n)) } pub fn repo_details( n: u32, @@ -53,7 +53,7 @@ pub fn branch_name(n: u32) -> BranchName { } pub fn repo_path(n: u32) -> RepoPath { - RepoPath(format!("repo-path-{}", n)) + RepoPath::new(format!("repo-path-{}", n)) } pub fn repo_alias(n: u32) -> RepoAlias { @@ -61,12 +61,8 @@ pub fn repo_alias(n: u32) -> RepoAlias { } pub fn repo_config(n: u32, source: RepoConfigSource) -> RepoConfig { - RepoConfig { - branches: RepoBranches { - main: format!("main-{n}"), - next: format!("next-{n}"), - dev: format!("dev-{n}"), - }, + RepoConfig::new( + RepoBranches::new(format!("main-{n}"), format!("next-{n}"), format!("dev-{n}")), source, - } + ) }