From d9ca75a38b090142dae51efa966fa156b18b75c7 Mon Sep 17 00:00:00 2001 From: jiahao6635 Date: Mon, 22 Jan 2024 01:27:45 +0800 Subject: [PATCH] folder get return Vec,add folder tree --- Cargo.lock | 8 ++++---- Cargo.toml | 4 ++-- src/handlers/folder.rs | 28 +++++++++++++++++++++++++--- src/routes.rs | 1 + src/services/folder.rs | 25 ++++++++++++++++--------- src/swagger.rs | 2 ++ 6 files changed, 50 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff39e49..5007f7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,8 +62,8 @@ checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" [[package]] name = "amp-client" -version = "0.7.4" -source = "git+https://github.com/amphitheatre-app/amp-client-rust?tag=v0.7.4#44813d305ffed8951dad053327d56538fca96bb3" +version = "0.7.6" +source = "git+https://github.com/amphitheatre-app/amp-client-rust?tag=v0.7.6#082f4db7d0668b8249e1cb7f3e744149711e9ee3" dependencies = [ "amp-common", "futures", @@ -75,8 +75,8 @@ dependencies = [ [[package]] name = "amp-common" -version = "0.7.4" -source = "git+https://github.com/amphitheatre-app/common?tag=v0.7.4#d4f8b4b6cfee8766a2d19bcc96291d09db6d0228" +version = "0.7.7" +source = "git+https://github.com/amphitheatre-app/common?tag=v0.7.7#663f18822f1a3cad634ef38396698c9add0e2f22" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index cfc6148..0591e40 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.4" } -amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.4" } +amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.7.6" } +amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.7" } anyhow = "1.0" axum = { version = "0.7.4" } clap = { version = "4.4.12", features = ["derive", "env"] } diff --git a/src/handlers/folder.rs b/src/handlers/folder.rs index 8b308f2..5126894 100644 --- a/src/handlers/folder.rs +++ b/src/handlers/folder.rs @@ -28,7 +28,7 @@ use crate::services::FolderService; // The Folders Service Handlers. -/// Returns a folder's tree. +/// Gets the file list of a directory in a repository. #[utoipa::path( get, path = "/v1/playbooks/{id}/folders/{reference}/{path}", params( @@ -37,7 +37,7 @@ use crate::services::FolderService; ("path" = String, description = "The file path relative to the root of the repository."), ), responses( - (status = 200, description = "The folder tree", body = Tree), + (status = 200, description = "The folder tree", body = Vec), (status = 404, description = "Playbook not found"), (status = 404, description = "Folder not found"), (status = 500, description = "Internal Server Error"), @@ -47,9 +47,31 @@ use crate::services::FolderService; pub async fn get( State(ctx): State>, Path((id, reference, path)): Path<(Uuid, String, Option)>, +) -> Result { + Ok(Json(FolderService::get(ctx, id, reference, path).await?)) +} + +/// Returns a folder's tree. + +#[utoipa::path( + get, path = "/v1/playbooks/{id}/tree", + params( + ("id" = Uuid, description = "The id of playbook"), + ), + responses( + (status = 200, description = "The folder tree", body = Tree), + (status = 404, description = "Playbook not found"), + (status = 404, description = "Folder not found"), + (status = 500, description = "Internal Server Error"), + ), + tag = "Folders" +)] +pub async fn tree( + State(ctx): State>, + Path(id): Path, Query(params): Query>, ) -> Result { - Ok(Json(FolderService::get(ctx, id, reference, path, params.get("recursive")).await?)) + Ok(Json(FolderService::tree(ctx, id, params.get("recursive")).await?)) } /// Create a folder diff --git a/src/routes.rs b/src/routes.rs index ca84cbc..dc1441d 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -40,6 +40,7 @@ pub fn build() -> Router> { // // folders .route("/v1/playbooks/:id/folders/:reference/:path", get(folder::get)) + .route("/v1/playbooks/:id/tree", get(folder::tree)) .route("/v1/playbooks/:id/folders/:reference/:path", post(folder::create)) .route("/v1/playbooks/:id/folders/:reference/:path", delete(folder::delete)) .route("/v1/playbooks/:id/folders/:reference/:path/actions/copy", post(folder::copy)) diff --git a/src/services/folder.rs b/src/services/folder.rs index e26cd9a..2a463f4 100644 --- a/src/services/folder.rs +++ b/src/services/folder.rs @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -use amp_common::scm::git::Tree; use std::sync::Arc; use uuid::Uuid; -use amp_common::scm::content::Content; +use amp_common::scm::content::{Content, File}; +use amp_common::scm::git::Tree; use crate::context::Context; use crate::errors::{ApiError, Result}; @@ -30,20 +30,27 @@ impl FolderService { id: Uuid, _reference: String, path: Option, - recursive: Option<&String>, - ) -> Result { + ) -> Result, ApiError> { let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?; let source = playbook.preface.repository.unwrap(); - let content = ctx - .github_client + 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()))?; + .list(&utils::repo(&source.repo)?, &path.unwrap_or_default(), &source.branch.unwrap_or_default()) + .map_err(|e| ApiError::NotFoundContent(e.to_string())) + } + + pub async fn tree(ctx: Arc, id: Uuid, recursive: Option<&String>) -> Result { + let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?; + let source = playbook.preface.repository.unwrap(); ctx.github_client .git() - .get_tree(&utils::repo(&source.repo)?, &content.sha, Option::from(recursive.is_some())) + .get_tree( + &utils::repo(&source.repo)?, + &source.branch.unwrap_or_default(), + 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/swagger.rs b/src/swagger.rs index b82d8ec..5a14407 100644 --- a/src/swagger.rs +++ b/src/swagger.rs @@ -34,6 +34,7 @@ use crate::{handlers, requests}; handlers::file::rename, handlers::folder::get, + handlers::folder::tree, handlers::folder::create, handlers::folder::delete, handlers::folder::copy, @@ -63,6 +64,7 @@ use crate::{handlers, requests}; amp_common::schema::Service, amp_common::scm::content::Content, + amp_common::scm::content::File, amp_common::scm::git::Tree, amp_common::scm::git::TreeEntry, )