From 6aa6a077e353ec65ea7c8983b25c95410e6c5db6 Mon Sep 17 00:00:00 2001 From: jiahao6635 Date: Sat, 20 Jan 2024 01:10:05 +0800 Subject: [PATCH 1/5] amp-common->v0.7.4,amp-client->v0.7.4 --- Cargo.lock | 8 ++++---- Cargo.toml | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b5116dc..961f2c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,8 +62,8 @@ checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" [[package]] name = "amp-client" -version = "0.7.3" -source = "git+https://github.com/amphitheatre-app/amp-client-rust?tag=v0.7.3#bda01131efe100bcfffb7e6c63668bfec5af3be1" +version = "0.7.4" +source = "git+https://github.com/amphitheatre-app/amp-client-rust?tag=v0.7.4#44813d305ffed8951dad053327d56538fca96bb3" dependencies = [ "amp-common", "futures", @@ -75,8 +75,8 @@ dependencies = [ [[package]] name = "amp-common" -version = "0.7.3" -source = "git+https://github.com/amphitheatre-app/common?tag=v0.7.3#879abac73ee9ea7001f80a1996d890420ade51c6" +version = "0.7.4" +source = "git+https://github.com/amphitheatre-app/common?tag=v0.7.4#d4f8b4b6cfee8766a2d19bcc96291d09db6d0228" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 8afe453..5b5478c 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.3" } -amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.3" } +amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.7.4" } +amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.4" } anyhow = "1.0" axum = { version = "0.7.4" } clap = { version = "4.4.12", features = ["derive", "env"] } From ca6c8fe87e4e91ea050084ed3b92137e69f668d6 Mon Sep 17 00:00:00 2001 From: jiahao6635 Date: Sat, 20 Jan 2024 01:13:47 +0800 Subject: [PATCH 2/5] Modify the parameter extraction of the obtained file --- src/handlers/file.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/handlers/file.rs b/src/handlers/file.rs index bfc3264..ab4a68e 100644 --- a/src/handlers/file.rs +++ b/src/handlers/file.rs @@ -45,10 +45,7 @@ use crate::services::FileService; )] pub async fn get( State(ctx): State>, - - Path(id): Path, - Path(reference): Path, - Path(path): Path, + Path((id, reference, path)): Path<(Uuid, String, String)>, ) -> Result { Ok(Json(FileService::get(ctx, id, reference, path).await?)) } From 0480d2bb874b7eedde75047e0bba44916a7e14c1 Mon Sep 17 00:00:00 2001 From: jiahao6635 Date: Sat, 20 Jan 2024 01:16:57 +0800 Subject: [PATCH 3/5] Modify Get folder reference to content.sha --- src/handlers/folder.rs | 11 +++++------ src/services/folder.rs | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/handlers/folder.rs b/src/handlers/folder.rs index dd3104d..b593d33 100644 --- a/src/handlers/folder.rs +++ b/src/handlers/folder.rs @@ -12,9 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::collections::HashMap; use std::sync::Arc; -use axum::extract::{Path, State}; +use axum::extract::{Path, Query, State}; use axum::http::StatusCode; use axum::response::IntoResponse; use axum::Json; @@ -45,12 +46,10 @@ use crate::services::FolderService; )] pub async fn get( State(ctx): State>, - - Path(id): Path, - Path(reference): Path, - Path(path): Path, + Path((id, reference, path)): Path<(Uuid, String, Option)>, + Query(params): Query>, ) -> Result { - Ok(Json(FolderService::get(ctx, id, reference, path).await?)) + Ok(Json(FolderService::get(ctx, id, reference, path, params).await?)) } /// Create a folder diff --git a/src/services/folder.rs b/src/services/folder.rs index 9390a9e..bd280c5 100644 --- a/src/services/folder.rs +++ b/src/services/folder.rs @@ -13,6 +13,7 @@ // limitations under the License. use amp_common::scm::git::Tree; +use std::collections::HashMap; use std::sync::Arc; use uuid::Uuid; @@ -27,13 +28,26 @@ pub struct FolderService; impl FolderService { // @FIXME: pass path to get_trees() method. // @TODO: add recursive option argument to current method. - pub async fn get(ctx: Arc, id: Uuid, reference: String, _path: String) -> Result { + pub async fn get( + ctx: Arc, + id: Uuid, + _reference: String, + path: Option, + recursive: HashMap, + ) -> Result { let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?; let source = playbook.preface.repository.unwrap(); + let recursive = recursive.get("recursive").is_some(); + + let content = ctx + .github_client + .contents() + .find(&utils::repo(&source.repo)?, &path.unwrap_or_default(), &source.branch.unwrap_or_default()) + .map_err(|e| ApiError::NotFoundContent(e.to_string()))?; ctx.github_client .git() - .git_trees(&utils::repo(&source.repo)?, &reference, Some(true)) + .get_tree(&utils::repo(&source.repo)?, &content.sha, Some(recursive)) .map_err(|e| ApiError::NotFoundFolder(e.to_string()))? .ok_or(ApiError::NotFoundFolder("The folder is none".to_string())) } From bf1af1db8f016dbcc7f22f700b06c03e462b6649 Mon Sep 17 00:00:00 2001 From: jiahao6635 Date: Sat, 20 Jan 2024 01:18:46 +0800 Subject: [PATCH 4/5] playbook create add reference --- src/requests/playbook.rs | 9 +++++++++ src/services/playbook.rs | 11 ++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/requests/playbook.rs b/src/requests/playbook.rs index 4db9a80..dd4da67 100644 --- a/src/requests/playbook.rs +++ b/src/requests/playbook.rs @@ -18,4 +18,13 @@ use utoipa::ToSchema; #[derive(Debug, Serialize, Deserialize, ToSchema)] pub struct CreatePlaybookRequest { pub repo: String, + pub reference: Option, +} +pub trait GitReferenceMethods { + fn new(repo: String, branch: Option) -> Self; +} +impl GitReferenceMethods for amp_common::schema::GitReference { + fn new(repo: String, branch: Option) -> Self { + amp_common::schema::GitReference { repo, branch, ..Default::default() } + } } diff --git a/src/services/playbook.rs b/src/services/playbook.rs index 8765f8d..7242d2e 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -22,7 +22,7 @@ use uuid::Uuid; use crate::context::Context; use crate::errors::ApiError; use crate::errors::Result; -use crate::requests::playbook::CreatePlaybookRequest; +use crate::requests::playbook::{CreatePlaybookRequest, GitReferenceMethods}; use crate::utils::repo; pub struct PlaybookService; @@ -31,8 +31,13 @@ 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| Option::from(r.name)).unwrap_or_default(); - let payload = PlaybookPayload { title: repo, description, preface: Preface::repository(&req.repo) }; + let description = repository.and_then(|r| r.description).unwrap_or_default(); + let preface = Preface { + name: req.repo.clone(), + repository: Some(GitReferenceMethods::new(req.repo.clone(), req.reference.clone())), + ..Preface::default() + }; + let payload = PlaybookPayload { title: repo, description, preface }; ctx.client.playbooks().create(payload).map_err(ApiError::FailedToCreatePlaybook) } From 188fab9acda2cc281f764c411eace7666f1a90b8 Mon Sep 17 00:00:00 2001 From: jiahao6635 Date: Sat, 20 Jan 2024 10:58:13 +0800 Subject: [PATCH 5/5] Modified according to review --- src/handlers/folder.rs | 2 +- src/requests/playbook.rs | 8 -------- src/services/folder.rs | 8 ++------ src/services/playbook.rs | 9 +++++++-- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/handlers/folder.rs b/src/handlers/folder.rs index b593d33..8b308f2 100644 --- a/src/handlers/folder.rs +++ b/src/handlers/folder.rs @@ -49,7 +49,7 @@ pub async fn get( Path((id, reference, path)): Path<(Uuid, String, Option)>, Query(params): Query>, ) -> Result { - Ok(Json(FolderService::get(ctx, id, reference, path, params).await?)) + Ok(Json(FolderService::get(ctx, id, reference, path, params.get("recursive")).await?)) } /// Create a folder diff --git a/src/requests/playbook.rs b/src/requests/playbook.rs index dd4da67..2828966 100644 --- a/src/requests/playbook.rs +++ b/src/requests/playbook.rs @@ -20,11 +20,3 @@ pub struct CreatePlaybookRequest { pub repo: String, pub reference: Option, } -pub trait GitReferenceMethods { - fn new(repo: String, branch: Option) -> Self; -} -impl GitReferenceMethods for amp_common::schema::GitReference { - fn new(repo: String, branch: Option) -> Self { - amp_common::schema::GitReference { repo, branch, ..Default::default() } - } -} diff --git a/src/services/folder.rs b/src/services/folder.rs index bd280c5..e26cd9a 100644 --- a/src/services/folder.rs +++ b/src/services/folder.rs @@ -13,7 +13,6 @@ // limitations under the License. use amp_common::scm::git::Tree; -use std::collections::HashMap; use std::sync::Arc; use uuid::Uuid; @@ -26,18 +25,15 @@ use crate::utils; pub struct FolderService; impl FolderService { - // @FIXME: pass path to get_trees() method. - // @TODO: add recursive option argument to current method. pub async fn get( ctx: Arc, id: Uuid, _reference: String, path: Option, - recursive: HashMap, + recursive: Option<&String>, ) -> Result { let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?; let source = playbook.preface.repository.unwrap(); - let recursive = recursive.get("recursive").is_some(); let content = ctx .github_client @@ -47,7 +43,7 @@ impl FolderService { ctx.github_client .git() - .get_tree(&utils::repo(&source.repo)?, &content.sha, Some(recursive)) + .get_tree(&utils::repo(&source.repo)?, &content.sha, Option::from(recursive.is_some())) .map_err(|e| ApiError::NotFoundFolder(e.to_string()))? .ok_or(ApiError::NotFoundFolder("The folder is none".to_string())) } diff --git a/src/services/playbook.rs b/src/services/playbook.rs index 7242d2e..978321a 100644 --- a/src/services/playbook.rs +++ b/src/services/playbook.rs @@ -14,6 +14,7 @@ use amp_client::playbooks::PlaybookPayload; use amp_common::resource::{PlaybookSpec, Preface}; +use amp_common::schema::GitReference; use std::sync::Arc; use tracing::{error, info}; @@ -22,7 +23,7 @@ use uuid::Uuid; use crate::context::Context; use crate::errors::ApiError; use crate::errors::Result; -use crate::requests::playbook::{CreatePlaybookRequest, GitReferenceMethods}; +use crate::requests::playbook::CreatePlaybookRequest; use crate::utils::repo; pub struct PlaybookService; @@ -34,7 +35,11 @@ impl PlaybookService { let description = repository.and_then(|r| r.description).unwrap_or_default(); let preface = Preface { name: req.repo.clone(), - repository: Some(GitReferenceMethods::new(req.repo.clone(), req.reference.clone())), + repository: Some(GitReference { + repo: req.repo.clone(), + branch: req.reference.clone(), + ..GitReference::default() + }), ..Preface::default() }; let payload = PlaybookPayload { title: repo, description, preface };