Skip to content

Commit

Permalink
move conflict handling out of client into main
Browse files Browse the repository at this point in the history
  • Loading branch information
colemickens committed May 8, 2024
1 parent 2b0e573 commit 2c53fac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 63 deletions.
60 changes: 4 additions & 56 deletions src/flakehub_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use color_eyre::eyre::{eyre, Context, Result};
use http::StatusCode;
use reqwest::header::HeaderMap;
use reqwest::Response;
use uuid::Uuid;

use crate::release_metadata::ReleaseMetadata;
use crate::Error;

pub struct FlakeHubClient {
host: url::Url,
Expand Down Expand Up @@ -59,8 +59,7 @@ impl FlakeHubClient {
release_version: &str,
release_metadata: &ReleaseMetadata,
tarball: &Tarball,
error_if_release_conflicts: bool,
) -> Result<Option<StageResult>> {
) -> Result<Response> {
let flake_tarball_len = tarball.bytes.len();
let flake_tarball_hash_base64 = &tarball.hash_base64;
let relative_url: &String = &format!("upload/{upload_name}/{release_version}/{flake_tarball_len}/{flake_tarball_hash_base64}");
Expand All @@ -76,66 +75,15 @@ impl FlakeHubClient {
tracing::debug!("repo={}", release_metadata.repo);
tracing::debug!("upload_name={}", upload_name);

let response = self
self
.client
.post(release_metadata_post_url)
.bearer_auth(&self.bearer_token)
.headers(flakehub_headers())
.json(&release_metadata)
.send()
.await
.unwrap();

let release_metadata_post_response_status = response.status();
tracing::trace!(
status = tracing::field::display(release_metadata_post_response_status),
"Got release metadata POST response"
);

match release_metadata_post_response_status {
StatusCode::OK => (),
StatusCode::CONFLICT => {
tracing::info!(
"Release for revision `{revision}` of {upload_name}/{release_version} already exists; flakehub-push will not upload it again",
revision = &release_metadata.revision,
upload_name = upload_name,
release_version = &release_version,
);
if error_if_release_conflicts {
return Err(Error::Conflict {
upload_name: upload_name.to_string(),
release_version: release_version.to_string(),
})?;
} else {
// we're just done, and happy about it:
return Ok(None);
}
}
StatusCode::UNAUTHORIZED => {
let body = response.bytes().await?;
let message = serde_json::from_slice::<String>(&body)?;

return Err(Error::Unauthorized(message))?;
}
_ => {
let body = response.bytes().await?;
let message = serde_json::from_slice::<String>(&body)?;
return Err(eyre!(
"\
Status {release_metadata_post_response_status} from metadata POST\n\
{}\
",
message
));
}
}

let stage_result: StageResult = response
.json()
.await
.wrap_err("Decoding release metadata POST response")?;

Ok(Some(stage_result))
.wrap_err("Publishing release")
}

pub async fn release_publish(&self, release_uuidv7: Uuid) -> Result<()> {
Expand Down
62 changes: 55 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{fmt::Display, io::IsTerminal, process::ExitCode};

use clap::Parser;
use color_eyre::eyre::Result;
use color_eyre::eyre::{eyre, Result};
use error::Error;
use http::StatusCode;

use crate::{
flakehub_client::{FlakeHubClient, StageResult},
Expand Down Expand Up @@ -72,19 +73,66 @@ async fn execute() -> Result<std::process::ExitCode> {
let fhclient = FlakeHubClient::new(ctx.flakehub_host, ctx.auth_token)?;

// "upload.rs" - stage the release
let stage_result: Option<StageResult> = fhclient
let stage_result = fhclient
.release_stage(
&ctx.upload_name,
&ctx.release_version,
&ctx.metadata,
&ctx.tarball,
ctx.error_if_release_conflicts,
)
.await?;
.await;

let stage_result: StageResult = match stage_result {
Err(e) => { return Err(e)?; },
Ok(response) => {
let response_status = response.status();
let stage_result = match response_status {
StatusCode::OK => {
let stage_result: StageResult = response
.json()
.await
.map_err(|_| eyre!("Decoding release metadata POST response"))?;

stage_result
},
StatusCode::CONFLICT => {
tracing::info!(
"Release for revision `{revision}` of {upload_name}/{release_version} already exists; flakehub-push will not upload it again",
revision = &ctx.metadata.revision,
upload_name = ctx.upload_name,
release_version = &ctx.release_version,
);
if ctx.error_if_release_conflicts {
return Err(Error::Conflict {
upload_name: ctx.upload_name.to_string(),
release_version: ctx.release_version.to_string(),
})?;
} else {
// we're just done, and happy about it:
return Ok(ExitCode::SUCCESS);
}
}
StatusCode::UNAUTHORIZED => {
let body = response.bytes().await?;
let message = serde_json::from_slice::<String>(&body)?;

let stage_result = match stage_result {
Some(stage_result) => stage_result,
None => return Ok(ExitCode::SUCCESS),
return Err(Error::Unauthorized(message))?;
}
_ => {
let body = response.bytes().await?;
let message = serde_json::from_slice::<String>(&body)?;
return Err(eyre!(
"\
Status {} from metadata POST\n\
{}\
",
response_status,
message
));
}
};
stage_result
},
};

// upload tarball to s3
Expand Down

0 comments on commit 2c53fac

Please sign in to comment.