diff --git a/src/handlers/file.rs b/src/handlers/file.rs index ab4a68e..f8e3b15 100644 --- a/src/handlers/file.rs +++ b/src/handlers/file.rs @@ -29,10 +29,9 @@ use crate::services::FileService; /// Returns a file's content. #[utoipa::path( - get, path = "/v1/playbooks/{id}/files/{reference}/{path}", + get, path = "/v1/playbooks/{id}/files/{path}", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag."), ("path" = String, description = "The file path relative to the root of the repository."), ), responses( @@ -45,17 +44,16 @@ use crate::services::FileService; )] pub async fn get( State(ctx): State>, - Path((id, reference, path)): Path<(Uuid, String, String)>, + Path((id, path)): Path<(Uuid, String)>, ) -> Result { - Ok(Json(FileService::get(ctx, id, reference, path).await?)) + Ok(Json(FileService::get(ctx, id, path).await?)) } /// Create a file #[utoipa::path( - post, path = "/v1/playbooks/{id}/files/{reference}/{path}", + post, path = "/v1/playbooks/{id}/files/{path}", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag."), ("path" = String, description = "The file path relative to the root of the repository."), ), request_body( @@ -74,20 +72,18 @@ pub async fn create( State(ctx): State>, Path(id): Path, - Path(reference): Path, Path(path): Path, Json(req): Json, ) -> Result { - Ok((StatusCode::CREATED, Json(FileService::create(ctx, id, reference, path, req.content).await?))) + Ok((StatusCode::CREATED, Json(FileService::create(ctx, id, path, req.content).await?))) } /// Update a file #[utoipa::path( - put, path = "/v1/playbooks/{id}/files/{reference}/{path}", + put, path = "/v1/playbooks/{id}/files/{path}", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."), ("path" = String, description = "The file path relative to the root of the repository."), ), request_body( @@ -107,20 +103,18 @@ pub async fn update( State(ctx): State>, Path(id): Path, - Path(reference): Path, Path(path): Path, Json(req): Json, ) -> Result { - Ok(Json(FileService::update(ctx, id, reference, path, req.content).await?)) + Ok(Json(FileService::update(ctx, id, path, req.content).await?)) } /// Delete a file #[utoipa::path( - delete, path = "/v1/playbooks/{id}/files/{reference}/{path}", + delete, path = "/v1/playbooks/{id}/files/{path}", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag."), ("path" = String, description = "The file path relative to the root of the repository."), ), responses( @@ -135,20 +129,18 @@ pub async fn delete( State(ctx): State>, Path(id): Path, - Path(reference): Path, Path(path): Path, ) -> Result { - FileService::delete(ctx, id, reference, path).await?; + FileService::delete(ctx, id, path).await?; Ok(StatusCode::NO_CONTENT) } /// Copy a file #[utoipa::path( - post, path = "/v1/playbooks/{id}/files/{reference}/{path}/actions/copy", + post, path = "/v1/playbooks/{id}/files/{path}/actions/copy", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."), ("path" = String, description = "The file path relative to the root of the repository."), ), request_body( @@ -168,20 +160,18 @@ pub async fn copy( State(ctx): State>, Path(id): Path, - Path(reference): Path, Path(path): Path, Json(req): Json, ) -> Result { - Ok(Json(FileService::copy(ctx, id, reference, path, req.destination).await?)) + Ok(Json(FileService::copy(ctx, id, path, req.destination).await?)) } /// Move a file #[utoipa::path( - post, path = "/v1/playbooks/{id}/files/{reference}/{path}/actions/move", + post, path = "/v1/playbooks/{id}/files/{path}/actions/move", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."), ("path" = String, description = "The file path relative to the root of the repository."), ), request_body( @@ -201,10 +191,9 @@ pub async fn rename( State(ctx): State>, Path(id): Path, - Path(reference): Path, Path(path): Path, Json(req): Json, ) -> Result { - Ok(Json(FileService::rename(ctx, id, reference, path, req.destination).await?)) + Ok(Json(FileService::rename(ctx, id, path, req.destination).await?)) } diff --git a/src/handlers/folder.rs b/src/handlers/folder.rs index 5126894..1fd3dc5 100644 --- a/src/handlers/folder.rs +++ b/src/handlers/folder.rs @@ -30,10 +30,9 @@ use crate::services::FolderService; /// Gets the file list of a directory in a repository. #[utoipa::path( - get, path = "/v1/playbooks/{id}/folders/{reference}/{path}", + get, path = "/v1/playbooks/{id}/folders/{path}", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag."), ("path" = String, description = "The file path relative to the root of the repository."), ), responses( @@ -46,9 +45,9 @@ use crate::services::FolderService; )] pub async fn get( State(ctx): State>, - Path((id, reference, path)): Path<(Uuid, String, Option)>, + Path((id, path)): Path<(Uuid, Option)>, ) -> Result { - Ok(Json(FolderService::get(ctx, id, reference, path).await?)) + Ok(Json(FolderService::get(ctx, id, path).await?)) } /// Returns a folder's tree. @@ -76,10 +75,9 @@ pub async fn tree( /// Create a folder #[utoipa::path( - post, path = "/v1/playbooks/{id}/folders/{reference}/{path}", + post, path = "/v1/playbooks/{id}/folders/{path}", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag."), ("path" = String, description = "The file path relative to the root of the repository."), ), responses( @@ -93,18 +91,16 @@ pub async fn create( State(ctx): State>, Path(id): Path, - Path(reference): Path, Path(path): Path, ) -> Result { - Ok((StatusCode::CREATED, Json(FolderService::create(ctx, id, reference, path).await?))) + Ok((StatusCode::CREATED, Json(FolderService::create(ctx, id, path).await?))) } /// Delete a folder #[utoipa::path( - delete, path = "/v1/playbooks/{id}/folders/{reference}/{path}", + delete, path = "/v1/playbooks/{id}/folders/{path}", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag."), ("path" = String, description = "The file path relative to the root of the repository."), ), responses( @@ -119,20 +115,18 @@ pub async fn delete( State(ctx): State>, Path(id): Path, - Path(reference): Path, Path(path): Path, ) -> Result { - FolderService::delete(ctx, id, reference, path).await?; + FolderService::delete(ctx, id, path).await?; Ok(StatusCode::NO_CONTENT) } /// Copy a folder #[utoipa::path( - post, path = "/v1/playbooks/{id}/folders/{reference}/{path}/actions/copy", + post, path = "/v1/playbooks/{id}/folders/{path}/actions/copy", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."), ("path" = String, description = "The file path relative to the root of the repository."), ), request_body( @@ -152,20 +146,18 @@ pub async fn copy( State(ctx): State>, Path(id): Path, - Path(reference): Path, Path(path): Path, Json(req): Json, ) -> Result { - Ok(Json(FolderService::copy(ctx, id, reference, path, req.destination).await?)) + Ok(Json(FolderService::copy(ctx, id, path, req.destination).await?)) } /// Move a folder #[utoipa::path( - post, path = "/v1/playbooks/{id}/folders/{reference}/{path}/actions/move", + post, path = "/v1/playbooks/{id}/folders/{path}/actions/move", params( ("id" = Uuid, description = "The id of playbook"), - ("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."), ("path" = String, description = "The file path relative to the root of the repository."), ), request_body( @@ -185,10 +177,9 @@ pub async fn rename( State(ctx): State>, Path(id): Path, - Path(reference): Path, Path(path): Path, Json(req): Json, ) -> Result { - Ok(Json(FolderService::rename(ctx, id, reference, path, req.destination).await?)) + Ok(Json(FolderService::rename(ctx, id, path, req.destination).await?)) } diff --git a/src/routes.rs b/src/routes.rs index dc1441d..2fd1760 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -31,18 +31,18 @@ pub fn build() -> Router> { .route("/v1/playbooks/:id/logs", get(logger::logs)) // // files - .route("/v1/playbooks/:id/files/:reference/:path", get(file::get)) - .route("/v1/playbooks/:id/files/:reference/:path", post(file::create)) - .route("/v1/playbooks/:id/files/:reference/:path", put(file::update)) - .route("/v1/playbooks/:id/files/:reference/:path", delete(file::delete)) - .route("/v1/playbooks/:id/files/:reference/:path/actions/copy", post(file::copy)) - .route("/v1/playbooks/:id/files/:reference/:path/actions/move", post(file::rename)) + .route("/v1/playbooks/:id/files/:path", get(file::get)) + .route("/v1/playbooks/:id/files/:path", post(file::create)) + .route("/v1/playbooks/:id/files/:path", put(file::update)) + .route("/v1/playbooks/:id/files/:path", delete(file::delete)) + .route("/v1/playbooks/:id/files/:path/actions/copy", post(file::copy)) + .route("/v1/playbooks/:id/files/:path/actions/move", post(file::rename)) // // folders - .route("/v1/playbooks/:id/folders/:reference/:path", get(folder::get)) + .route("/v1/playbooks/:id/folders/: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)) - .route("/v1/playbooks/:id/folders/:reference/:path/actions/move", post(folder::rename)) + .route("/v1/playbooks/:id/folders/:path", post(folder::create)) + .route("/v1/playbooks/:id/folders/:path", delete(folder::delete)) + .route("/v1/playbooks/:id/folders/:path/actions/copy", post(folder::copy)) + .route("/v1/playbooks/:id/folders/:path/actions/move", post(folder::rename)) } diff --git a/src/services/file.rs b/src/services/file.rs index f959380..6019ed0 100644 --- a/src/services/file.rs +++ b/src/services/file.rs @@ -27,13 +27,13 @@ pub struct FileService; impl FileService { /// Get a file content from the remote git repository. - pub async fn get(ctx: Arc, id: Uuid, reference: String, path: String) -> Result { + pub async fn get(ctx: Arc, id: Uuid, path: String) -> Result { let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?; let source = playbook.preface.repository.unwrap(); ctx.github_client .contents() - .find(&utils::repo(&source.repo)?, &path, &reference) + .find(&utils::repo(&source.repo)?, &path, &source.branch.unwrap_or_default()) .map_err(|e| ApiError::NotFoundContent(e.to_string())) } @@ -41,7 +41,6 @@ impl FileService { pub async fn create( ctx: Arc, id: Uuid, - _reference: String, path: String, content: String, ) -> Result { @@ -64,7 +63,6 @@ impl FileService { pub async fn update( _ctx: Arc, _id: Uuid, - _reference: String, _path: String, _content: String, ) -> Result { @@ -73,7 +71,7 @@ impl FileService { } /// Delete a file from the workspace. - pub async fn delete(_ctx: Arc, _id: Uuid, _reference: String, _path: String) -> Result<()> { + pub async fn delete(_ctx: Arc, _id: Uuid, _path: String) -> Result<()> { // refer to create() method. todo!() } @@ -82,7 +80,6 @@ impl FileService { pub async fn copy( _ctx: Arc, _id: Uuid, - _reference: String, _path: String, _destination: String, ) -> Result { @@ -94,7 +91,6 @@ impl FileService { pub async fn rename( _ctx: Arc, _id: Uuid, - _reference: String, _path: String, _destination: String, ) -> Result { diff --git a/src/services/folder.rs b/src/services/folder.rs index 2a463f4..62438b2 100644 --- a/src/services/folder.rs +++ b/src/services/folder.rs @@ -28,7 +28,6 @@ impl FolderService { pub async fn get( ctx: Arc, id: Uuid, - _reference: String, path: Option, ) -> Result, ApiError> { let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?; @@ -55,18 +54,17 @@ impl FolderService { .ok_or(ApiError::NotFoundFolder("The folder is none".to_string())) } - pub async fn create(_ctx: Arc, _id: Uuid, _reference: String, _path: String) -> Result { + pub async fn create(_ctx: Arc, _id: Uuid, _path: String) -> Result { todo!() } - pub async fn delete(_ctx: Arc, _id: Uuid, _reference: String, _path: String) -> Result<()> { + pub async fn delete(_ctx: Arc, _id: Uuid, _path: String) -> Result<()> { todo!() } pub async fn copy( _ctx: Arc, _id: Uuid, - _reference: String, _path: String, _destination: String, ) -> Result { @@ -76,7 +74,6 @@ impl FolderService { pub async fn rename( _ctx: Arc, _id: Uuid, - _reference: String, _path: String, _destination: String, ) -> Result {