Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

playbook create add reference #41

Merged
merged 5 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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"] }
Expand Down
5 changes: 1 addition & 4 deletions src/handlers/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ use crate::services::FileService;
)]
pub async fn get(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,
Path((id, reference, path)): Path<(Uuid, String, String)>,
) -> Result<impl IntoResponse> {
Ok(Json(FileService::get(ctx, id, reference, path).await?))
}
Expand Down
11 changes: 5 additions & 6 deletions src/handlers/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,12 +46,10 @@ use crate::services::FolderService;
)]
pub async fn get(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,
Path((id, reference, path)): Path<(Uuid, String, Option<String>)>,
Query(params): Query<HashMap<String, String>>,
) -> Result<impl IntoResponse> {
Ok(Json(FolderService::get(ctx, id, reference, path).await?))
Ok(Json(FolderService::get(ctx, id, reference, path, params).await?))
jiahao6635 marked this conversation as resolved.
Show resolved Hide resolved
}

/// Create a folder
Expand Down
9 changes: 9 additions & 0 deletions src/requests/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ use utoipa::ToSchema;
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct CreatePlaybookRequest {
pub repo: String,
pub reference: Option<String>,
}
pub trait GitReferenceMethods {
fn new(repo: String, branch: Option<String>) -> Self;
}
impl GitReferenceMethods for amp_common::schema::GitReference {
fn new(repo: String, branch: Option<String>) -> Self {
amp_common::schema::GitReference { repo, branch, ..Default::default() }
}
}
jiahao6635 marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 16 additions & 2 deletions src/services/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use amp_common::scm::git::Tree;
use std::collections::HashMap;
use std::sync::Arc;
use uuid::Uuid;

Expand All @@ -27,13 +28,26 @@ pub struct FolderService;
impl FolderService {
// @FIXME: pass path to get_trees() method.
jiahao6635 marked this conversation as resolved.
Show resolved Hide resolved
// @TODO: add recursive option argument to current method.
jiahao6635 marked this conversation as resolved.
Show resolved Hide resolved
pub async fn get(ctx: Arc<Context>, id: Uuid, reference: String, _path: String) -> Result<Tree> {
pub async fn get(
ctx: Arc<Context>,
id: Uuid,
_reference: String,
path: Option<String>,
recursive: HashMap<String, String>,
jiahao6635 marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<Tree> {
let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?;
let source = playbook.preface.repository.unwrap();
let recursive = recursive.get("recursive").is_some();

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, Some(recursive))
jiahao6635 marked this conversation as resolved.
Show resolved Hide resolved
.map_err(|e| ApiError::NotFoundFolder(e.to_string()))?
.ok_or(ApiError::NotFoundFolder("The folder is none".to_string()))
}
Expand Down
11 changes: 8 additions & 3 deletions src/services/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use uuid::Uuid;
use crate::context::Context;
use crate::errors::ApiError;
use crate::errors::Result;
use crate::requests::playbook::CreatePlaybookRequest;
use crate::requests::playbook::{CreatePlaybookRequest, GitReferenceMethods};
use crate::utils::repo;

pub struct PlaybookService;
Expand All @@ -31,8 +31,13 @@ impl PlaybookService {
pub async fn create(ctx: Arc<Context>, req: &CreatePlaybookRequest) -> Result<PlaybookSpec> {
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(GitReferenceMethods::new(req.repo.clone(), req.reference.clone())),
jiahao6635 marked this conversation as resolved.
Show resolved Hide resolved
..Preface::default()
};
let payload = PlaybookPayload { title: repo, description, preface };

ctx.client.playbooks().create(payload).map_err(ApiError::FailedToCreatePlaybook)
}
Expand Down