diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-18 19:29:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-18 19:29:16 +0100 |
commit | 526688a953b3ec7992218c195449c05a071242ff (patch) | |
tree | 6a5704e454616166604119bf84fe69ca035cc3b4 /src/repository | |
parent | 77715d93b2112b7d231db541ecce6ddca5c762c7 (diff) | |
parent | 4e266a2c2a22385a12ea16b992d345e9856f012f (diff) |
Merge pull request #19 from gico-net/feat/repo-with-branch
Create branch when the repository is created
Diffstat (limited to 'src/repository')
-rw-r--r-- | src/repository/models.rs | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/repository/models.rs b/src/repository/models.rs index de82d45..16cbb98 100644 --- a/src/repository/models.rs +++ b/src/repository/models.rs @@ -1,3 +1,4 @@ +use crate::branch::models::{Branch, BranchData}; use crate::commit::models::Commit; use crate::db::get_client; use crate::email::models::{Email, EmailData}; @@ -30,6 +31,7 @@ pub struct Repository { #[derive(Serialize, Deserialize)] pub struct RepositoryData { pub url: String, + pub branch: String, } impl Repository { @@ -190,9 +192,13 @@ impl Repository { match repo { Some(repo) => { - let commits = match git::repo_commits(&repo_name) { + let commits = match git::repo_commits(&repo_name, &data.branch) + { Ok(c) => c, Err(e) => { + // It also need to remove the repository from the db + let _ = + Repository::delete(pool.clone(), &repo.id).await; return Err(AppError { message: Some( format!( @@ -203,7 +209,7 @@ impl Repository { ), cause: Some("Repository clone".to_string()), error_type: AppErrorType::GitError, - }) + }); } }; @@ -224,9 +230,21 @@ impl Repository { let commits_result = Commit::create(pool.clone(), commits).await; - if let Err(e) = commits_result { - return Err(e); - } + + match commits_result { + Ok(commits_res_vec) => { + let branch_data = BranchData { + name: data.branch.clone(), + repository_id: repo.id, + head: commits_res_vec[0].hash.clone(), + }; + let _ = + Branch::create(pool.clone(), &branch_data).await; + } + Err(e) => { + return Err(e); + } + }; Ok(repo) } |