Skip to content

Commit

Permalink
Merge pull request #40 from jiahao6635/master
Browse files Browse the repository at this point in the history
Impl playbook create method
  • Loading branch information
wangeguo authored Jan 19, 2024
2 parents 68e68d0 + da5dc0f commit 08d6933
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 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.2" }
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.2" }
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" }
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 @@ -56,6 +56,9 @@ pub enum ApiError {

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

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

impl IntoResponse for ApiError {
Expand All @@ -72,6 +75,7 @@ impl IntoResponse for ApiError {
Self::NotFoundFolder(e) => (StatusCode::NOT_FOUND, e.to_string()),
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()),
};

error!("{} - {}", status, message);
Expand Down
5 changes: 1 addition & 4 deletions src/requests/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use amp_common::resource::Preface;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct CreatePlaybookRequest {
pub title: String,
pub description: Option<String>,
pub preface: Preface,
pub repo: String,
}
13 changes: 7 additions & 6 deletions src/services/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use amp_client::playbooks::PlaybookPayload;
use amp_common::resource::PlaybookSpec;
use amp_common::resource::{PlaybookSpec, Preface};
use std::sync::Arc;
use tracing::{error, info};

Expand All @@ -23,16 +23,17 @@ use crate::context::Context;
use crate::errors::ApiError;
use crate::errors::Result;
use crate::requests::playbook::CreatePlaybookRequest;
use crate::utils::repo;

pub struct PlaybookService;

impl PlaybookService {
pub async fn create(ctx: Arc<Context>, req: &CreatePlaybookRequest) -> Result<PlaybookSpec> {
let payload = PlaybookPayload {
title: req.title.clone(),
description: req.description.clone().unwrap_or_default(),
preface: req.preface.clone(),
};
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) };

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

Expand Down

0 comments on commit 08d6933

Please sign in to comment.