Skip to content

Commit

Permalink
Optimize the unwrap mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahao6635 committed Jan 22, 2024
1 parent 7c95dd4 commit f9900f3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
5 changes: 1 addition & 4 deletions src/handlers/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ use crate::services::FolderService;
),
tag = "Folders"
)]
pub async fn get(
State(ctx): State<Arc<Context>>,
Path((id, path)): Path<(Uuid, Option<String>)>,
) -> Result<impl IntoResponse> {
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?))
}

Expand Down
15 changes: 10 additions & 5 deletions src/services/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,32 @@ use amp_common::scm::git::Tree;
use crate::context::Context;
use crate::errors::{ApiError, Result};
use crate::utils;
use crate::utils::unwrap_or_error;

pub struct FolderService;

impl FolderService {
pub async fn get(ctx: Arc<Context>, id: Uuid, path: Option<String>) -> Result<Vec<File>, ApiError> {
pub async fn get(ctx: Arc<Context>, id: Uuid, path: String) -> Result<Vec<File>, ApiError> {
let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?;
let source = playbook.preface.repository.unwrap();

let source = unwrap_or_error(playbook.preface.repository, "The repository is none")?;
let reference = unwrap_or_error(source.reference(), "The reference is none")?;

ctx.github_client
.contents()
.list(&utils::repo(&source.repo)?, &path.unwrap_or_default(), &source.reference().unwrap())
.list(&utils::repo(&source.repo)?, &path, &reference)
.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();

let source = unwrap_or_error(playbook.preface.repository, "The repository is none")?;
let reference = unwrap_or_error(source.reference(), "The reference is none")?;

ctx.github_client
.git()
.get_tree(&utils::repo(&source.repo)?, &source.reference().unwrap(), Option::from(recursive.is_some()))
.get_tree(&utils::repo(&source.repo)?, &reference, 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
4 changes: 1 addition & 3 deletions src/services/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ impl PlaybookService {
..GitReference::default()
};
if repository.reference().is_none() {
return Err(ApiError::BadPlaybookRequest(
"branch tag rev All three fields cannot be empty, as long as one of them has a value".to_string(),
));
return Err(ApiError::BadPlaybookRequest("Requires either branch, tag or rev".to_string()));
}
let preface = Preface { name: req.repo.clone(), repository: Some(repository), ..Preface::default() };
let payload = PlaybookPayload { title: repo, description, preface };
Expand Down
6 changes: 6 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ pub fn repo(url: &str) -> Result<String> {

Ok(repo)
}
pub fn unwrap_or_error<T>(option: Option<T>, error_message: &str) -> Result<T, ApiError> {
match option {
Some(value) => Ok(value),
None => Err(ApiError::BadPlaybookRequest(error_message.to_string())),
}
}

0 comments on commit f9900f3

Please sign in to comment.