2024-05-31 07:37:40 +01:00
|
|
|
//
|
|
|
|
use crate::{self as github, GithubState};
|
|
|
|
use git::forge::commit::Status;
|
|
|
|
use git_next_git as git;
|
2024-06-04 18:30:11 +01:00
|
|
|
use github::GithubStatus;
|
2024-05-31 07:37:40 +01:00
|
|
|
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;
|
2024-06-04 18:30:11 +01:00
|
|
|
let hostname = repo_details.forge.hostname();
|
2024-05-31 07:37:40 +01:00
|
|
|
let repo_path = &repo_details.repo_path;
|
|
|
|
let api_token = &repo_details.forge.token();
|
|
|
|
use secrecy::ExposeSecret;
|
|
|
|
let token = api_token.expose_secret();
|
|
|
|
let url = network::NetUrl::new(format!(
|
2024-06-04 18:30:11 +01:00
|
|
|
"https://api.{hostname}/repos/{repo_path}/commits/{commit}/statuses"
|
2024-05-31 07:37:40 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
let headers = network::NetRequestHeaders::new()
|
|
|
|
.with("X-GitHub-Api-Version", "2022-11-28")
|
|
|
|
.with("Authorization", format!("Bearer: {token}").as_str());
|
|
|
|
|
|
|
|
let request = network::NetRequest::new(
|
|
|
|
network::RequestMethod::Get,
|
|
|
|
url,
|
|
|
|
headers,
|
|
|
|
network::RequestBody::None,
|
|
|
|
network::ResponseType::Json,
|
|
|
|
None,
|
|
|
|
network::NetRequestLogging::None,
|
|
|
|
);
|
2024-06-04 18:30:11 +01:00
|
|
|
let result = github.net.get::<Vec<GithubStatus>>(request).await;
|
2024-05-31 07:37:40 +01:00
|
|
|
match result {
|
2024-06-04 18:30:11 +01:00
|
|
|
Ok(response) => response
|
|
|
|
.response_body()
|
|
|
|
.and_then(|statuses| {
|
2024-05-31 07:37:40 +01:00
|
|
|
statuses
|
|
|
|
.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,
|
|
|
|
})
|
|
|
|
.reduce(|l, r| match (l, r) {
|
|
|
|
(Status::Pass, Status::Pass) => Status::Pass,
|
|
|
|
(_, Status::Fail) => Status::Fail,
|
|
|
|
(Status::Fail, _) => Status::Fail,
|
2024-06-04 08:25:22 +01:00
|
|
|
(_, Status::Pending) => Status::Pending,
|
2024-05-31 07:37:40 +01:00
|
|
|
(Status::Pending, _) => Status::Pending,
|
|
|
|
})
|
2024-06-04 18:30:11 +01:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
tracing::warn!("No status checks configured for 'next' branch",);
|
|
|
|
Status::Pass
|
|
|
|
}),
|
2024-05-31 07:37:40 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::warn!(?e, "Failed to get commit status");
|
|
|
|
Status::Pending // assume issue is transient and allow retry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|