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"] } 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?)) } diff --git a/src/handlers/folder.rs b/src/handlers/folder.rs index dd3104d..8b308f2 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.get("recursive")).await?)) } /// Create a folder diff --git a/src/requests/playbook.rs b/src/requests/playbook.rs index 4db9a80..2828966 100644 --- a/src/requests/playbook.rs +++ b/src/requests/playbook.rs @@ -18,4 +18,5 @@ use utoipa::ToSchema; #[derive(Debug, Serialize, Deserialize, ToSchema)] pub struct CreatePlaybookRequest { pub repo: String, + pub reference: Option, } diff --git a/src/services/folder.rs b/src/services/folder.rs index 9390a9e..e26cd9a 100644 --- a/src/services/folder.rs +++ b/src/services/folder.rs @@ -25,15 +25,25 @@ 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: String) -> Result { + pub async fn get( + ctx: Arc, + id: Uuid, + _reference: String, + path: Option, + recursive: Option<&String>, + ) -> Result { let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?; let source = playbook.preface.repository.unwrap(); + 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, 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 8765f8d..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}; @@ -31,8 +32,17 @@ 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(GitReference { + repo: req.repo.clone(), + branch: req.reference.clone(), + ..GitReference::default() + }), + ..Preface::default() + }; + let payload = PlaybookPayload { title: repo, description, preface }; ctx.client.playbooks().create(payload).map_err(ApiError::FailedToCreatePlaybook) }