summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-16 11:19:53 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-16 11:19:53 +0100
commit6350610ef5f7d73680853d39898094f2bf15febb (patch)
treeccfc5c2c26c56a496d0f34b3f4db0965c713e7bb
parent4048dd774c817462c0a692f0f94d979290e725ee (diff)
feat: make regex of url to check if it is valid
Currently it works only with GitHub
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/helpers.rs17
-rw-r--r--src/repository/models.rs16
4 files changed, 33 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
index e579fb2..ad4fcfa 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -913,6 +913,7 @@ dependencies = [
"config",
"deadpool-postgres",
"dotenv",
+ "regex",
"serde 1.0.124",
"slog",
"slog-async",
diff --git a/Cargo.toml b/Cargo.toml
index fa12bb6..ae7576f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,3 +24,4 @@ config = "0.10.1"
serde = { version = "1.0.104", features = ["derive"] }
chrono = { version = "0.4.19", features = ["serde"] }
uuid = { version = "0.8.2", features = ["serde", "v4"] }
+regex = "1"
diff --git a/src/helpers.rs b/src/helpers.rs
index d915a50..2dbacfd 100644
--- a/src/helpers.rs
+++ b/src/helpers.rs
@@ -1,3 +1,4 @@
+use regex::Regex;
use uuid::Uuid;
/// Returns a valid Uuid if `id` is not a valid Uuid
@@ -7,3 +8,19 @@ pub fn uuid_from_string(id: &String) -> Uuid {
Err(_) => Uuid::parse_str("00000000000000000000000000000000").unwrap(),
};
}
+
+/// Check if a path is into the "valid git repositories" and returns the name
+pub fn name_of_git_repository(url: &String) -> Option<String> {
+ const GITHUB_RE: &str = r"^(http(s)?://)?(www.)?github.com/(?P<username>[a-zA-Z0-9-]+)/(?P<repository>[a-zA-Z0-9-]+)";
+ let re = Regex::new(GITHUB_RE).unwrap();
+
+ if !re.is_match(&url) {
+ return None;
+ }
+
+ let captures = re.captures(&url).unwrap();
+ let name = captures.name("username").unwrap().as_str();
+ let repo = captures.name("repository").unwrap().as_str();
+
+ Some(format!("{}/{}", name, repo))
+}
diff --git a/src/repository/models.rs b/src/repository/models.rs
index 1fd1649..1cbf3bb 100644
--- a/src/repository/models.rs
+++ b/src/repository/models.rs
@@ -1,5 +1,6 @@
use crate::db::get_client;
use crate::errors::{AppError, AppErrorType};
+use crate::helpers::name_of_git_repository;
use chrono::NaiveDateTime;
use deadpool_postgres::{Client, Pool};
@@ -133,9 +134,20 @@ impl Repository {
) -> Result<Repository, AppError> {
let client = get_client(pool.clone()).await.unwrap();
+ let repo_name: String = match name_of_git_repository(&data.url) {
+ Some(path) => path,
+ None => {
+ return Err(AppError {
+ message: Some("Repository not found".to_string()),
+ cause: Some("".to_string()),
+ error_type: AppErrorType::NotFoundError,
+ });
+ }
+ };
+
// Search a repository that matches with that url, because if it's
// exists, the server do not create a clone
- let repo_search = Repository::search(&client, data.url.clone()).await;
+ let repo_search = Repository::search(&client, repo_name.clone()).await;
match repo_search {
Ok(_) => {
return Err(AppError {
@@ -168,7 +180,7 @@ impl Repository {
};
let repo = client
- .query(&statement, &[&uuid, &data.url, &user_ip])
+ .query(&statement, &[&uuid, &repo_name, &user_ip])
.await?
.iter()
.map(|row| Repository::from_row_ref(row).unwrap())