diff --git a/crates/forge-github/Cargo.toml b/crates/forge-github/Cargo.toml index b8bb1a79..2498c259 100644 --- a/crates/forge-github/Cargo.toml +++ b/crates/forge-github/Cargo.toml @@ -6,6 +6,15 @@ license = { workspace = true } repository = { workspace = true } description = "GitHub support for git-next, the trunk-based development manager" +[lints.clippy] +nursery = { level = "warn", priority = -1 } +pedantic = { level = "warn", priority = -1 } +unwrap_used = "warn" +expect_used = "warn" + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] } + [dependencies] git-next-core = { workspace = true } @@ -43,12 +52,3 @@ tokio = { workspace = true } # Testing assert2 = { workspace = true } rand = { workspace = true } - -[lints.clippy] -nursery = { level = "warn", priority = -1 } -# pedantic = "warn" -unwrap_used = "warn" -expect_used = "warn" - -[lints.rust] -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] } diff --git a/crates/forge-github/src/commit.rs b/crates/forge-github/src/commit.rs index a5520082..bee1db6b 100644 --- a/crates/forge-github/src/commit.rs +++ b/crates/forge-github/src/commit.rs @@ -6,7 +6,7 @@ use kxio::network; /// Checks the results of any (e.g. CI) status checks for the commit. /// -/// GitHub: https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#list-commit-statuses-for-a-reference +/// GitHub: pub async fn status(github: &github::Github, commit: &git::Commit) -> git::forge::commit::Status { let repo_details = &github.repo_details; let hostname = repo_details.forge.hostname(); @@ -33,17 +33,13 @@ pub async fn status(github: &github::Github, commit: &git::Commit) -> git::forge .into_iter() .map(|status| match status.state { GithubState::Success => Status::Pass, - GithubState::Pending => Status::Pending, - GithubState::Failure => Status::Fail, - GithubState::Error => Status::Fail, - GithubState::Blank => Status::Pending, + GithubState::Pending | GithubState::Blank => Status::Pending, + GithubState::Failure | GithubState::Error => Status::Fail, }) .reduce(|l, r| match (l, r) { (Status::Pass, Status::Pass) => Status::Pass, - (_, Status::Fail) => Status::Fail, - (Status::Fail, _) => Status::Fail, - (_, Status::Pending) => Status::Pending, - (Status::Pending, _) => Status::Pending, + (_, Status::Fail) | (Status::Fail, _) => Status::Fail, + (_, Status::Pending) | (Status::Pending, _) => Status::Pending, }) }) .unwrap_or_else(|| { diff --git a/crates/forge-github/src/tests/mod.rs b/crates/forge-github/src/tests/mod.rs index 7b96f0c7..a240a458 100644 --- a/crates/forge-github/src/tests/mod.rs +++ b/crates/forge-github/src/tests/mod.rs @@ -231,7 +231,7 @@ mod github { let forge = given::a_github_forge(&repo_details, net); - let_assert!(Ok(_) = forge.unregister_webhook(&webhook_id).await); + let_assert!(Ok(()) = forge.unregister_webhook(&webhook_id).await); } #[tokio::test] async fn should_return_error_on_network_error() { @@ -335,11 +335,11 @@ mod github { hostname, repo_path, }; - let hook1 = with::ReturnedWebhook::new(given::a_github_webhook_id(), &repo_listen_url); - let hook2 = with::ReturnedWebhook::new(given::a_github_webhook_id(), &repo_listen_url); - let hook3 = + let hook_1 = with::ReturnedWebhook::new(given::a_github_webhook_id(), &repo_listen_url); + let hook_2 = with::ReturnedWebhook::new(given::a_github_webhook_id(), &repo_listen_url); + let hook_3 = with::ReturnedWebhook::new(given::a_github_webhook_id(), &given::any_webhook_url()); - let hooks = [hook1, hook2, hook3]; + let hooks = [hook_1, hook_2, hook_3]; // there are three existing webhooks, two are matching webhooks with::get_webhooks_by_page(1, &hooks, &mut args); with::get_webhooks_by_page(2, &[], &mut args); @@ -511,7 +511,7 @@ mod github { a_commit_status_url(repo_details, commit).as_str(), StatusCode::OK, response.to_string().as_str(), - ) + ); } pub fn a_commit_status_url( diff --git a/crates/forge-github/src/webhook/authorised.rs b/crates/forge-github/src/webhook/authorisation.rs similarity index 82% rename from crates/forge-github/src/webhook/authorised.rs rename to crates/forge-github/src/webhook/authorisation.rs index 58aae38b..8b1a19d2 100644 --- a/crates/forge-github/src/webhook/authorised.rs +++ b/crates/forge-github/src/webhook/authorisation.rs @@ -1,17 +1,20 @@ // +use std::string::ToString; + use git_next_core::{ForgeNotification, WebhookAuth}; +use hmac::Mac; +type HmacSha256 = hmac::Hmac; + pub fn is_authorised(msg: &ForgeNotification, webhook_auth: &WebhookAuth) -> bool { msg.header("x-hub-signature-256") .map(|x| x.trim_matches('"').to_string()) - .and_then(|sha| sha.strip_prefix("sha256=").map(|k| k.to_string())) + .and_then(|sha| sha.strip_prefix("sha256=").map(ToString::to_string)) .and_then(|github_signature| hex::decode(github_signature).ok()) .and_then(|gh_sig| { let payload = &msg.body().as_str(); - use hmac::Mac; - type HmacSha256 = hmac::Hmac; let mut hmac = HmacSha256::new_from_slice(webhook_auth.to_string().as_bytes()).ok()?; hmac::Mac::update(&mut hmac, payload.as_ref()); Some(hmac::Mac::verify_slice(hmac, gh_sig.as_ref()).is_ok()) @@ -25,8 +28,6 @@ pub fn sign_body( body: &git_next_core::webhook::forge_notification::Body, ) -> Option { let payload = body.as_str(); - use hmac::Mac; - type HmacSha256 = hmac::Hmac; let mut hmac = HmacSha256::new_from_slice(webhook_auth.to_string().as_bytes()).ok()?; hmac::Mac::update(&mut hmac, payload.as_ref()); let f = hmac::Mac::finalize(hmac); diff --git a/crates/forge-github/src/webhook/mod.rs b/crates/forge-github/src/webhook/mod.rs index bbf9709a..d9d5d0f0 100644 --- a/crates/forge-github/src/webhook/mod.rs +++ b/crates/forge-github/src/webhook/mod.rs @@ -1,20 +1,20 @@ // use git_next_core::{git, webhook, ApiToken, BranchName}; -mod authorised; +mod authorisation; mod list; -mod parse; +mod parser; mod register; mod unregister; -pub use authorised::is_authorised; +pub use authorisation::is_authorised; pub use list::list; -pub use parse::parse_body; +pub use parser::parse_body; pub use register::register; pub use unregister::unregister; #[cfg(test)] -pub use authorised::sign_body; +pub use authorisation::sign_body; pub fn headers(token: &ApiToken) -> kxio::network::NetRequestHeaders { use secrecy::ExposeSecret; diff --git a/crates/forge-github/src/webhook/parse.rs b/crates/forge-github/src/webhook/parser.rs similarity index 100% rename from crates/forge-github/src/webhook/parse.rs rename to crates/forge-github/src/webhook/parser.rs