diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-18 12:14:32 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-03-18 12:14:32 +0100 |
commit | 214137b65413daff684cfe8df4d544ea0cb78516 (patch) | |
tree | 66ece5a4c42e17cc0eb7ebc5aa229e0dd9e00fb7 /src/repository | |
parent | 5f4f01791d26003889afbb7179633ca98d5647e1 (diff) |
feat: add Commit(s) when repository is created
Diffstat (limited to 'src/repository')
-rw-r--r-- | src/repository/models.rs | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/src/repository/models.rs b/src/repository/models.rs index e3d8456..5b7b445 100644 --- a/src/repository/models.rs +++ b/src/repository/models.rs @@ -1,5 +1,8 @@ +use crate::commit::models::Commit; use crate::db::get_client; +use crate::email::models::{Email, EmailData}; use crate::errors::{AppError, AppErrorType}; +use crate::git; use crate::helpers::name_of_git_repository; use chrono::NaiveDateTime; @@ -9,6 +12,7 @@ use tokio_pg_mapper::FromTokioPostgresRow; use tokio_pg_mapper_derive::PostgresMapper; use uuid::Uuid; +use std::collections::HashSet; use std::net::SocketAddr; #[derive(Serialize, Deserialize, PostgresMapper)] @@ -185,7 +189,47 @@ impl Repository { .map(|row| Repository::from_row_ref(&row).unwrap()); match repo { - Some(repo) => Ok(repo), + Some(repo) => { + let commits = match git::repo_commits(&repo_name) { + Ok(c) => c, + Err(e) => { + return Err(AppError { + message: Some( + format!( + "Repository couldn't be created now: {:?}", + e + ) + .to_string(), + ), + cause: Some("Repository clone".to_string()), + error_type: AppErrorType::GitError, + }) + } + }; + + let mut emails: HashSet<String> = HashSet::new(); + for commit in &commits { + emails.insert(commit.author_email.clone()); + emails.insert(commit.committer_email.clone()); + } + for email in emails { + if let Err(e) = + Email::create(pool.clone(), &EmailData { email }).await + { + if e.error_type == AppErrorType::DbError { + return Err(e); + } + } + } + + let commits_result = + Commit::create(pool.clone(), commits).await; + if let Err(e) = commits_result { + panic!("{}", e); + } + + Ok(repo) + } None => Err(AppError { message: Some("Error creating a new repository".to_string()), cause: Some("Unknown error".to_string()), |