Skip to content

Commit

Permalink
Merge branch 'release/v0.1.3' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
kilpkonn committed Mar 24, 2021
2 parents 4066bb6 + d5557a9 commit 0627d13
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 32 deletions.
31 changes: 12 additions & 19 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ lazy_static! {

#[derive(Serialize, Deserialize)]
pub struct Config {
target_host: String,
target_port: Option<u16>,
target_prefix: Option<String>,
pub port: Option<u16>,
pub address: Option<String>,
target_base_url: String,
pub sync_base_url: Option<String>,
pub prefix: Option<String>,
pub access_token: Option<String>,
pub ssh_public_key: Option<String>,
Expand All @@ -40,30 +37,26 @@ 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 {
let (provider, user, repo) = generate_credentials_from_clone_url(url);
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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Repository {
pub ssh_passphrase: Option<String>,
}

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()),
Expand Down
15 changes: 10 additions & 5 deletions src/repo/repo_manager.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -22,6 +22,7 @@ pub struct AddRepoDto {
pub provider: Option<String>,
pub user: Option<String>,
pub repo: Option<String>,
pub sync_url: Option<String>,
pub message: Option<String>,
}

Expand All @@ -30,8 +31,6 @@ pub struct RepoDto {
pub provider: String,
pub user: String,
pub repo: String,
pub sync_url: String,
pub access_token: Option<String>,
pub commits: Vec<Commit>,
}

Expand Down Expand Up @@ -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 {
Expand All @@ -96,11 +93,18 @@ 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 = 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),
user: Option::from(user),
repo: Option::from(repository),
sync_url: Option::from(sync_url),
message: None,
};
}
Expand All @@ -111,6 +115,7 @@ pub async fn add_repo(repo_dto: AddRepositoryDto) -> AddRepoDto {
provider: None,
user: None,
repo: None,
sync_url: None,
message: Option::from(error_msg),
};
}
20 changes: 20 additions & 0 deletions src/server/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/<provider>/<user>/<repo>")]
pub fn repo(provider: String, user: String, repo: String) -> JsonValue {
Expand Down Expand Up @@ -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="<dto>")]
pub fn sync_repo_github(dto: Json<GithubPushWebhook>) -> 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="<dto>")]
pub fn sync_repo_gitlab(dto: Json<GitlabPushWebhook>) -> 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!({})
}
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod server;

mod resource;
mod controller;
21 changes: 21 additions & 0 deletions src/server/resource.rs
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
5 changes: 1 addition & 4 deletions src/sync/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct SyncSingleResult {

#[derive(Deserialize)]
pub struct LastSyncResponse {
hash: String,
// hash: String,
timestamp: i64,
tracked_commit_hashes: Vec<String>,
}
Expand Down Expand Up @@ -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![],
});
Expand All @@ -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 {
Expand Down

0 comments on commit 0627d13

Please sign in to comment.