Before the builder evaluates anything, an approver sees the requester's commits relative to the branch and their diff, and approves using the builder. A lease from an earlier request can cover it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
4.1 KiB
Rust
104 lines
4.1 KiB
Rust
//! The builder's describe stage against a local Git repository. Skipped where `git` isn't
|
|
//! installed, such as the package build's sandbox.
|
|
use std::io::{Read, Write};
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::{Command, Stdio};
|
|
|
|
use slop_broker::builder::{BuilderRequest, Description, MAX_DIFF};
|
|
use slop_broker::proto::{decode, encode};
|
|
|
|
fn git_directory() -> Option<PathBuf> {
|
|
let output = Command::new("sh").args(["-c", "command -v git"]).output().ok()?;
|
|
if !output.status.success() {
|
|
return None;
|
|
}
|
|
let path = PathBuf::from(String::from_utf8(output.stdout).ok()?.trim());
|
|
Some(path.parent()?.to_owned())
|
|
}
|
|
|
|
fn git(directory: &Path, args: &[&str]) -> String {
|
|
let output = Command::new("git")
|
|
.arg("-C")
|
|
.arg(directory)
|
|
.args(["-c", "user.name=agent", "-c", "user.email=agent@example", "-c", "commit.gpgsign=false"])
|
|
.args(args)
|
|
.output()
|
|
.unwrap();
|
|
assert!(output.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&output.stderr));
|
|
String::from_utf8(output.stdout).unwrap().trim().to_owned()
|
|
}
|
|
|
|
/// Sends one request to `slop-broker builder` and reads its one-frame answer.
|
|
fn ask(config: &Path, request: &BuilderRequest) -> Description {
|
|
let mut child = Command::new(env!("CARGO_BIN_EXE_slop-broker"))
|
|
.args(["builder", "--config"])
|
|
.arg(config)
|
|
.stdin(Stdio::piped())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::inherit())
|
|
.spawn()
|
|
.unwrap();
|
|
|
|
let frame = encode(request).unwrap();
|
|
let mut stdin = child.stdin.take().unwrap();
|
|
stdin.write_all(&(frame.len() as u32).to_be_bytes()).unwrap();
|
|
stdin.write_all(&frame).unwrap();
|
|
|
|
// The builder exits after answering; stdin stays open until then, as the broker keeps it.
|
|
let mut answer = Vec::new();
|
|
child.stdout.take().unwrap().read_to_end(&mut answer).unwrap();
|
|
assert!(child.wait().unwrap().success());
|
|
drop(stdin);
|
|
|
|
let length = u32::from_be_bytes(answer[..4].try_into().unwrap()) as usize;
|
|
decode(&answer[4..4 + length]).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn describes_commits_with_git_alone() {
|
|
let Some(git_path) = git_directory() else {
|
|
eprintln!("skipped: git isn't installed");
|
|
return;
|
|
};
|
|
|
|
let directory = tempfile::tempdir().unwrap();
|
|
let work = directory.path().join("work");
|
|
std::fs::create_dir(&work).unwrap();
|
|
git(&work, &["init", "--quiet", "--initial-branch=master"]);
|
|
std::fs::write(work.join("flake.nix"), "{ outputs = _: { }; }\n").unwrap();
|
|
git(&work, &["add", "."]);
|
|
git(&work, &["commit", "--quiet", "-m", "Initial"]);
|
|
let reviewed = git(&work, &["rev-parse", "HEAD"]);
|
|
|
|
// Anything in it would run if it were evaluated; describing mustn't.
|
|
git(&work, &["checkout", "--quiet", "-b", "agent"]);
|
|
let flake = format!("{{ outputs = _: builtins.trace \"evaluated\" {{ }}; }}\n# {}\n", "x".repeat(MAX_DIFF));
|
|
std::fs::write(work.join("flake.nix"), flake).unwrap();
|
|
git(&work, &["commit", "--quiet", "-am", "Add \x1b[2Ksomething"]);
|
|
let agent = git(&work, &["rev-parse", "HEAD"]);
|
|
|
|
let state = directory.path().join("state");
|
|
std::fs::create_dir(&state).unwrap();
|
|
let config = directory.path().join("builder.toml");
|
|
std::fs::write(
|
|
&config,
|
|
format!(
|
|
"repository = {:?}\nkey = \"/nonexistent\"\ndirectory = {:?}\npath = [{:?}]\n",
|
|
work.display().to_string(),
|
|
state.display().to_string(),
|
|
git_path.display().to_string(),
|
|
),
|
|
)
|
|
.unwrap();
|
|
|
|
let described = ask(&config, &BuilderRequest::Describe(agent.parse().unwrap()));
|
|
assert!(!described.on_branch);
|
|
assert!(described.log.contains("Add \x1b[2Ksomething"), "{}", described.log);
|
|
assert!(described.stat.contains("flake.nix"), "{}", described.stat);
|
|
assert!(described.diff.contains("builtins.trace") && described.diff.ends_with("(diff cut short)\n"));
|
|
assert!(described.diff.len() < MAX_DIFF + 100);
|
|
|
|
let described = ask(&config, &BuilderRequest::Describe(reviewed.parse().unwrap()));
|
|
assert!(described.on_branch && described.diff.is_empty());
|
|
}
|