hive-forge: ci-log errors on a --job past the run's last job
This commit is contained in:
parent
be2a406818
commit
e44cbea4b6
1 changed files with 60 additions and 4 deletions
|
|
@ -57,6 +57,30 @@ fn steps_of(view: &Value) -> Vec<Value> {
|
|||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The number of jobs in the run, from a run-view response
|
||||
/// (`state.run.jobs`); `None` when the response does not list them.
|
||||
fn job_count(view: &Value) -> Option<usize> {
|
||||
view.get("state")
|
||||
.and_then(|s| s.get("run"))
|
||||
.and_then(|r| r.get("jobs"))
|
||||
.and_then(Value::as_array)
|
||||
.map(Vec::len)
|
||||
.filter(|&n| n > 0)
|
||||
}
|
||||
|
||||
/// A usage error for a `--job` past the run's last job. The web routes clamp
|
||||
/// such an index to job 0 and answer 200, so without this the log of job 0
|
||||
/// would print under a header naming the requested job.
|
||||
fn job_out_of_range(run: u64, job: u64, count: usize) -> Option<String> {
|
||||
let in_range = usize::try_from(job).is_ok_and(|j| j < count);
|
||||
(!in_range).then(|| {
|
||||
format!(
|
||||
"job {job} out of range - run #{run} has {count} job(s) (0..{})",
|
||||
count - 1
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Best-effort display name for a step. Forgejo's `ViewJobStep` carries
|
||||
/// the name in `summary`; fall back to `name` then a generic label so a
|
||||
/// schema tweak doesn't blank the header.
|
||||
|
|
@ -209,14 +233,24 @@ fn persisted_logs(client: &Client, repo: &str, args: &Args) -> Result<bool> {
|
|||
/// (network, an unknown run number, or a non-2xx), if a requested `--step`
|
||||
/// is out of range, or if both sources yield no log content.
|
||||
///
|
||||
/// An out-of-range `--job` is **not** an error: the web routes clamp it to
|
||||
/// job 0 and answer 200, and establishing the real job count costs an extra
|
||||
/// two API calls on every indexed read. [`persisted_logs`] names the job the
|
||||
/// server actually served instead, from a header already in the response.
|
||||
/// An out-of-range `--job` is an error naming the run's job count: the web
|
||||
/// routes clamp it to job 0 and answer 200, so the run view's job list
|
||||
/// (one extra request, made only when `--job` is not the default 0) is what
|
||||
/// tells them apart. When that request fails or lists no jobs the read goes
|
||||
/// ahead, and [`persisted_logs`] still names the job the server actually
|
||||
/// served, from a header already in the response.
|
||||
pub fn run(client: &Client, args: Args) -> Result<()> {
|
||||
let repo = client.repo()?;
|
||||
let stream_path = format!("/{repo}/actions/runs/{}/jobs/{}", args.run, args.job);
|
||||
|
||||
if args.job > 0
|
||||
&& let Ok(view) = client.post_json_web(&stream_path, &json!({ "logCursors": [] }))
|
||||
&& let Some(count) = job_count(&view)
|
||||
&& let Some(msg) = job_out_of_range(args.run, args.job, count)
|
||||
{
|
||||
bail!(msg);
|
||||
}
|
||||
|
||||
// `--step` only has meaning against the live streamer's per-step
|
||||
// framing (the persisted download is a flat whole-job log) — honor it
|
||||
// directly instead of preferring the persisted route.
|
||||
|
|
@ -306,6 +340,28 @@ mod tests {
|
|||
assert_eq!(step_name(steps.get(1)), "Run tests");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_job_past_the_last_is_out_of_range_and_names_the_count() {
|
||||
let view = serde_json::json!({
|
||||
"state": { "run": { "jobs": [{}, {}, {}] } }
|
||||
});
|
||||
let count = job_count(&view).expect("three jobs");
|
||||
assert_eq!(count, 3);
|
||||
assert_eq!(job_out_of_range(8265, 2, count), None);
|
||||
let msg = job_out_of_range(8265, 3, count).expect("past the last");
|
||||
assert!(msg.contains("has 3 job(s) (0..2)"), "{msg}");
|
||||
assert!(job_out_of_range(8265, u64::MAX, count).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_view_without_a_job_list_has_no_count() {
|
||||
assert_eq!(job_count(&serde_json::json!({})), None);
|
||||
assert_eq!(
|
||||
job_count(&serde_json::json!({ "state": { "run": { "jobs": [] } } })),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn steps_of_missing_is_empty() {
|
||||
assert!(steps_of(&serde_json::json!({})).is_empty());
|
||||
|
|
|
|||
Loading…
Reference in a new issue