From c7f287e58415c81bb03defeacc255675d5bea069 Mon Sep 17 00:00:00 2001 From: tavo Date: Tue, 23 Mar 2021 09:44:50 +0200 Subject: [PATCH 1/2] More readable cfg --- src/config/config.rs | 31 ++++++++++++------------------- src/repo/repo_manager.rs | 8 ++++---- src/sync/sync.rs | 5 +---- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/config/config.rs b/src/config/config.rs index 320886c..884d033 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -12,11 +12,8 @@ lazy_static! { #[derive(Serialize, Deserialize)] pub struct Config { - target_host: String, - target_port: Option, - target_prefix: Option, - pub port: Option, - pub address: Option, + target_base_url: String, + pub sync_base_url: Option, pub prefix: Option, pub access_token: Option, pub ssh_public_key: Option, @@ -40,19 +37,14 @@ pub fn save(config_file: &String, config: &Config) { impl Config { pub fn get_target_url(&self) -> String { - return format!("{}:{}{}", - self.target_host, - self.target_port.unwrap_or(8000), - self.target_prefix.as_ref().unwrap_or(&"".to_string()) - ); + return self.target_base_url.clone(); } pub fn get_sync_url(&self) -> String { - return format!("{}:{}{}", - self.address.clone().unwrap_or("localhost".to_string()), - self.port.clone().unwrap_or(8000), - self.prefix.as_ref().unwrap_or(&"".to_string()) - ); + if self.sync_base_url.is_some() { + return self.sync_base_url.clone().unwrap(); + } + "http://localhost:8090/services/gtm/sync".to_string() } pub fn generate_path_from_git_url(&self, url: &String) -> String { @@ -60,10 +52,11 @@ impl Config { return format!("{}/{}/{}/{}", self.repositories_base_path.trim_end_matches("/"), provider, user, repo); } - pub fn generate_path_from_provider_user_repo(&self, - provider: &String, - user: &String, - repo: &String, + pub fn generate_path_from_provider_user_repo( + &self, + provider: &String, + user: &String, + repo: &String, ) -> String { return format!("{}/{}/{}/{}", self.repositories_base_path.trim_end_matches("/"), provider, user, repo); } diff --git a/src/repo/repo_manager.rs b/src/repo/repo_manager.rs index b77a239..88ab445 100644 --- a/src/repo/repo_manager.rs +++ b/src/repo/repo_manager.rs @@ -22,6 +22,7 @@ pub struct AddRepoDto { pub provider: Option, pub user: Option, pub repo: Option, + pub sync_url: Option, pub message: Option, } @@ -30,8 +31,6 @@ pub struct RepoDto { pub provider: String, pub user: String, pub repo: String, - pub sync_url: String, - pub access_token: Option, pub commits: Vec, } @@ -76,8 +75,6 @@ pub fn get_repo(provider: &String, user: &String, repo: &String) -> RepoWrapperD provider: provider.clone(), user: user.clone(), repo: repo.clone(), - sync_url: cfg.get_sync_url(), - access_token: cfg.access_token, commits }; return RepoWrapperDto { @@ -96,11 +93,13 @@ pub async fn add_repo(repo_dto: AddRepositoryDto) -> AddRepoDto { config::save(&config::CONFIG_PATH, &cfg); } sync::sync_repo(&provider, &user, &repository).await; + let sync_url = format!("{}/repositories/{}/{}/{}/sync", cfg.get_sync_url(), provider, user, repository); return AddRepoDto { success: true, provider: Option::from(provider), user: Option::from(user), repo: Option::from(repository), + sync_url: Option::from(sync_url), message: None, }; } @@ -111,6 +110,7 @@ pub async fn add_repo(repo_dto: AddRepositoryDto) -> AddRepoDto { provider: None, user: None, repo: None, + sync_url: None, message: Option::from(error_msg), }; } diff --git a/src/sync/sync.rs b/src/sync/sync.rs index a7180c4..72b02ea 100644 --- a/src/sync/sync.rs +++ b/src/sync/sync.rs @@ -21,7 +21,7 @@ pub struct SyncSingleResult { #[derive(Deserialize)] pub struct LastSyncResponse { - hash: String, + // hash: String, timestamp: i64, tracked_commit_hashes: Vec, } @@ -87,7 +87,6 @@ async fn sync_single( &cfg.access_token.clone().unwrap_or("".to_string())) .await .unwrap_or(LastSyncResponse { - hash: "".to_string(), timestamp: -1, tracked_commit_hashes: vec![], }); @@ -108,8 +107,6 @@ async fn sync_single( provider: provider.clone(), user: user.clone(), repo: repo.clone(), - sync_url: cfg.get_sync_url(), - access_token: cfg.access_token.clone(), commits, }; let dto = RepoWrapperDto { From e3ae582bc78c8b389e823f1276b216f65be0a4c6 Mon Sep 17 00:00:00 2001 From: tavo Date: Tue, 23 Mar 2021 11:26:49 +0200 Subject: [PATCH 2/2] Add more generic push hook endpoints --- src/config/repository.rs | 2 +- src/repo/repo_manager.rs | 9 +++++++-- src/server/controller.rs | 20 ++++++++++++++++++++ src/server/mod.rs | 2 +- src/server/resource.rs | 21 +++++++++++++++++++++ src/server/server.rs | 6 ++++-- 6 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/server/resource.rs diff --git a/src/config/repository.rs b/src/config/repository.rs index fb8bc11..327319f 100644 --- a/src/config/repository.rs +++ b/src/config/repository.rs @@ -17,7 +17,7 @@ pub struct Repository { pub ssh_passphrase: Option, } -pub fn generate_credentials_from_clone_url(url: &String) -> (String, String, String) { +pub fn generate_credentials_from_clone_url(url: &str) -> (String, String, String) { let caps = PATH_FROM_URL_REGEX.captures(url).unwrap(); return (caps.get(2).map_or("provider".to_string(), |m| m.as_str().to_string()), caps.get(3).map_or("user".to_string(), |m| m.as_str().to_string()), diff --git a/src/repo/repo_manager.rs b/src/repo/repo_manager.rs index 88ab445..e93d5b9 100644 --- a/src/repo/repo_manager.rs +++ b/src/repo/repo_manager.rs @@ -1,11 +1,11 @@ use serde::{Deserialize, Serialize}; use crate::config::config; -use crate::sync::sync; use crate::config::repository::generate_credentials_from_clone_url; use crate::config::repository::Repository; use crate::gtm::git; use crate::gtm::gtm::Commit; +use crate::sync::sync; #[derive(Serialize, Deserialize)] pub struct AddRepositoryDto { @@ -93,7 +93,12 @@ pub async fn add_repo(repo_dto: AddRepositoryDto) -> AddRepoDto { config::save(&config::CONFIG_PATH, &cfg); } sync::sync_repo(&provider, &user, &repository).await; - let sync_url = format!("{}/repositories/{}/{}/{}/sync", cfg.get_sync_url(), provider, user, repository); + let sync_url = if provider.contains("bitbucket") { + format!("{}/repositories/{}/{}/{}/sync", cfg.get_sync_url(), provider, user, repository) + } else { + let domain = if provider.contains("gitlab") { "gitlab" } else { "github" }; + format!("{}/hooks/{}/push", cfg.get_sync_url(), domain) + }; return AddRepoDto { success: true, provider: Option::from(provider), diff --git a/src/server/controller.rs b/src/server/controller.rs index 6e33f97..a509328 100644 --- a/src/server/controller.rs +++ b/src/server/controller.rs @@ -3,6 +3,8 @@ use rocket_contrib::json::{JsonValue, Json}; use crate::repo::repo_manager; use crate::sync::sync; use crate::repo::repo_manager::AddRepositoryDto; +use crate::server::resource::{GithubPushWebhook, GitlabPushWebhook}; +use crate::config::repository::generate_credentials_from_clone_url; #[get("/repositories///")] pub fn repo(provider: String, user: String, repo: String) -> JsonValue { @@ -35,3 +37,21 @@ pub fn sync_repo(provider: String, user: String, repo: String) -> JsonValue { pub fn post_sync_repo(provider: String, user: String, repo: String) -> JsonValue { sync_repo(provider, user, repo) } + +#[post("/hooks/github/push", data="")] +pub fn sync_repo_github(dto: Json) -> JsonValue { + let dto = dto.into_inner(); + let (provider, user, repo) = generate_credentials_from_clone_url(&dto.repository.ssh_url); + let mut rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(sync::sync_repo(&provider, &user, &repo)); + rocket_contrib::json!({}) +} + +#[post("/hooks/gitlab/push", data="")] +pub fn sync_repo_gitlab(dto: Json) -> JsonValue { + let dto = dto.into_inner(); + let (provider, user, repo) = generate_credentials_from_clone_url(&dto.repository.git_ssh_url); + let mut rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(sync::sync_repo(&provider, &user, &repo)); + rocket_contrib::json!({}) +} diff --git a/src/server/mod.rs b/src/server/mod.rs index 53ca2b0..73bb017 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,3 +1,3 @@ pub mod server; - +mod resource; mod controller; diff --git a/src/server/resource.rs b/src/server/resource.rs new file mode 100644 index 0000000..c8e6453 --- /dev/null +++ b/src/server/resource.rs @@ -0,0 +1,21 @@ +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct GithubRepository { + pub ssh_url: String +} + +#[derive(Deserialize)] +pub struct GithubPushWebhook { + pub repository: GithubRepository +} + +#[derive(Deserialize)] +pub struct GitlabRepository { + pub git_ssh_url: String +} + +#[derive(Deserialize)] +pub struct GitlabPushWebhook { + pub repository: GitlabRepository +} diff --git a/src/server/server.rs b/src/server/server.rs index 8fcf5b9..753831a 100644 --- a/src/server/server.rs +++ b/src/server/server.rs @@ -3,12 +3,14 @@ use crate::server::controller; pub fn run() { rocket::ignite() - .mount("/services/gtm/sync//", + .mount("/services/gtm/sync/", routes![ controller::repo, controller::add_repo, controller::sync_repo, controller::post_sync_repo, - controller::sync_all], + controller::sync_all, + controller::sync_repo_github, + controller::sync_repo_gitlab], ).launch(); } \ No newline at end of file