From ee1f15bacb810b761b3b11f3e1a19a1b22e08548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=98=89=E8=B1=AA?= Date: Fri, 19 Jan 2024 12:39:51 +0800 Subject: [PATCH 1/8] Impl playbook create method --- Cargo.toml | 4 ++-- src/requests/playbook.rs | 4 +--- src/services/playbook.rs | 30 ++++++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f4aa28d..8afe453 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ name = "playground" path = "src/lib.rs" [dependencies] -amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.7.2" } -amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.2" } +amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.7.3" } +amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.3" } anyhow = "1.0" axum = { version = "0.7.4" } clap = { version = "4.4.12", features = ["derive", "env"] } diff --git a/src/requests/playbook.rs b/src/requests/playbook.rs index a9e5e5b..4ec382e 100644 --- a/src/requests/playbook.rs +++ b/src/requests/playbook.rs @@ -18,7 +18,5 @@ use utoipa::ToSchema; #[derive(Debug, Serialize, Deserialize, ToSchema)] pub struct CreatePlaybookRequest { - pub title: String, - pub description: Option, - pub preface: Preface, + pub repo: String, } diff --git a/src/services/playbook.rs b/src/services/playbook.rs index ff99552..dbbf834 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -13,8 +13,9 @@ // limitations under the License. use amp_client::playbooks::PlaybookPayload; -use amp_common::resource::PlaybookSpec; +use amp_common::resource::{PlaybookSpec, Preface}; use std::sync::Arc; +use amp_common::schema::GitReference; use tracing::{error, info}; use uuid::Uuid; @@ -23,19 +24,40 @@ use crate::context::Context; use crate::errors::ApiError; use crate::errors::Result; use crate::requests::playbook::CreatePlaybookRequest; +use crate::utils::repo; pub struct PlaybookService; impl PlaybookService { pub async fn create(ctx: Arc, req: &CreatePlaybookRequest) -> Result { + let mut title = String::new(); + let mut description = String::new(); + + let repo = repo(req.repo.as_str()).unwrap_or_default(); + let repository = ctx.github_client.repositories().find(repo.as_str()).ok().unwrap_or_default(); + match repository { + Some(repository) => { + title = repo; + description = repository.description; + } + None => { + info!("Not found github repositories in {}...", repo); + } + } + let mut preface = Preface::default(); + let mut reference = GitReference::default(); + reference.repo = req.repo.clone(); + preface.name = req.repo.clone(); + preface.repository = Some(reference); let payload = PlaybookPayload { - title: req.title.clone(), - description: req.description.clone().unwrap_or_default(), - preface: req.preface.clone(), + title, + description, + preface, }; ctx.client.playbooks().create(payload).map_err(ApiError::FailedToCreatePlaybook) } + pub async fn delete(ctx: Arc, id: Uuid) -> Result { let playbooks = ctx.client.playbooks(); match playbooks.get(&id.to_string()) { From 3e77e665b49537848dc3bf787c0bfd215b43ebce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=98=89=E8=B1=AA?= Date: Fri, 19 Jan 2024 12:49:19 +0800 Subject: [PATCH 2/8] Impl playbook create method --- Cargo.lock | 8 ++++---- src/services/playbook.rs | 9 ++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 34da2ae..b5116dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,8 +62,8 @@ checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" [[package]] name = "amp-client" -version = "0.7.2" -source = "git+https://github.com/amphitheatre-app/amp-client-rust?tag=v0.7.2#c2be152c056ccc64bb1a6edc5e21fe9ff98dcbeb" +version = "0.7.3" +source = "git+https://github.com/amphitheatre-app/amp-client-rust?tag=v0.7.3#bda01131efe100bcfffb7e6c63668bfec5af3be1" dependencies = [ "amp-common", "futures", @@ -75,8 +75,8 @@ dependencies = [ [[package]] name = "amp-common" -version = "0.7.2" -source = "git+https://github.com/amphitheatre-app/common?tag=v0.7.2#eb4a212317901c24be7dae2b4ef541760e1c3340" +version = "0.7.3" +source = "git+https://github.com/amphitheatre-app/common?tag=v0.7.3#879abac73ee9ea7001f80a1996d890420ade51c6" dependencies = [ "anyhow", "chrono", diff --git a/src/services/playbook.rs b/src/services/playbook.rs index dbbf834..259354e 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -14,8 +14,8 @@ use amp_client::playbooks::PlaybookPayload; use amp_common::resource::{PlaybookSpec, Preface}; -use std::sync::Arc; use amp_common::schema::GitReference; +use std::sync::Arc; use tracing::{error, info}; use uuid::Uuid; @@ -49,15 +49,10 @@ impl PlaybookService { reference.repo = req.repo.clone(); preface.name = req.repo.clone(); preface.repository = Some(reference); - let payload = PlaybookPayload { - title, - description, - preface, - }; + let payload = PlaybookPayload { title, description, preface }; ctx.client.playbooks().create(payload).map_err(ApiError::FailedToCreatePlaybook) } - pub async fn delete(ctx: Arc, id: Uuid) -> Result { let playbooks = ctx.client.playbooks(); match playbooks.get(&id.to_string()) { From e3b1085efdfdc1e0f509bb159103eb2b0e57edb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=98=89=E8=B1=AA?= Date: Fri, 19 Jan 2024 13:29:46 +0800 Subject: [PATCH 3/8] Impl playbook create method --- src/requests/playbook.rs | 1 - src/services/playbook.rs | 7 +------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/requests/playbook.rs b/src/requests/playbook.rs index 4ec382e..4db9a80 100644 --- a/src/requests/playbook.rs +++ b/src/requests/playbook.rs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use amp_common::resource::Preface; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; diff --git a/src/services/playbook.rs b/src/services/playbook.rs index 259354e..be25135 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -44,12 +44,7 @@ impl PlaybookService { info!("Not found github repositories in {}...", repo); } } - let mut preface = Preface::default(); - let mut reference = GitReference::default(); - reference.repo = req.repo.clone(); - preface.name = req.repo.clone(); - preface.repository = Some(reference); - let payload = PlaybookPayload { title, description, preface }; + let payload = PlaybookPayload { title, description, preface: Preface::repository(&req.repo) }; ctx.client.playbooks().create(payload).map_err(ApiError::FailedToCreatePlaybook) } From 66d4e1869a8e34d2e598515047ab2c783d5c98c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=98=89=E8=B1=AA?= Date: Fri, 19 Jan 2024 13:31:50 +0800 Subject: [PATCH 4/8] Impl playbook create method --- src/services/playbook.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/playbook.rs b/src/services/playbook.rs index be25135..da9ca19 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -33,7 +33,7 @@ impl PlaybookService { let mut title = String::new(); let mut description = String::new(); - let repo = repo(req.repo.as_str()).unwrap_or_default(); + let repo = repo(&req.repo).unwrap_or_default(); let repository = ctx.github_client.repositories().find(repo.as_str()).ok().unwrap_or_default(); match repository { Some(repository) => { From ba0ab9d6a740edbcb6de28e917b353d1c9a9e599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=98=89=E8=B1=AA?= Date: Fri, 19 Jan 2024 13:32:31 +0800 Subject: [PATCH 5/8] Impl playbook create method --- src/services/playbook.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/playbook.rs b/src/services/playbook.rs index da9ca19..ac0e6d4 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -14,7 +14,6 @@ use amp_client::playbooks::PlaybookPayload; use amp_common::resource::{PlaybookSpec, Preface}; -use amp_common::schema::GitReference; use std::sync::Arc; use tracing::{error, info}; From 92d946b4e2a1f729252759d245036942527147ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=98=89=E8=B1=AA?= Date: Fri, 19 Jan 2024 13:37:45 +0800 Subject: [PATCH 6/8] Change repo.as_str() to &repo --- src/services/playbook.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/playbook.rs b/src/services/playbook.rs index ac0e6d4..c8e0a7d 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -33,7 +33,7 @@ impl PlaybookService { let mut description = String::new(); let repo = repo(&req.repo).unwrap_or_default(); - let repository = ctx.github_client.repositories().find(repo.as_str()).ok().unwrap_or_default(); + let repository = ctx.github_client.repositories().find(&repo).ok().unwrap_or_default(); match repository { Some(repository) => { title = repo; From 2e622a5dcecbdfeaa8d4018863abc3efd85d4e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=98=89=E8=B1=AA?= Date: Fri, 19 Jan 2024 16:00:45 +0800 Subject: [PATCH 7/8] optimize playbook create method --- src/services/playbook.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/services/playbook.rs b/src/services/playbook.rs index c8e0a7d..d795517 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -29,21 +29,11 @@ pub struct PlaybookService; impl PlaybookService { pub async fn create(ctx: Arc, req: &CreatePlaybookRequest) -> Result { - let mut title = String::new(); - let mut description = String::new(); + let repo = repo(&req.repo)?; + let repository = ctx.github_client.repositories().find(&repo).map_err(ApiError::NotFoundRepo)?; + let description= repository.and_then(|r|r.description).unwrap_or_default(); + let payload = PlaybookPayload { title:repo, description, preface: Preface::repository(&req.repo) }; - let repo = repo(&req.repo).unwrap_or_default(); - let repository = ctx.github_client.repositories().find(&repo).ok().unwrap_or_default(); - match repository { - Some(repository) => { - title = repo; - description = repository.description; - } - None => { - info!("Not found github repositories in {}...", repo); - } - } - let payload = PlaybookPayload { title, description, preface: Preface::repository(&req.repo) }; ctx.client.playbooks().create(payload).map_err(ApiError::FailedToCreatePlaybook) } From da5dc0fdd1f4d61d41dd202cd3101352ee998082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=98=89=E8=B1=AA?= Date: Fri, 19 Jan 2024 16:06:06 +0800 Subject: [PATCH 8/8] Impl playbook create method --- src/errors.rs | 4 ++++ src/services/playbook.rs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index c020c78..44b55de 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -56,6 +56,9 @@ pub enum ApiError { #[error("Bad Playbook: {0}")] BadPlaybook(String), + + #[error("Not Found Repo: {0}")] + NotFoundRepo(anyhow::Error), } impl IntoResponse for ApiError { @@ -72,6 +75,7 @@ impl IntoResponse for ApiError { Self::NotFoundFolder(e) => (StatusCode::NOT_FOUND, e.to_string()), Self::FailedToSynchronize(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), Self::BadPlaybook(e) => (StatusCode::BAD_REQUEST, e.to_string()), + Self::NotFoundRepo(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), }; error!("{} - {}", status, message); diff --git a/src/services/playbook.rs b/src/services/playbook.rs index d795517..8765f8d 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -31,8 +31,8 @@ impl PlaybookService { pub async fn create(ctx: Arc, req: &CreatePlaybookRequest) -> Result { let repo = repo(&req.repo)?; let repository = ctx.github_client.repositories().find(&repo).map_err(ApiError::NotFoundRepo)?; - let description= repository.and_then(|r|r.description).unwrap_or_default(); - let payload = PlaybookPayload { title:repo, description, preface: Preface::repository(&req.repo) }; + let description = repository.and_then(|r| Option::from(r.name)).unwrap_or_default(); + let payload = PlaybookPayload { title: repo, description, preface: Preface::repository(&req.repo) }; ctx.client.playbooks().create(payload).map_err(ApiError::FailedToCreatePlaybook) }