diff --git a/tools/ci/commands/workflow-coordinator/src/main.rs b/tools/ci/commands/workflow-coordinator/src/main.rs index 6b76deae096..3748c60143d 100644 --- a/tools/ci/commands/workflow-coordinator/src/main.rs +++ b/tools/ci/commands/workflow-coordinator/src/main.rs @@ -145,6 +145,13 @@ fn git_tree(repo: &str, sha: &str) -> Result { get(&format!("/repos/{repo}/git/trees/{sha}")) } +/// `matching-refs` matches by prefix and returns an empty list rather than a 404. +fn branch_exists(repo: &str, branch: &str) -> Result { + let refs: Vec = get(&format!("/repos/{repo}/git/matching-refs/heads/{branch}"))?; + let name = format!("refs/heads/{branch}"); + Ok(refs.iter().any(|git_ref| git_ref.name == name)) +} + /// Returns runs for an exact event and private SHA. fn workflow_runs(event: &str, head_sha: &str) -> Result> { let path = format!( @@ -249,6 +256,12 @@ struct GitTreeEntry { sha: String, } +#[derive(Clone, Debug, Deserialize, PartialEq, Eq)] +struct GitRef { + #[serde(rename = "ref")] + name: String, +} + #[derive(Clone, Debug, Deserialize, PartialEq, Eq)] struct WorkflowRunsPage { workflow_runs: Vec, @@ -268,6 +281,7 @@ struct WorkflowRun { run_attempt: u64, html_url: String, created_at: String, + head_branch: Option, head_commit: Option, #[serde(default)] pull_requests: Vec, @@ -519,8 +533,19 @@ fn coordinate_merge_queue_reuse() -> Result<()> { return Ok(()); }; println!("Found equivalent merge queue run: {}", run.html_url); + let head_branch = run.head_branch.clone(); let mut selected = wait_for_completion(PUBLIC_REPO, run.into())?; if should_rerun_failed_jobs(&selected) { + // Rerun jobs check out the run's merge queue branch, which GitHub deletes once its entry leaves the queue. + let can_rerun = match head_branch.as_deref() { + Some(branch) => branch_exists(PUBLIC_REPO, branch)?, + None => false, + }; + if !can_rerun { + println!("Its merge queue branch no longer exists, so its jobs can't be rerun. Running CI normally."); + write_github_output("reused", false)?; + return Ok(()); + } selected = wait_for_completion(PUBLIC_REPO, rerun_failed_jobs(PUBLIC_REPO, &selected)?)?; } write_github_output("failed", selected.conclusion.as_deref() != Some("success"))?; @@ -612,6 +637,7 @@ mod tests { run_attempt: 1, html_url: format!("https://example.test/{id}"), created_at: created_at.to_owned(), + head_branch: None, head_commit: Some(WorkflowRunCommit { tree_id: "tree".to_owned(), }),