git-next/crates/forge-github/src/commit.rs
Paul Campbell ad358ad7c2
All checks were successful
Rust / build (push) Successful in 2m17s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
Release Please / Release-plz (push) Successful in 37s
refactor: cleanup pedantic clippy in forge-github crate
2024-08-06 16:21:25 +01:00

54 lines
2.2 KiB
Rust

//
use crate::{self as github, GithubState};
use git_next_core::git::{self, forge::commit::Status};
use github::GithubStatus;
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>
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();
let repo_path = &repo_details.repo_path;
let url = network::NetUrl::new(format!(
"https://api.{hostname}/repos/{repo_path}/commits/{commit}/statuses"
));
let headers = github::webhook::headers(repo_details.forge.token());
let request = network::NetRequest::new(
network::RequestMethod::Get,
url,
headers,
network::RequestBody::None,
network::ResponseType::Json,
None,
network::NetRequestLogging::None,
);
let result = github.net.get::<Vec<GithubStatus>>(request).await;
match result {
Ok(response) => response
.response_body()
.and_then(|statuses| {
statuses
.into_iter()
.map(|status| match status.state {
GithubState::Success => Status::Pass,
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::Pending) | (Status::Pending, _) => Status::Pending,
})
})
.unwrap_or_else(|| {
tracing::warn!("No status checks configured for 'next' branch",);
Status::Pass
}),
Err(e) => {
tracing::warn!(?e, "Failed to get commit status");
Status::Pending // assume issue is transient and allow retry
}
}
}