From b79e10683ee6ef53680638382241a8b240bd50f9 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 18 Mar 2021 19:23:41 +0100 Subject: fix: 'insert into branch' --- src/branch/models.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/branch/models.rs b/src/branch/models.rs index c8a8d65..3d57537 100644 --- a/src/branch/models.rs +++ b/src/branch/models.rs @@ -119,7 +119,7 @@ impl Branch { let statement = client .prepare( - "INSERT INTO repository(id, name, repository_id, head) + "INSERT INTO branch(id, name, repository_id, head) VALUES($1, $2, $3, $4) RETURNING *", ) -- cgit v1.2.3-18-g5258 From 893435cca8093e4713e077785139989debe0bb1b Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 18 Mar 2021 19:24:33 +0100 Subject: fix: security with single quote on queries This error was raised because it does not use the `prepare` method --- src/commit/models.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/commit/models.rs b/src/commit/models.rs index 2f1536b..7f6a9fc 100644 --- a/src/commit/models.rs +++ b/src/commit/models.rs @@ -108,15 +108,15 @@ impl Commit { None => "NULL".to_string(), }; raw_query += &format!( - "('{}', {}, '{}', '{}', '{}', '{}', '{}', '{}', '{}'),", + "('{}', {}, E'{}', '{}', '{}', E'{}', '{}', E'{}', '{}'),", commit.hash, tree, - commit.text, + commit.text.replace("'", "\\'"), commit.date, commit.author_email, - commit.author_name, + commit.author_name.replace("'", "\\'"), commit.committer_email, - commit.committer_name, + commit.committer_name.replace("'", "\\'"), commit.repository_url )[..] } -- cgit v1.2.3-18-g5258 From 07475371456740c793d65efe829124eb4bad4780 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 18 Mar 2021 19:26:53 +0100 Subject: chore: remove the temporary directory before --- src/git.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/git.rs b/src/git.rs index 76f9a4e..479b98c 100644 --- a/src/git.rs +++ b/src/git.rs @@ -103,7 +103,13 @@ fn get_commit(gcommit: &git2::Commit, repo_name: &String) -> Commit { /// Then, open the repository. /// Then, get commits /// Finally, remove the temporary folder -pub fn repo_commits(repo_name: &String) -> Result, Error> { +pub fn repo_commits( + repo_name: &String, + branch: &String, +) -> Result, Error> { + // Remove a possible already cloned repository + let _ = remove_dir_all(get_tmp_dir(&repo_name)); + // Try to clone the repo. If it returns an error, it's useless to go ahead: // raises an error. let repo = match clone_repo(&repo_name) { @@ -130,13 +136,5 @@ pub fn repo_commits(repo_name: &String) -> Result, Error> { commits.push(get_commit(&hash, &repo_name)); } - if let Err(_) = remove_dir_all(get_tmp_dir(&repo_name)) { - return Err(git2::Error::new( - git2::ErrorCode::GenericError, - git2::ErrorClass::Os, - "Temporary clone not deleted", - )); - } - Ok(commits) } -- cgit v1.2.3-18-g5258 From 88a18cbe2a6150b2a611b943fc5a5017b3196aa0 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 18 Mar 2021 19:27:18 +0100 Subject: fix: returns the added commits --- src/commit/models.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/commit/models.rs b/src/commit/models.rs index 7f6a9fc..b2aa2d2 100644 --- a/src/commit/models.rs +++ b/src/commit/models.rs @@ -123,6 +123,7 @@ impl Commit { // Remove the last `,` let _ = raw_query.pop(); + raw_query += " RETURNING *"; // TODO: write query with &commits and parameter. Need to implement // ToSql trait for `Commit` model -- cgit v1.2.3-18-g5258 From 4e266a2c2a22385a12ea16b992d345e9856f012f Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 18 Mar 2021 19:27:51 +0100 Subject: feat: add branch connected to head of repository --- src/git.rs | 2 +- src/repository/models.rs | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/git.rs b/src/git.rs index 479b98c..2a8749c 100644 --- a/src/git.rs +++ b/src/git.rs @@ -119,7 +119,7 @@ pub fn repo_commits( } }; - if let Err(e) = get_branch(&repo, "main") { + if let Err(e) = get_branch(&repo, branch) { return Err(e); } 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) } -- cgit v1.2.3-18-g5258