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
17 changes: 17 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/mergify-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ clap = { version = "4.5", features = ["derive"] }
mergify-ci = { path = "../mergify-ci" }
mergify-config = { path = "../mergify-config" }
mergify-core = { path = "../mergify-core" }
mergify-freeze = { path = "../mergify-freeze" }
mergify-py-shim = { path = "../mergify-py-shim" }
mergify-queue = { path = "../mergify-queue" }
tokio = { version = "1", default-features = false, features = ["macros", "rt", "time"] }

[dev-dependencies]
regex = "1"
serde_yaml_ng = "0.10"

[lints]
workspace = true
70 changes: 70 additions & 0 deletions crates/mergify-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use mergify_config::simulate::PullRequestRef;
use mergify_config::simulate::SimulateOptions;
use mergify_core::OutputMode;
use mergify_core::StdioOutput;
use mergify_freeze::list::ListOptions as FreezeListOptions;
use mergify_queue::pause::PauseOptions;
use mergify_queue::show::ShowOptions;
use mergify_queue::status::StatusOptions;
Expand Down Expand Up @@ -91,6 +92,7 @@ const NATIVE_COMMANDS: &[(&str, &str)] = &[
("queue", "unpause"),
("queue", "status"),
("queue", "show"),
("freeze", "list"),
];

/// Native commands the Rust binary handles without delegating to
Expand All @@ -105,6 +107,7 @@ enum NativeCommand {
QueueUnpause(QueueUnpauseOpts),
QueueStatus(QueueStatusOpts),
QueueShow(QueueShowOpts),
FreezeList(FreezeListOpts),
}

struct ConfigSimulateOpts {
Expand Down Expand Up @@ -156,6 +159,13 @@ struct QueueShowOpts {
output_json: bool,
}

struct FreezeListOpts {
repository: Option<String>,
token: Option<String>,
api_url: Option<String>,
output_json: bool,
}

/// Heuristic: does argv look like the user intended a native
/// subcommand?
///
Expand Down Expand Up @@ -334,6 +344,17 @@ fn detect_native(argv: &[String]) -> Option<NativeCommand> {
verbose,
output_json: json,
})),
Subcommands::Freeze(FreezeArgs {
repository,
token,
api_url,
command: FreezeSubcommand::List(FreezeListCliArgs { json }),
}) => Some(NativeCommand::FreezeList(FreezeListOpts {
repository,
token,
api_url,
output_json: json,
})),
}
}

Expand Down Expand Up @@ -440,6 +461,18 @@ fn run_native(cmd: NativeCommand) -> ExitCode {
)
.await
}
NativeCommand::FreezeList(opts) => {
mergify_freeze::list::run(
FreezeListOptions {
repository: opts.repository.as_deref(),
token: opts.token.as_deref(),
api_url: opts.api_url.as_deref(),
output_json: opts.output_json,
},
&mut output,
)
.await
}
}
});

Expand Down Expand Up @@ -469,6 +502,8 @@ enum Subcommands {
Ci(CiArgs),
/// Manage the Mergify merge queue.
Queue(QueueArgs),
/// Manage scheduled freezes.
Freeze(FreezeArgs),
}

#[derive(clap::Args)]
Expand Down Expand Up @@ -655,3 +690,38 @@ struct ShowCliArgs {
#[arg(long, default_value_t = false)]
json: bool,
}

#[derive(clap::Args)]
struct FreezeArgs {
/// Mergify or GitHub token. Falls back to ``MERGIFY_TOKEN`` and
/// then ``GITHUB_TOKEN`` env vars.
#[arg(long, short = 't', global = true)]
token: Option<String>,

/// Mergify API URL. Falls back to ``MERGIFY_API_URL`` env var,
/// then to the default.
#[arg(long = "api-url", short = 'u', global = true)]
api_url: Option<String>,

/// Repository full name (owner/repo). Falls back to
/// ``GITHUB_REPOSITORY`` env var.
#[arg(long, short = 'r', global = true)]
repository: Option<String>,

#[command(subcommand)]
command: FreezeSubcommand,
}

#[derive(Subcommand)]
enum FreezeSubcommand {
/// List scheduled freezes for a repository.
List(FreezeListCliArgs),
}

#[derive(clap::Args)]
struct FreezeListCliArgs {
/// Emit the raw `scheduled_freezes` array as a single JSON
/// document.
#[arg(long, default_value_t = false)]
json: bool,
}
137 changes: 137 additions & 0 deletions crates/mergify-cli/tests/skill_references.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//! Cross-checks the `mergify-merge-queue` skill against the
//! freshly-built test binary.
//!
//! Replaces the pre-port Python `tests/queue/test_skill.py`: the
//! artifacts being validated (a Markdown skill file and the Rust
//! binary's `--list-native-commands` output) have no Python in
//! the picture, so the test lives next to the binary that emits
//! the truth.
//!
//! Each test fires the freshly-built binary via
//! `CARGO_BIN_EXE_mergify` — that's the same artifact `cargo test`
//! built moments earlier, so the test always exercises the
//! current code rather than whatever happens to be on `PATH`.

