Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions tools/ci/src/workflow_watch.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
use anyhow::{bail, Context, Result};
use duct::cmd;
use serde::Deserialize;
use std::process::Command;

#[derive(Deserialize)]
struct WorkflowRunView {
status: String,
conclusion: Option<String>,
jobs: Vec<WorkflowJobView>,
}

#[derive(Deserialize)]
struct WorkflowJobView {
name: String,
status: String,
conclusion: Option<String>,
}

fn get_workflow_run(repo: &str, run_id: u64) -> Result<WorkflowRunView> {
let raw = cmd!(
"gh",
"run",
"view",
run_id.to_string(),
"--repo",
repo,
"--json",
"status,conclusion,jobs",
)
.read()
.with_context(|| format!("failed to read workflow run {run_id} in {repo}"))?;
serde_json::from_str(&raw).with_context(|| format!("failed to parse workflow run {run_id} in {repo}"))
}
let path = format!("repos/{repo}/actions/runs/{run_id}");
let output = Command::new("gh")
.args(["api", "--include", &path])
.output()
.with_context(|| format!("failed to run gh api for workflow run {run_id} in {repo}"))?;

fn print_workflow_job_summary(run: &WorkflowRunView) {
println!("Job summary:");
for job in &run.jobs {
let result = job.conclusion.as_deref().unwrap_or(&job.status);
println!(" {result:>11} {}", job.name);
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
if !output.status.success() {
eprintln!("gh api failed while reading workflow run {run_id} in {repo}:");
if !stdout.is_empty() {
eprintln!("--- gh api stdout ---\n{stdout}");
}
if !stderr.is_empty() {
eprintln!("--- gh api stderr ---\n{stderr}");
}
bail!("gh api {path} exited with {}", output.status);
}

let body = response_body(&stdout);
serde_json::from_str(body).with_context(|| format!("failed to parse workflow run {run_id} in {repo}"))
}

fn response_body(response: &str) -> &str {
response
.rsplit_once("\r\n\r\n")
.or_else(|| response.rsplit_once("\n\n"))
.map_or(response, |(_, body)| body)
}

pub(crate) fn watch_workflow_run(
Expand All @@ -53,7 +52,6 @@ pub(crate) fn watch_workflow_run(
attempts += 1;
let run = get_workflow_run(repo, run_id)?;
if run.status == "completed" {
print_workflow_job_summary(&run);
let conclusion = run.conclusion.as_deref().unwrap_or("success");
if conclusion == "success" {
return Ok(());
Expand Down
Loading