Skip to content

Commit

Permalink
chore(deps): upgrade amp-common and amp-client
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Jan 30, 2025
1 parent 046d078 commit 0e28cdf
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 71 deletions.
89 changes: 37 additions & 52 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playground-api"
version = "0.3.3"
version = "0.4.0"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/amphitheatre-app/playground-api"
Expand All @@ -14,8 +14,8 @@ name = "playground"
path = "src/lib.rs"

[dependencies]
amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.9.5" }
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.9.6" }
amp-client = { git = "https://github.com/amphitheatre-app/amp-client-rust", tag = "v0.10.1" }
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.10.3" }
anyhow = "1.0.95"
axum = { version = "0.8.1" }
clap = { version = "4.5.27", features = ["derive", "env"] }
Expand Down
7 changes: 6 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ pub struct Context {

impl Context {
pub async fn new(config: Config) -> anyhow::Result<Context> {
// Create amphitheatre client
let client = Arc::new(Client::new(&config.amp_server, config.auth_token.clone()));
let github_client = Arc::new(ScmClient::new(github::new(GITHUB_ENDPOINT, config.auth_token.clone())));

// Create SCM client with GitHub driver
let github_driver = github::new(GITHUB_ENDPOINT, config.auth_token.clone())?;
let github_client = Arc::new(ScmClient::new(github_driver));

Ok(Context { config, client, github_client })
}
}
3 changes: 2 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use amp_common::http::HTTPError;
use amp_common::scm::errors::SCMError;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::Json;
Expand Down Expand Up @@ -58,7 +59,7 @@ pub enum ApiError {
BadPlaybook(String),

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

#[error("Bad Playbook Request: {0}")]
BadPlaybookRequest(String),
Expand Down
15 changes: 10 additions & 5 deletions src/services/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ pub struct FileService;
impl FileService {
/// Get a file content from the remote git repository.
pub async fn get(ctx: Arc<Context>, id: Uuid, path: String) -> Result<Content> {
let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?;
let playbook = ctx.client.playbooks().get(&id.to_string()).await.map_err(ApiError::NotFoundPlaybook)?;
let source = playbook.preface.repository.unwrap();

ctx.github_client
.contents()
.find(&utils::repo(&source.repo)?, &path, &source.branch.unwrap_or_default())
.await
.map_err(|e| ApiError::NotFoundContent(e.to_string()))
}

Expand All @@ -48,7 +49,7 @@ impl FileService {
};

// Call sync() method to create file.
FileService::sync(ctx, id, req)?;
FileService::sync(ctx, id, req).await?;

// We build a content object to return, because it's just a temporary file on the workspace.
Ok(Content { path, data, sha: String::new(), blob_id: String::new() })
Expand Down Expand Up @@ -79,14 +80,18 @@ impl FileService {
}

/// Sync to the workspace.
fn sync(ctx: Arc<Context>, id: Uuid, req: Synchronization) -> Result<u16> {
let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?;
async fn sync(ctx: Arc<Context>, id: Uuid, req: Synchronization) -> Result<u16> {
let playbook = ctx.client.playbooks().get(&id.to_string()).await.map_err(ApiError::NotFoundPlaybook)?;

debug!("update playbooks in {}...", id);

if let Some(characters) = playbook.characters {
let character = characters.first().unwrap();
ctx.client.actors().sync(&id.to_string(), &character.meta.name, req).map_err(ApiError::FailedToSynchronize)
ctx.client
.actors()
.sync(&id.to_string(), &character.meta.name, req)
.await
.map_err(ApiError::FailedToSynchronize)
} else {
Err(ApiError::BadPlaybook("The playbook has no characters".to_string()))
}
Expand Down
6 changes: 4 additions & 2 deletions src/services/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@ pub struct FolderService;

impl FolderService {
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 playbook = ctx.client.playbooks().get(&id.to_string()).await.map_err(ApiError::NotFoundPlaybook)?;

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, &reference)
.await
.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 playbook = ctx.client.playbooks().get(&id.to_string()).await.map_err(ApiError::NotFoundPlaybook)?;

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)?, &reference, Option::from(recursive.is_some()))
.await
.map_err(|e| ApiError::NotFoundFolder(e.to_string()))?
.ok_or(ApiError::NotFoundFolder("The folder is none".to_string()))
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct LoggerService;

impl LoggerService {
pub async fn logs(ctx: Arc<Context>, id: Uuid) -> Result<EventSource, ApiError> {
let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?;
let playbook = ctx.client.playbooks().get(&id.to_string()).await.map_err(ApiError::NotFoundPlaybook)?;

if let Some(characters) = playbook.characters {
let character = characters.first().unwrap();
Expand Down
Loading

0 comments on commit 0e28cdf

Please sign in to comment.