use std::collections::BTreeSet;
use std::path::PathBuf;
use std::process::Command;

use regex::Regex;
use serde_yaml_ng::Value;

const REQUIRED_SECTIONS: &[&str] = &[
"## Commands",
"## Checking Queue Status",
"## Inspecting a PR",
"## Queue States",
"## Troubleshooting",
];

/// Resolve `skills/mergify-merge-queue/SKILL.md` from the
/// repository root. `CARGO_MANIFEST_DIR` points at this crate's
/// directory; two `..` hops up to the workspace root.
fn skill_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("..")
.join("skills")
.join("mergify-merge-queue")
.join("SKILL.md")
}

fn skill_content() -> String {
let path = skill_path();
std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()))
}

/// Ask the binary for its `(group, subcommand)` pairs and collect
/// the subcommands for `group`. Spawning the binary keeps the
/// test honest — a port that adds a native subcommand and its
/// `NATIVE_COMMANDS` entry shows up automatically, no parallel
/// list to drift.
fn native_commands_for_group(group: &str) -> BTreeSet<String> {
let binary = env!("CARGO_BIN_EXE_mergify");
let output = Command::new(binary)
.arg("--list-native-commands")
.output()
.unwrap_or_else(|e| panic!("spawn {binary} --list-native-commands: {e}"));
assert!(
output.status.success(),
"mergify --list-native-commands exited {:?}\nstderr:\n{}",
output.status,
String::from_utf8_lossy(&output.stderr),
);
let stdout = String::from_utf8(output.stdout).expect("stdout is UTF-8");
stdout
.lines()
.filter_map(|line| {
let (g, sub) = line.split_once(char::is_whitespace)?;
(g == group).then(|| sub.to_string())
})
.collect()
}

#[test]
fn skill_content_is_readable() {
assert!(!skill_content().is_empty(), "SKILL.md must not be empty");
}

#[test]
fn skill_has_valid_frontmatter() {
let content = skill_content();
// Extract YAML frontmatter between --- markers — the same
// shape Claude Code's skill loader expects.
let re = Regex::new(r"(?s)^---\n(.+?)\n---\n").expect("frontmatter regex compiles");
let captures = re
.captures(&content)
.expect("Skill must have YAML frontmatter");
let yaml = captures.get(1).unwrap().as_str();
let parsed: Value = serde_yaml_ng::from_str(yaml).expect("frontmatter is valid YAML");
let mapping = parsed
.as_mapping()
.expect("frontmatter must be a YAML mapping");
let name = mapping
.get(Value::from("name"))
.and_then(Value::as_str)
.expect("frontmatter must have 'name'");
assert_eq!(name, "mergify-merge-queue");
assert!(
mapping.get(Value::from("description")).is_some(),
"frontmatter must have 'description'",
);
}

#[test]
fn skill_has_required_sections() {
let content = skill_content();
for section in REQUIRED_SECTIONS {
assert!(
content.contains(section),
"Skill is missing required section: {section}",
);
}
}

#[test]
fn skill_references_valid_commands() {
let content = skill_content();
let re = Regex::new(r"mergify queue ([\w-]+)").expect("reference regex compiles");
// BTreeSet so iteration order — and therefore which assertion
// trips first — is deterministic. Same for `available` below:
// its `Debug` output ends up in the failure message and would
// otherwise reshuffle between runs.
let referenced: BTreeSet<String> = re
.captures_iter(&content)
.map(|c| c[1].to_string())
.collect();
let available = native_commands_for_group("queue");

for cmd in &referenced {
assert!(
available.contains(cmd),
"Skill references 'mergify queue {cmd}' but it's not a Rust-native \
command reported by `mergify --list-native-commands`. \
Available: {available:?}",
);
}
}
25 changes: 25 additions & 0 deletions crates/mergify-freeze/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "mergify-freeze"
version = "0.0.0"
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true
authors.workspace = true
description = "Native implementation of `mergify freeze` subcommands."
publish = false

[dependencies]
mergify-core = { path = "../mergify-core" }
mergify-tui = { path = "../mergify-tui" }
anstyle = "1"
chrono = { version = "0.4", default-features = false, features = ["clock"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[dev-dependencies]
tokio = { version = "1", default-features = false, features = ["macros", "rt", "time"] }
wiremock = "0.6"

[lints]
workspace = true
9 changes: 9 additions & 0 deletions crates/mergify-freeze/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Native Rust implementation of the `mergify freeze` subcommands.
//!
//! `freeze list` is the first port — a read-only `GET` on
//! `/v1/repos/<repo>/scheduled_freeze` with either a JSON
//! passthrough of the inner `scheduled_freezes` array or a
//! human-readable table. `create` / `update` / `delete` follow
//! the same module-per-subcommand layout once they land.
pub mod list;
Loading
Loading