use super::status::{Fetched, PrStatus, State};
use crate::entire::{Availability, Brief, Entire};
fn wanted(pr: &PrStatus) -> bool {
matches!(pr.state, State::Open | State::Draft)
&& pr
.entire
.as_ref()
.is_some_and(|e| e.gates.fails("findings") || !e.gates.read())
}
const FROM_TRAIL: &str = "jouet could not read the Entire Gates check's summary, so its gates come from `entire trail show`";
pub(crate) fn add(prs: &mut [Fetched]) -> Option<Availability> {
if !prs.iter_mut().any(|p| wanted(p.status_mut())) {
return None;
}
let probed = Entire::probe();
let availability = Availability::of(&probed);
let Ok(entire) = probed else {
return Some(availability);
};
std::thread::scope(|s| {
for pr in prs
.iter_mut()
.map(Fetched::status_mut)
.filter(|p| wanted(p))
{
let entire = &entire;
s.spawn(move || fill(entire, pr));
}
});
Some(availability)
}
fn fill(entire: &Entire, pr: &mut PrStatus) {
let Some(repo) = pr.repository() else {
return;
};
let name = format!("{}#{}", pr.repo, pr.number);
let head = pr.head.clone();
let Some(status) = pr.entire.as_mut() else {
return;
};
let mut read = || -> Result<(), String> {
let Some(trail) = entire.trail(&repo, status.gates.trail, &head)? else {
return Ok(());
};
if !status.gates.read() && !trail.gates.is_empty() {
status.gates.gates = trail.gates.clone();
status.gates.trail = Some(trail.number);
status.note = Some(FROM_TRAIL.into());
}
if !status.gates.fails("findings") {
return Ok(());
}
let found = entire.findings(&repo, trail.number, false)?;
let open: Vec<_> = found.findings.into_iter().filter(|f| f.is_open()).collect();
status.findings = Brief::of(&open);
status.findings_incomplete = found.incomplete;
Ok(())
};
if let Err(e) = read() {
eprintln!("jouet: entire could not describe {name}: {e}");
status.error = Some(e);
}
}