verctl-core 0.9.5

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
Documentation
//! Which machines run PR and push validation.
//!
//! A `ci` entry is one job; its `runners` name machines from `runners`, one
//! check each. Write nothing and one `verify` job runs on `ubuntu-26.04`,
//! which is what every repo in this family did before the section existed.
//!
//! GitHub needs `runs-on` before a job exists, so a job cannot read its own
//! machine from verctl. A small `plan` job emits this matrix and the real jobs
//! consume it, the same bootstrap the publish lane uses for assets.

use std::path::Path;

use anyhow::{Context, Result};
use serde::Serialize;

use crate::config::Config;
use crate::{env, runners};

const DEFAULT_JOB: &str = "verify";
const DEFAULT_MACHINE: &str = "ubuntu-26.04";
const DEFAULT_LABELS: &[&str] = &["ubuntu-26.04"];

/// One row of a GitHub Actions `strategy.matrix`, and one check.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CiJob {
    /// The job as declared. Rows share it when the job fans out.
    pub id: String,
    /// The check name, unique across rows.
    pub name: String,
    /// The machine this row runs on, as declared.
    pub machine: String,
    /// Straight into `runs-on:`.
    pub labels: Vec<String>,
}

/// A GitHub Actions `strategy.matrix`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Matrix {
    /// One row per check.
    pub include: Vec<CiJob>,
}

/// The validation jobs to run, as the workflow reads them.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CiPlan {
    /// The matrix the workflow fans out over.
    pub matrix: Matrix,
}

/// Plan the jobs declared under `ci`, or one `verify` job when none is
/// declared.
///
/// # Errors
///
/// Returns an error when a declared runner is invalid, or when a job names a
/// machine that is missing, repeated, or not declared under `runners`.
pub fn plan(config: &Config) -> Result<CiPlan> {
    runners::declared(config)?;
    let mut include = Vec::new();
    if config.ci.is_empty() {
        let resolved = machines(config, DEFAULT_JOB, None)?;
        push(&mut include, DEFAULT_JOB, &resolved);
        return Ok(CiPlan {
            matrix: Matrix { include },
        });
    }
    for (id, job) in &config.ci {
        let resolved = machines(config, id, job.runners.as_ref())?;
        push(&mut include, id, &resolved);
    }
    Ok(CiPlan {
        matrix: Matrix { include },
    })
}

fn machines(
    config: &Config,
    id: &str,
    named: Option<&Vec<String>>,
) -> Result<Vec<runners::Machine>> {
    runners::of(config, "ci job", id, named, DEFAULT_MACHINE, DEFAULT_LABELS)
}

fn push(include: &mut Vec<CiJob>, id: &str, resolved: &[runners::Machine]) {
    for machine in resolved {
        include.push(CiJob {
            id: id.to_owned(),
            name: runners::check_name(id, &machine.name, resolved),
            machine: machine.name.clone(),
            labels: machine.labels.clone(),
        });
    }
}

/// Write `matrix` as a JSON line for `$GITHUB_OUTPUT`.
///
/// # Errors
///
/// Returns an error when the matrix cannot be encoded as JSON, or when the
/// file at `path` cannot be opened or written.
pub fn write_github_output(plan: &CiPlan, path: &Path) -> Result<()> {
    let encoded = serde_json::to_string(&plan.matrix).context("encode matrix")?;
    env::write_github_output(path, &format!("matrix={encoded}\n"))
}

#[cfg(test)]
mod tests;