Skip to content

Commit

Permalink
Add functions to revert a release and an entire package
Browse files Browse the repository at this point in the history
  • Loading branch information
Pi-Cla authored and lpil committed May 1, 2024
1 parent 507abc2 commit 1b0d28d
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,64 @@ pub fn publish_package_response(response: http::Response<Vec<u8>>) -> Result<(),
}
}

pub fn revert_release_request(
package_name: &str,
version: &str,
api_key: &str,
config: &Config,
) -> Result<http::Request<Vec<u8>>, ApiError> {
validate_package_and_version(package_name, version)?;

Ok(config
.api_request(
Method::DELETE,
&format!("packages/{}/releases/{}", package_name, version),
Some(api_key),
)
.body(vec![])
.expect("publish_package_request request"))
}

pub fn revert_release_response(response: http::Response<Vec<u8>>) -> Result<(), ApiError> {
let (parts, body) = response.into_parts();
match parts.status {
StatusCode::NO_CONTENT => Ok(()),
StatusCode::NOT_FOUND => Err(ApiError::NotFound),
StatusCode::TOO_MANY_REQUESTS => Err(ApiError::RateLimited),
StatusCode::UNAUTHORIZED => Err(ApiError::InvalidApiKey),
StatusCode::FORBIDDEN => Err(ApiError::Forbidden),
status => Err(ApiError::unexpected_response(status, body)),
}
}

pub fn revert_package_request(
package_name: &str,
owner: &str,
api_key: &str,
config: &Config,
) -> Result<http::Request<Vec<u8>>, ApiError> {
Ok(config
.api_request(
Method::DELETE,
&format!("packages/{}/owners/{}", package_name, owner),
Some(api_key),
)
.body(vec![])
.expect("publish_package_request request"))
}

pub fn revert_package_response(response: http::Response<Vec<u8>>) -> Result<(), ApiError> {
let (parts, body) = response.into_parts();
match parts.status {
StatusCode::NO_CONTENT => Ok(()),
StatusCode::NOT_FOUND => Err(ApiError::NotFound),
StatusCode::TOO_MANY_REQUESTS => Err(ApiError::RateLimited),
StatusCode::UNAUTHORIZED => Err(ApiError::InvalidApiKey),
StatusCode::FORBIDDEN => Err(ApiError::Forbidden),
status => Err(ApiError::unexpected_response(status, body)),
}
}

#[derive(Error, Debug)]
pub enum ApiError {
#[error(transparent)]
Expand Down
66 changes: 66 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,72 @@ async fn remove_docs_success() {
mock.assert();
}

#[tokio::test]
async fn revert_release_success() {
let key = "my-api-key-here";
let package = "gleam_experimental_stdlib";
let version = "0.8.0";

let mut server = mockito::Server::new_async().await;
let mock = server
.mock(
"DELETE",
format!("/packages/{}/releases/{}", package, version).as_str(),
)
.expect(1)
.match_header("authorization", key)
.match_header("accept", "application/json")
.with_status(204)
.create_async()
.await;

let mut config = Config::new();
config.api_base = http::Uri::try_from(server.url()).unwrap();

let result = crate::revert_release_response(
http_send(crate::revert_release_request(package, version, key, &config).unwrap())
.await
.unwrap(),
)
.unwrap();

assert_eq!(result, ());
mock.assert();
}

#[tokio::test]
async fn revert_package_success() {
let key = "my-api-key-here";
let package = "gleam_experimental_stdlib";
let owner = "lpil";

let mut server = mockito::Server::new_async().await;
let mock = server
.mock(
"DELETE",
format!("/packages/{}/owners/{}", package, owner).as_str(),
)
.expect(1)
.match_header("authorization", key)
.match_header("accept", "application/json")
.with_status(204)
.create_async()
.await;

let mut config = Config::new();
config.api_base = http::Uri::try_from(server.url()).unwrap();

let result = crate::revert_package_response(
http_send(crate::revert_package_request(package, owner, key, &config).unwrap())
.await
.unwrap(),
)
.unwrap();

assert_eq!(result, ());
mock.assert();
}

#[tokio::test]
async fn remove_key_success() {
let name = "some-key-name";
Expand Down

0 comments on commit 1b0d28d

Please sign in to comment.