use crate::workflow::{Trigger, Workflow};
use globset::{GlobBuilder, GlobMatcher};
#[derive(Debug, Clone)]
pub struct Event {
pub base: String,
pub head: Option<String>,
pub changed: Vec<String>,
pub all: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Decision {
Run { event: String },
Skip { reason: String },
}
fn matcher(pat: &str) -> Option<GlobMatcher> {
let mut p = String::new();
let mut from = 0;
for (i, _) in pat.match_indices("**") {
p.push_str(&pat[from..i]);
from = i + 2;
let after = &pat[from..];
if after.is_empty() || after.starts_with('/') {
p.push_str("**");
} else {
p.push_str("**/*");
}
}
p.push_str(&pat[from..]);
GlobBuilder::new(&p)
.literal_separator(true)
.backslash_escape(true)
.build()
.ok()
.map(|g| g.compile_matcher())
}
pub fn last_match(patterns: &[String], path: &str) -> Option<bool> {
let mut result = None;
for p in patterns {
let (neg, body) = match p.strip_prefix('!') {
Some(b) => (true, b),
None => (false, p.as_str()),
};
if let Some(m) = matcher(body) {
if m.is_match(path) {
result = Some(!neg);
}
}
}
result
}
fn branch_ok(t: &Trigger, branch: Option<&str>) -> bool {
if t.branches.is_empty() && t.branches_ignore.is_empty() {
return true;
}
let Some(b) = branch else { return false };
if !t.branches.is_empty() {
return last_match(&t.branches, b) == Some(true);
}
last_match(&t.branches_ignore, b) != Some(true)
}
fn paths_ok(t: &Trigger, ev: &Event) -> Result<(), String> {
if ev.all {
return Ok(());
}
if !t.paths.is_empty() {
if ev
.changed
.iter()
.any(|f| last_match(&t.paths, f) == Some(true))
{
return Ok(());
}
return Err("paths not matched".into());
}
if !t.paths_ignore.is_empty()
&& !ev.changed.is_empty()
&& ev
.changed
.iter()
.all(|f| last_match(&t.paths_ignore, f) == Some(true))
{
return Err("all changes in paths-ignore".into());
}
Ok(())
}
const PR_EVENTS: [&str; 2] = ["pull_request", "pull_request_target"];
const DEFAULT_PR_TYPES: [&str; 3] = ["opened", "synchronize", "reopened"];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum MissingMetadata {
BaseBranch,
HeadBranch,
ChangedFiles,
}
pub fn decide(w: &Workflow, ev: &Event) -> Decision {
decide_with_metadata(w, ev, true, true, true).expect("a complete event has no unknown metadata")
}
pub(crate) fn decide_with_metadata(
w: &Workflow,
ev: &Event,
base_known: bool,
head_known: bool,
changes_known: bool,
) -> Result<Decision, MissingMetadata> {
let mut reasons = Vec::new();
let mut unknown = Vec::new();
for t in &w.triggers {
let pr = PR_EVENTS.contains(&t.event.as_str());
if !pr && t.event != "push" {
continue;
}
if pr
&& !t.types.is_empty()
&& !t
.types
.iter()
.any(|ty| DEFAULT_PR_TYPES.contains(&ty.as_str()))
{
reasons.push(format!("{}: types {}", t.event, t.types.join(",")));
continue;
}
if !pr
&& (!t.tags.is_empty() || !t.tags_ignore.is_empty())
&& t.branches.is_empty()
&& t.branches_ignore.is_empty()
{
reasons.push("push: tags only".into());
continue;
}
let event = if pr { "pull_request" } else { "push" };
let missing_branch = if !t.branches.is_empty() || !t.branches_ignore.is_empty() {
if pr && !base_known {
Some(MissingMetadata::BaseBranch)
} else if !pr && !head_known {
Some(MissingMetadata::HeadBranch)
} else {
None
}
} else {
None
};
let branch = if pr {
Some(ev.base.as_str())
} else {
ev.head.as_deref()
};
if missing_branch.is_none() && !branch_ok(t, branch) {
reasons.push(format!("{}: branch filter", t.event));
continue;
}
if !ev.all && !changes_known && (!t.paths.is_empty() || !t.paths_ignore.is_empty()) {
unknown.push((
event,
missing_branch.unwrap_or(MissingMetadata::ChangedFiles),
));
continue;
}
match paths_ok(t, ev) {
Ok(()) => {
if let Some(missing) = missing_branch {
unknown.push((event, missing));
continue;
}
if let Some((_, missing)) = unknown.iter().find(|(earlier, _)| *earlier != event) {
return Err(*missing);
}
return Ok(Decision::Run {
event: event.into(),
});
}
Err(r) => reasons.push(r),
}
}
if let Some((_, missing)) = unknown.first() {
return Err(*missing);
}
if w.triggers
.iter()
.all(|t| !PR_EVENTS.contains(&t.event.as_str()) && t.event != "push")
{
let evs: Vec<&str> = w.triggers.iter().map(|t| t.event.as_str()).collect();
return Ok(Decision::Skip {
reason: format!(
"only {}",
if evs.is_empty() {
"no triggers".to_string()
} else {
evs.join(", ")
}
),
});
}
reasons.dedup();
Ok(Decision::Skip {
reason: reasons.join("; "),
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::workflow::parse;
fn ev(changed: &[&str]) -> Event {
Event {
base: "main".into(),
head: Some("feature".into()),
changed: changed.iter().map(|s| s.to_string()).collect(),
all: false,
}
}
#[test]
fn double_star_glued_to_a_name_is_its_own_component() {
let p = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
let m = |pat: &str, path: &str| last_match(&p(&[pat]), path);
assert_eq!(m("docs/**.md", "docs/a/b.md"), Some(true));
assert_eq!(m("docs/**.md", "docs/b.md"), Some(true));
assert_eq!(m("docs/**.md", "docs/b.txt"), None);
assert_eq!(m("a/**b/**c", "a/x/b/y/c"), Some(true));
assert_eq!(m("a/**b/**c", "a/x/b/y/d"), None);
assert_eq!(m("a/**/**.md", "a/x/y/b.md"), Some(true));
assert_eq!(m("a/**/**.md", "a/x/y/b.txt"), None);
}
#[test]
fn glob_semantics() {
let p = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
assert_eq!(last_match(&p(&["src/**"]), "src/a/b.ts"), Some(true));
assert_eq!(last_match(&p(&["*.js"]), "a/b.js"), None);
assert_eq!(last_match(&p(&["*.js"]), "b.js"), Some(true));
assert_eq!(last_match(&p(&["**/*.ts"]), "x/y/z.ts"), Some(true));
assert_eq!(last_match(&p(&["**/*.ts"]), "z.ts"), Some(true));
assert_eq!(
last_match(&p(&["app/**", "!app/docs/**"]), "app/docs/x.md"),
Some(false)
);
assert_eq!(
last_match(
&p(&[".github/workflows/ci.yml"]),
".github/workflows/ci.yml"
),
Some(true)
);
}
#[test]
fn paths_and_ignore() {
let w = parse(
"a.yml",
"on:\n pull_request:\n paths: ['api/**']\njobs: {}\n",
)
.unwrap();
assert!(matches!(
decide(&w, &ev(&["api/x.ts"])),
Decision::Run { .. }
));
assert!(matches!(
decide(&w, &ev(&["web/x.ts"])),
Decision::Skip { .. }
));
let mut all = ev(&["web/x.ts"]);
all.all = true;
assert!(matches!(decide(&w, &all), Decision::Run { .. }));
let w = parse(
"a.yml",
"on:\n pull_request:\n paths-ignore: ['**.md']\njobs: {}\n",
)
.unwrap();
assert!(matches!(
decide(&w, &ev(&["README.md"])),
Decision::Skip { .. }
));
assert!(matches!(
decide(&w, &ev(&["README.md", "a.ts"])),
Decision::Run { .. }
));
}
#[test]
fn paths_ignore_does_not_skip_a_run_that_changed_nothing() {
let w = parse(
"a.yml",
"on:\n pull_request:\n paths-ignore: ['**.md']\njobs: {}\n",
)
.unwrap();
assert!(matches!(decide(&w, &ev(&[])), Decision::Run { .. }));
}
#[test]
fn events_types_and_branches() {
let w = parse("a.yml", "on:\n push:\n branches: [main]\njobs: {}\n").unwrap();
assert!(matches!(decide(&w, &ev(&["a"])), Decision::Skip { .. }));
let w = parse(
"a.yml",
"on:\n pull_request:\n branches: [main]\njobs: {}\n",
)
.unwrap();
assert_eq!(
decide(&w, &ev(&["a"])),
Decision::Run {
event: "pull_request".into()
}
);
let w = parse(
"a.yml",
"on:\n pull_request:\n types: [closed]\njobs: {}\n",
)
.unwrap();
assert!(matches!(decide(&w, &ev(&["a"])), Decision::Skip { .. }));
let w = parse(
"a.yml",
"on:\n pull_request:\n types: [opened, ready_for_review]\njobs: {}\n",
)
.unwrap();
assert!(matches!(decide(&w, &ev(&["a"])), Decision::Run { .. }));
let w = parse(
"a.yml",
"on:\n workflow_dispatch:\n schedule:\n - cron: '0 0 * * *'\njobs: {}\n",
)
.unwrap();
assert_eq!(
decide(&w, &ev(&["a"])),
Decision::Skip {
reason: "only workflow_dispatch, schedule".into()
}
);
let w = parse("a.yml", "on: push\njobs: {}\n").unwrap();
assert_eq!(
decide(&w, &ev(&["a"])),
Decision::Run {
event: "push".into()
}
);
let w = parse(
"a.yml",
"on:\n push:\n tags: ['v*']\n workflow_dispatch:\njobs: {}\n",
)
.unwrap();
assert_eq!(
decide(&w, &ev(&["a"])),
Decision::Skip {
reason: "push: tags only".into()
}
);
}
#[test]
fn an_invalid_glob_matches_nothing() {
let p = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
assert_eq!(last_match(&p(&["[a"]), "a"), None);
assert_eq!(last_match(&p(&["[a", "a"]), "a"), Some(true));
assert_eq!(last_match(&p(&["a", "!b"]), "a"), Some(true));
assert_eq!(last_match(&p(&["a", "!a"]), "a"), Some(false));
}
#[test]
fn branches_and_branches_ignore() {
let mut e = ev(&["a"]);
let w = parse("a.yml", "on:\n push:\n branches: [main]\njobs: {}\n").unwrap();
e.head = None;
assert!(matches!(decide(&w, &e), Decision::Skip { .. }));
let w = parse(
"a.yml",
"on:\n pull_request:\n branches-ignore: [main]\njobs: {}\n",
)
.unwrap();
assert!(matches!(decide(&w, &ev(&["a"])), Decision::Skip { .. }));
let mut other = ev(&["a"]);
other.base = "dev".into();
assert!(matches!(decide(&w, &other), Decision::Run { .. }));
}
#[test]
fn a_workflow_with_no_triggers_says_so() {
let w = parse("a.yml", "jobs: {}\n").unwrap();
assert_eq!(
decide(&w, &ev(&["a"])),
Decision::Skip {
reason: "only no triggers".into()
}
);
}
#[test]
fn partial_metadata_requires_only_fields_that_can_change_the_ordered_decision() {
use MissingMetadata::{BaseBranch, ChangedFiles, HeadBranch};
let skip = |reason: &str| {
Ok(Decision::Skip {
reason: reason.into(),
})
};
let run = |event: &str| {
Ok(Decision::Run {
event: event.into(),
})
};
for (triggers, base, head, changes, expected) in [
(
"push:\n paths-ignore: ['docs/**']",
false,
false,
false,
Err(ChangedFiles),
),
(
"pull_request:\n branches-ignore: ['release/**']",
false,
false,
false,
Err(BaseBranch),
),
(
"push:\n branches-ignore: ['release/**']",
false,
false,
false,
Err(HeadBranch),
),
(
"push:\n tags-ignore: ['legacy']\n paths: ['src/**']",
false,
false,
false,
skip("push: tags only"),
),
(
"pull_request:\n branches: [main]\n paths: ['src/**']",
true,
false,
false,
skip("pull_request: branch filter"),
),
(
"pull_request:\n branches: [main]\n paths: ['src/**']",
false,
false,
true,
skip("paths not matched"),
),
(
"pull_request:\n branches: [main]\n paths: ['src/**']",
false,
false,
false,
Err(BaseBranch),
),
(
"push:\n branches: [main]\n paths: ['src/**']",
false,
false,
false,
Err(HeadBranch),
),
(
"push:\n branches: [main]\n paths: ['src/**']",
false,
true,
false,
skip("push: branch filter"),
),
(
"push:\n paths: ['src/**']",
false,
false,
false,
Err(ChangedFiles),
),
(
"pull_request:\n types: [closed]\n branches: [main]\n paths: ['src/**']",
false,
false,
false,
skip("pull_request: types closed"),
),
(
"push:\n tags: ['v*']\n paths: ['src/**']",
false,
false,
false,
skip("push: tags only"),
),
(
"push: {}\n pull_request:\n branches: [main]",
false,
false,
false,
run("push"),
),
(
"pull_request:\n paths: ['src/**']\n pull_request_target: {}",
false,
false,
false,
run("pull_request"),
),
(
"pull_request:\n branches: [main]\n pull_request_target: {}",
false,
false,
false,
run("pull_request"),
),
(
"pull_request:\n branches: [main]\n push: {}",
false,
false,
false,
Err(BaseBranch),
),
(
"push:\n paths: ['src/**']\n pull_request: {}",
false,
false,
false,
Err(ChangedFiles),
),
(
"pull_request:\n types: [closed]\n paths: ['src/**']\n push: {}",
false,
false,
false,
run("push"),
),
(
"pull_request:\n branches: [main]\n pull_request_target:\n branches: [main]",
false,
false,
false,
Err(BaseBranch),
),
] {
let w = parse("ci.yml", &format!("on:\n {triggers}\njobs: {{}}\n")).unwrap();
let event = Event {
base: "dev".into(),
head: None,
changed: vec!["docs/readme.md".into()],
all: false,
};
assert_eq!(
decide_with_metadata(&w, &event, base, head, changes),
expected,
"{triggers}"
);
}
}
}