Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ cargo test # wfc's own tests, and `check` of this repository
`check` also checks every workflow in `.github/workflows/`: a lock file
must have a source and the compiled shape (`src/shape.rs`: the generated
enter and seal steps, byte for byte, and nothing between them that
leaves the wrapper), and anything else must be listed, with its reason,
leaves the wrapper; the enter step is compared with `runtime/`, or with
`--runtime DIR` in a repository that imports the library from a checkout
of this one), and anything else must be listed, with its reason,
in `.github/uncompiled-workflows`. `trusted-check.yml` runs the same
checks on every pull request with the base branch's code, the pull
request's tree only as data, and needs a maintainer's `compiler-change`
Expand Down
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,19 @@ fn stale(path: &str, source: &str, committed: Option<&str>, compiled: &str) -> O

/// Compiles or checks the repository at `root`, returning the problems
/// found. Only fetching new actions' metadata happens outside it.
pub fn run(root: &Path, mode: Mode) -> Result<Vec<String>> {
/// `runtime` is the directory, relative to `root`, of the compiler
/// runtime the lock files embed: `runtime` in this repository, and the
/// `runtime` of a checkout of it in a repository that imports its library
/// from there.
pub fn run(root: &Path, runtime: &str, mode: Mode) -> Result<Vec<String>> {
let repo = Repo::open(root)?;
match mode {
Mode::Check => check(&repo),
Mode::Check => check(&repo, runtime),
Mode::Compile => compile(&repo),
}
}

fn check(root: &Repo) -> Result<Vec<String>> {
fn check(root: &Repo, runtime: &str) -> Result<Vec<String>> {
let lock = actions::read_lock(root)?;
let mut problems = actions::check_lock(root, &lock);
// The files the sources load, whose scripts are linted below; a
Expand Down Expand Up @@ -160,7 +164,7 @@ fn check(root: &Repo) -> Result<Vec<String>> {
}
}
}
problems.extend(shape::check_tree(root)?);
problems.extend(shape::check_tree(root, runtime)?);
problems.extend(pins::check(root)?);
let loaded: Vec<String> = loaded.into_iter().collect();
problems.extend(scripts::check(root, &loaded)?);
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::process::ExitCode;

use anyhow::Result;
use clap::{Args, Parser, Subcommand};
use workflow_compiler::{Mode, extract, run};
use workflow_compiler::{Mode, extract, run, shape};

#[derive(Parser)]
#[command(
Expand Down Expand Up @@ -46,6 +46,11 @@ struct Repo {
/// The repository's root.
#[arg(long, default_value = ".")]
root: PathBuf,
/// The compiler's runtime/ directory, relative to the root, that the
/// lock files embed: a checkout's, in a repository that imports the
/// library from a checkout of workflow-compiler (#8).
#[arg(long, default_value = shape::RUNTIME_DIR)]
runtime: String,
}

fn main_inner() -> Result<bool> {
Expand All @@ -62,7 +67,7 @@ fn main_inner() -> Result<bool> {
return Ok(true);
}
};
let problems = run(&repo.root, mode)?;
let problems = run(&repo.root, &repo.runtime, mode)?;
for p in &problems {
eprintln!("error: {p}");
}
Expand Down
22 changes: 15 additions & 7 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use crate::{LOCKS_DIR, SOURCES_DIR, UNCOMPILED, nickel};
/// name.
pub type Runtime = BTreeMap<String, String>;

const RUNTIME_DIR: &str = "runtime";
/// Where a repository keeps the compiler's runtime scripts, by default:
/// this repository's own.
pub const RUNTIME_DIR: &str = "runtime";
const EXEC: &str = "/usr/local/bin/runner-sandbox-exec";
const SEAL_ID: &str = "runner-sandbox-seal";
const SEAL_CONDITION: &str = "steps.runner-sandbox-seal.outcome == 'success'";
Expand Down Expand Up @@ -620,11 +622,11 @@ fn files_in(root: &Repo, rel: &str) -> Result<Vec<String>> {
.collect())
}

/// The runtime scripts of the repository at `root`.
pub fn read_runtime(root: &Repo) -> Result<Runtime> {
/// The runtime scripts in `dir` of the repository at `root`.
pub fn read_runtime(root: &Repo, dir: &str) -> Result<Runtime> {
let mut runtime = Runtime::new();
for name in files_in(root, RUNTIME_DIR)? {
let rel = format!("{RUNTIME_DIR}/{name}");
for name in files_in(root, dir)? {
let rel = format!("{dir}/{name}");
// Only files are scripts; a directory or symlink there is skipped.
if root.is_file(&rel)?
&& let Some(text) = root.read(&rel)?
Expand All @@ -640,7 +642,7 @@ pub fn read_runtime(root: &Repo) -> Result<Runtime> {
/// [`UNCOMPILED`], one name per line with its reason after `#`: each runs
/// with none of the compiler's guarantees, so each is a reviewed
/// exception. Returns the problems.
pub fn check_tree(root: &Repo) -> Result<Vec<String>> {
pub fn check_tree(root: &Repo, runtime_dir: &str) -> Result<Vec<String>> {
let mut found = Vec::new();
let list_name = Path::new(UNCOMPILED)
.file_name()
Expand All @@ -654,7 +656,7 @@ pub fn check_tree(root: &Repo) -> Result<Vec<String>> {
Value::from(line)
));
}
let runtime = read_runtime(root)?;
let runtime = read_runtime(root, runtime_dir)?;
let files = files_in(root, LOCKS_DIR)?;
for file in &files {
let rel = format!("{LOCKS_DIR}/{file}");
Expand All @@ -670,6 +672,12 @@ pub fn check_tree(root: &Repo) -> Result<Vec<String>> {
found.push(format!("{rel} has no source in {SOURCES_DIR}/"));
continue;
}
if runtime.is_empty() {
found.push(format!(
"{rel}: no runtime scripts in {runtime_dir}/ to check its enter step against; pass --runtime with the runtime/ directory of the compiler the sources import"
));
continue;
}
let text = root
.read(&rel)?
.with_context(|| format!("{rel} disappeared"))?;
Expand Down
12 changes: 9 additions & 3 deletions tests/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ use std::sync::LazyLock;

use serde_json::{Value, json};
use workflow_compiler::repo::Repo;
use workflow_compiler::shape::{Runtime, check_tree, check_workflow, read_runtime};
use workflow_compiler::shape::{RUNTIME_DIR, Runtime, check_tree, check_workflow, read_runtime};

const EXEC: &str = "/usr/local/bin/runner-sandbox-exec";
const SEAL: &str = "steps.runner-sandbox-seal.outcome == 'success'";
const UPLOAD: &str = "actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a";

static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
read_runtime(&Repo::open(Path::new(env!("CARGO_MANIFEST_DIR"))).unwrap()).unwrap()
read_runtime(
&Repo::open(Path::new(env!("CARGO_MANIFEST_DIR"))).unwrap(),
RUNTIME_DIR,
)
.unwrap()
});

fn rt(name: &str) -> &'static str {
Expand Down Expand Up @@ -487,7 +491,9 @@ fn every_workflow_file_is_compiled_or_a_listed_exception() {
".github/uncompiled-workflows",
"# comment\nci.yml # our own checks\nnoreason.yml\ngone.yml # was here\n",
);
let found = check_tree(&Repo::open(root).unwrap()).unwrap().join("\n");
let found = check_tree(&Repo::open(root).unwrap(), RUNTIME_DIR)
.unwrap()
.join("\n");
for want in [
"extra.YML isn't compiled",
"orphan.lock.yml has no source",
Expand Down
Loading