summaryrefslogtreecommitdiff
path: root/src/repository/models.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-18 12:19:25 +0100
committerGitHub <noreply@github.com>2021-03-18 12:19:25 +0100
commit77715d93b2112b7d231db541ecce6ddca5c762c7 (patch)
treec3718080abcbe0a6cc52735bcc6726952c6494a8 /src/repository/models.rs
parentb8e22e372cb947771aa122767a4405afa208226e (diff)
parent7bdd6d8f2d18022c34986a6eea4620d8eb6dcb3a (diff)
Merge pull request #18 from gico-net/feat/git2
Create commits from a new Git Repository
Diffstat (limited to 'src/repository/models.rs')
-rw-r--r--src/repository/models.rs46
1 files changed, 45 insertions, 1 deletions
diff --git a/src/repository/models.rs b/src/repository/models.rs
index e3d8456..de82d45 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 {
+ return Err(e);
+ }
+
+ Ok(repo)
+ }
None => Err(AppError {
message: Some("Error creating a new repository".to_string()),
cause: Some("Unknown error".to_string()),