Skip to content
Open
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
6 changes: 5 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ review bots read and write at the same time, written in Rust.
what it reads and checks what it writes, by the rights `policy.rs`
resolves from `policy.toml`. `plugin.rs` runs `origin <name>` as
`origin-<name>` from `PATH`, passing the repository in the environment.
- `apps/origin-ci`: the CI plugin, `origin-ci`, which reads `checks.toml`.
- `apps/origin-ci`: the CI plugin, `origin-ci`. It reads `checks.toml`
from the revision it checks (`origin ci run`). It runs each check in a
checkout of the revision of its own, and issues the results. Like every
plugin, it uses only the public API: `Remote`, which is the SDK over HTTP
or the service in-process, never origin-src's internals.
- `tools/xtask`: development tasks, run with `cargo xtask`, such as timing
origin against git (`just bench-git`).

Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ origin fetch [--target <t>] [--change <id>] # new acceptances, and the records
origin pull # fetch, then move the working copy onto its target's tip
origin whoami | permissions # who the remote thinks you are, and what you may do there
origin token issue --scope "write src" [--ttl 1h] # a short-lived token for an agent

origin ci run [<rev>] # run the checks of checks.toml, and issue their results
```

Landing rebases a change onto the target's tip, file by file, checks the
Expand Down Expand Up @@ -119,7 +121,7 @@ send each server under `[tokens]`, by its URL
(`"http://127.0.0.1:7878" = "<token>"`), or in `$ORIGIN_TOKEN`. See
[the design](docs/design.md) for the model.

## Plugins
## Plugins and CI

`origin <name>` runs `origin-<name>` from `PATH` when `<name>` isn't a
built-in command, with its arguments; its output and exit code are the
Expand All @@ -129,6 +131,40 @@ the plugin `ORIGIN_REPOSITORY`, the directory holding the repository's store,
remote, if there's one. Plugins use origin's public API only: a server's
over HTTP, or for a local path, the same service in-process.

The first is [`origin-ci`](apps/origin-ci). It reads the checks from
`checks.toml`:

```toml
[checks.test]
command = "cargo test"
inputs = ["src", "Cargo.toml", "Cargo.lock"] # the whole snapshot by default
toolchain = "rustc -V" # its output is hashed

