Skip to content

Commit

Permalink
folder get return Vec<File>,add folder tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahao6635 committed Jan 21, 2024
1 parent 7910646 commit d9ca75a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 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
28 changes: 25 additions & 3 deletions src/handlers/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<File>),
(status = 404, description = "Playbook not found"),
(status = 404, description = "Folder not found"),
(status = 500, description = "Internal Server Error"),
Expand All @@ -47,9 +47,31 @@ use crate::services::FolderService;
pub async fn get(
State(ctx): State<Arc<Context>>,
Path((id, reference, path)): Path<(Uuid, String, Option<String>)>,
) -> Result<impl IntoResponse> {
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<Arc<Context>>,
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
Expand Down
1 change: 1 addition & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn build() -> Router<Arc<Context>> {
//
// 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))
Expand Down
25 changes: 16 additions & 9 deletions src/services/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -30,20 +30,27 @@ impl FolderService {
id: Uuid,
_reference: String,
path: Option<String>,
recursive: Option<&String>,
) -> Result<Tree> {
) -> Result<Vec<File>, 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<Context>, id: Uuid, recursive: Option<&String>) -> Result<Tree, ApiError> {
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()))
}
Expand Down
2 changes: 2 additions & 0 deletions src/swagger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit d9ca75a

Please sign in to comment.