Skip to content

Commit

Permalink
Merge pull request #42 from jiahao6635/master
Browse files Browse the repository at this point in the history
folder get return Vec<File>,add folder tree
  • Loading branch information
wangeguo authored Jan 22, 2024
2 parents 7910646 + f9900f3 commit f30c8fd
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 137 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub enum ApiError {

#[error("Not Found Repo: {0}")]
NotFoundRepo(anyhow::Error),

#[error("Bad Playbook Request: {0}")]
BadPlaybookRequest(String),
}

impl IntoResponse for ApiError {
Expand All @@ -76,6 +79,7 @@ impl IntoResponse for ApiError {
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()),
Self::BadPlaybookRequest(e) => (StatusCode::BAD_REQUEST, e.to_string()),
};

error!("{} - {}", status, message);
Expand Down
40 changes: 13 additions & 27 deletions src/handlers/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -43,19 +42,15 @@ use crate::services::FileService;
),
tag = "Files"
)]
pub async fn get(
State(ctx): State<Arc<Context>>,
Path((id, reference, path)): Path<(Uuid, String, String)>,
) -> Result<impl IntoResponse> {
Ok(Json(FileService::get(ctx, id, reference, path).await?))
pub async fn get(State(ctx): State<Arc<Context>>, Path((id, path)): Path<(Uuid, String)>) -> Result<impl IntoResponse> {
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(
Expand All @@ -74,20 +69,18 @@ pub async fn create(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<FileRequest>,
) -> Result<impl IntoResponse> {
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(
Expand All @@ -107,20 +100,18 @@ pub async fn update(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<FileRequest>,
) -> Result<impl IntoResponse> {
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(
Expand All @@ -135,20 +126,18 @@ pub async fn delete(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,
) -> Result<impl IntoResponse> {
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(
Expand All @@ -168,20 +157,18 @@ pub async fn copy(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<DestinationRequest>,
) -> Result<impl IntoResponse> {
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(
Expand All @@ -201,10 +188,9 @@ pub async fn rename(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<DestinationRequest>,
) -> Result<impl IntoResponse> {
Ok(Json(FileService::rename(ctx, id, reference, path, req.destination).await?))
Ok(Json(FileService::rename(ctx, id, path, req.destination).await?))
}
54 changes: 32 additions & 22 deletions src/handlers/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,32 @@ 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}",
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(
(status = 200, description = "The folder tree", body = Vec<File>),
(status = 404, description = "Playbook not found"),
(status = 404, description = "Folder not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Folders"
)]
pub async fn get(State(ctx): State<Arc<Context>>, Path((id, path)): Path<(Uuid, String)>) -> Result<impl IntoResponse> {
Ok(Json(FolderService::get(ctx, id, 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"),
Expand All @@ -44,20 +62,19 @@ use crate::services::FolderService;
),
tag = "Folders"
)]
pub async fn get(
pub async fn tree(
State(ctx): State<Arc<Context>>,
Path((id, reference, path)): Path<(Uuid, String, Option<String>)>,
Path(id): Path<Uuid>,
Query(params): Query<HashMap<String, String>>,
) -> Result<impl IntoResponse> {
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
#[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(
Expand All @@ -71,18 +88,16 @@ pub async fn create(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,
) -> Result<impl IntoResponse> {
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(
Expand All @@ -97,20 +112,18 @@ pub async fn delete(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,
) -> Result<impl IntoResponse> {
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(
Expand All @@ -130,20 +143,18 @@ pub async fn copy(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<DestinationRequest>,
) -> Result<impl IntoResponse> {
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(
Expand All @@ -163,10 +174,9 @@ pub async fn rename(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<DestinationRequest>,
) -> Result<impl IntoResponse> {
Ok(Json(FolderService::rename(ctx, id, reference, path, req.destination).await?))
Ok(Json(FolderService::rename(ctx, id, path, req.destination).await?))
}
14 changes: 13 additions & 1 deletion src/requests/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ use utoipa::ToSchema;

#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct CreatePlaybookRequest {
/// Source code repository the partner should be cloned from.
/// e.g. https://github.com/amphitheatre-app/amphitheatre.git.
pub repo: String,
pub reference: Option<String>,
/// Git branch the partner should be cloned from. eg. master or main
#[serde(skip_serializing_if = "Option::is_none")]
pub branch: Option<String>,
/// Git tag the partner should be cloned from. eg. v1.0
#[serde(skip_serializing_if = "Option::is_none")]
pub tag: Option<String>,
/// A commit hash like rev = "4c59b707", or a named reference exposed by
/// the remote repository such as rev = "refs/pull/493/head". What references
/// are available varies by where the repo is hosted.
#[serde(skip_serializing_if = "Option::is_none")]
pub rev: Option<String>,
}
23 changes: 12 additions & 11 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ pub fn build() -> Router<Arc<Context>> {
.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/: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", get(folder::get))
.route("/v1/playbooks/:id/tree", get(folder::tree))
.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))
}
Loading

0 comments on commit f30c8fd

Please sign in to comment.