[checks.links]
command = "lychee docs"
inputs = ["docs"]
external = true # reads the network: never promoted
```

Each check runs in a checkout of the revision of its own, removed
afterwards, and issues a `ci.<name>` statement: `pass` if the command exits
with 0, with the last 64 KiB of its output as the payload, declaring its
inputs, `checks.toml` always among them, command and toolchain output, so
its result is promoted to every revision where they're the same. A check a
result of the same issuer already covers, ran or promoted, is skipped.
`origin ci run` reads the checks from the revision it checks.
Each command runs in a process group of its own. On Linux, what it leaves
running in that group is killed once it ends, or if the run was killed, by
the next run; a process that leaves the group escapes.
On macOS, where origin can't tell which processes are still the check's,
nothing a check leaves running is killed, so stop it yourself. The
landing queue promotes results without running the toolchain command, so
new tools with unchanged inputs don't stop old results from being
promoted: pin the toolchain in an input, such as `rust-toolchain.toml`.
`origin ci run` checks a revision of this repository as you, and fails if a
check fails. Its checkouts
live in `$XDG_STATE_HOME/origin-ci/` (or `~/.local/state/`).

## Permissions

What each user may do lives in `policy.toml`, so changing it is a landing
Expand Down
6 changes: 6 additions & 0 deletions apps/origin-ci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ rust-version.workspace = true
license.workspace = true
repository.workspace = true

[[bin]]
name = "origin-ci"
path = "src/main.rs"

[dependencies]
anyhow.workspace = true
clap.workspace = true
origin-platform.workspace = true
origin-shared.workspace = true
origin-src.workspace = true
Expand Down
5 changes: 5 additions & 0 deletions apps/origin-ci/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//! `origin-ci`, origin's CI plugin. It runs the checks a revision's
//! `checks.toml` lists and issues their results as statements, which the
//! landing queue takes as evidence.
//!
//! As a tool, `origin ci run` checks a revision of the repository it runs
//! in, as the user. It talks to origin only through the public API: the
//! SDK over HTTP, or for a repository on a local path, the same service
//! in-process.

pub mod checks;
pub mod runner;
Expand Down
125 changes: 125 additions & 0 deletions apps/origin-ci/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//! The `origin-ci` binary, which `origin ci` runs.

use std::process::ExitCode;

use anyhow::{Context as _, Result, bail};
use clap::{Parser, Subcommand};
use origin_ci::runner::{Outcome, Runner};
use origin_ci::state::State;
use origin_shared::api::At;
use origin_shared::{ChangeId, RecordId, RevisionId};
use origin_src::config::UserConfig;
use origin_src::output;
use origin_src::plugin::Context;
use origin_src::remote::{Remote, RemoteUrl};

/// Run the checks of `checks.toml` and issue their results.
#[derive(Debug, Parser)]
#[command(name = "origin ci", version)]
struct Cli {
#[command(subcommand)]
command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
/// Run a revision's checks here, as its own `checks.toml` defines them,
/// and issue their results as you, in this repository. Skips checks that a
/// result of yours, run or promoted, already covers. Fails if a check
/// fails.
Run {
/// The revision: `@` for the working copy, a change's full id for its
/// head, or a revision's full id.
#[arg(default_value = "@")]
rev: String,
},
}

fn main() -> ExitCode {
output::exit_quietly_on_broken_pipe();
match Cli::parse().run() {
Ok(code) => code,
Err(error) => {
eprintln!("error: {error:#}");
ExitCode::FAILURE
}
}
}

impl Cli {
fn run(self) -> Result<ExitCode> {
let config = UserConfig::load()?;
let context = Context::from_env()?;
match self.command {
Command::Run { rev } => {
let (runner, subject) = Self::here(context.as_ref(), &config, "run", &rev)?;
let outcomes = runner.check(subject, &At::Record(subject), None)?;
Ok(Self::report(&outcomes))
}
}
}

/// Creates a runner for the repository `origin ci <command>` runs in, as
/// the user, and returns it with the record `rev` names there.
fn here(
context: Option<&Context>,
config: &UserConfig,
command: &str,
rev: &str,
) -> Result<(Runner, RecordId)> {
let Some(context) = context else {
bail!("run `origin ci {command}` in a working copy");
};
let url = RemoteUrl::Path(context.repository.clone());
let runner = Self::runner(&url, config)?;
let subject = Self::subject(&runner, context, rev)?;
Ok((runner, subject))
}

/// Prints each outcome, and the output of each failed check. Returns a
/// failing exit code if a check failed.
fn report(outcomes: &[Outcome]) -> ExitCode {
let mut failed = false;
for outcome in outcomes {
println!("{outcome}");
if let Outcome::Ran { output, .. } = outcome
&& outcome.failed()
{
print!("{}", String::from_utf8_lossy(output));
}
failed |= outcome.failed();
}
if failed {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
}

/// Creates a runner for the repository at `url`, as the user.
fn runner(url: &RemoteUrl, config: &UserConfig) -> Result<Runner> {
let remote = Remote::open_for(url, config).with_context(|| format!("cannot open {url}"))?;
Ok(Runner::new(remote, config.user(), State::open(url)?)?)
}

/// Returns the record `rev` names: the working copy, a change's head, or a
/// revision.
fn subject(runner: &Runner, context: &Context, rev: &str) -> Result<RecordId> {
if rev == "@" {
return Ok(RecordId::Revision(context.revision));
}
if let Ok(revision) = rev.parse::<RevisionId>() {
return Ok(RecordId::Revision(revision));
}
let change: ChangeId = rev
.parse()
.with_context(|| format!("`{rev}` is neither `@`, a change's id nor a revision's"))?;
match runner.remote().revisions(change)?.heads[..] {
[head] => Ok(RecordId::Revision(head)),
_ => bail!(
"change {} is divergent: name one of its heads",
change.short()
),
}
}
}
Loading