From eda7ce792c9875a437314924f90d66a37d81a1bb Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 17 Mar 2021 09:14:58 +0100 Subject: fix: response status for repo creation --- src/repository/routes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/repository/routes.rs b/src/repository/routes.rs index abf77b6..32443b1 100644 --- a/src/repository/routes.rs +++ b/src/repository/routes.rs @@ -93,7 +93,7 @@ async fn create_repo( .await; result - .map(|repo| HttpResponse::Ok().json(repo)) + .map(|repo| HttpResponse::Created().json(repo)) .map_err(|e| e) } -- cgit v1.2.3-18-g5258 From bc9a0de90f0463b59e69e6bb2f2e75f3c29062e5 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 17 Mar 2021 09:15:15 +0100 Subject: feat: add commit models and get all of them --- src/commit/mod.rs | 2 ++ src/commit/models.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/commit/routes.rs | 28 ++++++++++++++++++++++++++++ src/main.rs | 2 ++ 4 files changed, 74 insertions(+) create mode 100644 src/commit/mod.rs create mode 100644 src/commit/models.rs create mode 100644 src/commit/routes.rs diff --git a/src/commit/mod.rs b/src/commit/mod.rs new file mode 100644 index 0000000..a0e1883 --- /dev/null +++ b/src/commit/mod.rs @@ -0,0 +1,2 @@ +pub mod models; +pub mod routes; diff --git a/src/commit/models.rs b/src/commit/models.rs new file mode 100644 index 0000000..215ab57 --- /dev/null +++ b/src/commit/models.rs @@ -0,0 +1,42 @@ +use crate::db::get_client; +use crate::errors::AppError; + +use chrono::NaiveDateTime; +use deadpool_postgres::Pool; +use serde::{Deserialize, Serialize}; +use tokio_pg_mapper::FromTokioPostgresRow; +use tokio_pg_mapper_derive::PostgresMapper; + +#[derive(Serialize, Deserialize, PostgresMapper)] +#[pg_mapper(table = "commit")] +/// Commit model +pub struct Commit { + pub hash: String, + pub tree: String, + pub text: String, + pub date: NaiveDateTime, + pub author_email: String, // Reference to Email + pub author_name: String, + pub committer_email: String, // Reference to Email + pub committer_name: String, + pub repository_url: String, // Reference to Repository +} + +impl Commit { + /// Find all commits. Order them by descrescent `date` field + pub async fn find_all(pool: Pool) -> Result, AppError> { + let client = get_client(pool.clone()).await.unwrap(); + let statement = client + .prepare("SELECT * FROM commit ORDER BY date DESC") + .await?; + + let commits = client + .query(&statement, &[]) + .await? + .iter() + .map(|row| Commit::from_row_ref(row).unwrap()) + .collect::>(); + + Ok(commits) + } +} diff --git a/src/commit/routes.rs b/src/commit/routes.rs new file mode 100644 index 0000000..bf04108 --- /dev/null +++ b/src/commit/routes.rs @@ -0,0 +1,28 @@ +use crate::commit::models::Commit; +use crate::config::AppState; +use crate::errors::AppErrorResponse; + +use actix_web::{web, HttpResponse, Responder}; +use slog::info; + +/// Endpoint used for getting all commits +async fn index(state: web::Data) -> impl Responder { + info!(state.log, "GET /commit/"); + let result = Commit::find_all(state.pool.clone()).await; + + match result { + Ok(commits) => HttpResponse::Ok().json(commits), + _ => HttpResponse::BadRequest().json(AppErrorResponse { + detail: "Error trying to read all commits from database" + .to_string(), + }), + } +} + +/// Routes for commits +pub fn config(cfg: &mut web::ServiceConfig) { + cfg.service( + web::scope("/commit") + .service(web::resource("{_:/?}").route(web::get().to(index))), + ); +} diff --git a/src/main.rs b/src/main.rs index 63f48fa..b2e0ba3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod db; mod errors; mod helpers; +mod commit; mod email; mod repository; @@ -37,6 +38,7 @@ async fn main() -> std::io::Result<()> { .wrap(middleware::Logger::default()) .configure(repository::routes::config) .configure(email::routes::config) + .configure(commit::routes::config) }) .bind(format!("{}:{}", config.server.host, config.server.port))? .run() -- cgit v1.2.3-18-g5258 From f18450a460da8897aaf1ca95553653497f7e7d70 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 17 Mar 2021 09:57:00 +0100 Subject: fix: error when 'tree' is null --- src/commit/models.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commit/models.rs b/src/commit/models.rs index 215ab57..068c550 100644 --- a/src/commit/models.rs +++ b/src/commit/models.rs @@ -12,7 +12,7 @@ use tokio_pg_mapper_derive::PostgresMapper; /// Commit model pub struct Commit { pub hash: String, - pub tree: String, + pub tree: Option, pub text: String, pub date: NaiveDateTime, pub author_email: String, // Reference to Email -- cgit v1.2.3-18-g5258 From 0ed019b99d7603f19afbef8e676b342e3c868e76 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 17 Mar 2021 09:57:48 +0100 Subject: chore: use timestamptz for commit date --- Cargo.toml | 2 +- src/commit/models.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8502fcb..42b2318 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ slog-async = "2.4.0" dotenv = "0.15.0" config = "0.10.1" serde = { version = "1.0.104", features = ["derive"] } -chrono = { version = "0.4.19", features = ["serde"] } +chrono = { version = "0.4", features = ["serde"] } uuid = { version = "0.8.2", features = ["serde", "v4"] } regex = "1" md-5 = "0.9.1" diff --git a/src/commit/models.rs b/src/commit/models.rs index 068c550..6246511 100644 --- a/src/commit/models.rs +++ b/src/commit/models.rs @@ -1,7 +1,7 @@ use crate::db::get_client; use crate::errors::AppError; -use chrono::NaiveDateTime; +use chrono::{DateTime, Local}; use deadpool_postgres::Pool; use serde::{Deserialize, Serialize}; use tokio_pg_mapper::FromTokioPostgresRow; @@ -14,7 +14,7 @@ pub struct Commit { pub hash: String, pub tree: Option, pub text: String, - pub date: NaiveDateTime, + pub date: DateTime, pub author_email: String, // Reference to Email pub author_name: String, pub committer_email: String, // Reference to Email -- cgit v1.2.3-18-g5258 From 07b052797e273d4a85f2316e7ef9ca8677595dd1 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 17 Mar 2021 09:57:56 +0100 Subject: feat: get commit by hash --- src/commit/models.rs | 24 +++++++++++++++++++++++- src/commit/routes.rs | 20 +++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/commit/models.rs b/src/commit/models.rs index 6246511..c70ecf2 100644 --- a/src/commit/models.rs +++ b/src/commit/models.rs @@ -1,5 +1,5 @@ use crate::db::get_client; -use crate::errors::AppError; +use crate::errors::{AppError, AppErrorType}; use chrono::{DateTime, Local}; use deadpool_postgres::Pool; @@ -39,4 +39,26 @@ impl Commit { Ok(commits) } + + // Find a commit that it has an hash equals to `hash` + pub async fn find(pool: Pool, hash: String) -> Result { + let client = get_client(pool.clone()).await.unwrap(); + let statement = client + .prepare("SELECT * FROM commit WHERE hash = $1") + .await?; + + let commit = client + .query_opt(&statement, &[&hash]) + .await? + .map(|row| Commit::from_row_ref(&row).unwrap()); + + match commit { + Some(commit) => Ok(commit), + None => Err(AppError { + error_type: AppErrorType::NotFoundError, + cause: None, + message: Some("Commit not found".to_string()), + }), + } + } } diff --git a/src/commit/routes.rs b/src/commit/routes.rs index bf04108..1b73c4f 100644 --- a/src/commit/routes.rs +++ b/src/commit/routes.rs @@ -19,10 +19,28 @@ async fn index(state: web::Data) -> impl Responder { } } +// Endpoint used for getting one commit +async fn get_commit( + state: web::Data, + hash: web::Path<(String,)>, +) -> impl Responder { + info!(state.log, "GET /commit/{}/", &hash.0); + + let result = Commit::find(state.pool.clone(), hash.0.clone()).await; + + result + .map(|commit| HttpResponse::Ok().json(commit)) + .map_err(|e| e) +} + /// Routes for commits pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( web::scope("/commit") - .service(web::resource("{_:/?}").route(web::get().to(index))), + .service(web::resource("{_:/?}").route(web::get().to(index))) + .service( + web::resource("/{hash}{_:/?}") + .route(web::get().to(get_commit)), + ), ); } -- cgit v1.2.3-18-g5258 From c3522a8d3b0236b3c9358db4160182cc211f8069 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 17 Mar 2021 14:14:36 +0100 Subject: fix(schema): add on delete referenceses --- schema.sql | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/schema.sql b/schema.sql index 9daa7f9..ef066d7 100644 --- a/schema.sql +++ b/schema.sql @@ -1,9 +1,9 @@ CREATE TABLE "repository" ( id uuid PRIMARY KEY NOT NULL, url varchar(255) UNIQUE NOT NULL, - created_at timestamp NOT NULL, - updated_at timestamp NOT NULL, - uploader_ip varchar(15) NOT NULL + created_at timestamp NOT NULL DEFAULT NOW(), + updated_at timestamp NOT NULL DEFAULT NOW(), + uploader_ip varchar(21) NOT NULL ); CREATE TABLE "email"( @@ -13,19 +13,19 @@ CREATE TABLE "email"( CREATE TABLE "commit" ( hash varchar(40) PRIMARY KEY NOT NULL, - tree varchar(40) REFERENCES commit(hash) NULL, + tree varchar(40) REFERENCES commit(hash) ON DELETE CASCADE NULL, text text NOT NULL, - date timestamp NOT NULL, - author_email varchar(120) REFERENCES email(email) NOT NULL, + date timestamptz NOT NULL, + author_email varchar(120) REFERENCES email(email) ON DELETE NO ACTION NOT NULL, author_name varchar(120) NOT NULL, - committer_email varchar(120) REFERENCES email(email) NOT NULL, + committer_email varchar(120) REFERENCES email(email) ON DELETE NO ACTION NOT NULL, committer_name varchar(120) NOT NULL, - repository_url varchar(256) REFERENCES repository(url) NOT NULL + repository_url varchar(256) REFERENCES repository(url) ON DELETE CASCADE NOT NULL ); CREATE TABLE "branch" ( id uuid PRIMARY KEY NOT NULL, name varchar(120) NOT NULL, - repository_id uuid REFERENCES repository(id) NOT NULL, - head varchar(40) REFERENCES commit(hash) NULL + repository_id uuid REFERENCES repository(id) ON DELETE CASCADE NOT NULL, + head varchar(40) REFERENCES commit(hash) ON DELETE SET NULL NULL ); -- cgit v1.2.3-18-g5258 From 904c7e40a2d092eea87ca6d9ea80edce8a2aad2a Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 17 Mar 2021 14:14:46 +0100 Subject: feat: add delete commit endpoint --- src/commit/models.rs | 32 ++++++++++++++++++++++++++++++++ src/commit/routes.rs | 46 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/commit/models.rs b/src/commit/models.rs index c70ecf2..8b81a15 100644 --- a/src/commit/models.rs +++ b/src/commit/models.rs @@ -61,4 +61,36 @@ impl Commit { }), } } + + /// Find a commit and delete it, but before check if "Authorization" + /// matches with SECRET_KEY + pub async fn delete( + pool: Pool, + hash: &String, + ) -> Result { + let client = get_client(pool.clone()).await.unwrap(); + let statement = client + .prepare( + " + DELETE FROM commit + WHERE hash=$1 + RETURNING * + ", + ) + .await?; + + let commit = client + .query_opt(&statement, &[&hash]) + .await? + .map(|row| Commit::from_row_ref(&row).unwrap()); + + match commit { + Some(commit) => Ok(commit), + None => Err(AppError { + error_type: AppErrorType::NotFoundError, + cause: None, + message: Some("Commit not found".to_string()), + }), + } + } } diff --git a/src/commit/routes.rs b/src/commit/routes.rs index 1b73c4f..e49f698 100644 --- a/src/commit/routes.rs +++ b/src/commit/routes.rs @@ -1,9 +1,10 @@ use crate::commit::models::Commit; use crate::config::AppState; -use crate::errors::AppErrorResponse; - -use actix_web::{web, HttpResponse, Responder}; +use crate::errors::{AppError, AppErrorResponse, AppErrorType}; +use actix_web::http::header; +use actix_web::{web, HttpRequest, HttpResponse, Responder}; use slog::info; +use std::env; /// Endpoint used for getting all commits async fn index(state: web::Data) -> impl Responder { @@ -33,6 +34,42 @@ async fn get_commit( .map_err(|e| e) } +/// Endpoint used for delete commitsitory. +/// It uses a SECRET_KEY used like an API key +async fn delete_commit( + req: HttpRequest, + state: web::Data, + hash: web::Path<(String,)>, +) -> impl Responder { + match req.headers().get(header::AUTHORIZATION) { + Some(x) + if x.to_str().unwrap() + != env::var("SECRET_KEY").unwrap_or("".to_string()) => + { + info!(state.log, "DELETE /commit/{}/ 401", &hash.0); + return Err(AppError { + error_type: AppErrorType::AuthorizationError, + message: Some( + "You must provide a valid Authorization".to_string(), + ), + cause: None, + }); + } + Some(_) => {} + None => { + info!(state.log, "DELETE /commit/{}/ 400", &hash.0); + return Ok(HttpResponse::BadRequest().body("")); + } + }; + + let result = Commit::delete(state.pool.clone(), &hash.0).await; + info!(state.log, "DELETE /commit/{}/", &hash.0); + + result + .map(|_| HttpResponse::NoContent().body("")) + .map_err(|e| e) +} + /// Routes for commits pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( @@ -40,7 +77,8 @@ pub fn config(cfg: &mut web::ServiceConfig) { .service(web::resource("{_:/?}").route(web::get().to(index))) .service( web::resource("/{hash}{_:/?}") - .route(web::get().to(get_commit)), + .route(web::get().to(get_commit)) + .route(web::delete().to(delete_commit)), ), ); } -- cgit v1.2.3-18-g5258