use super::ci::CiCommand;
struct CommandInputAction {
name: &'static str,
inputs: &'static [&'static str],
cwd_input: Option<&'static str>,
}
const COMMAND_INPUT_ACTIONS: &[CommandInputAction] = &[CommandInputAction {
name: "changesets/action",
inputs: &["version", "publish"],
cwd_input: Some("cwd"),
}];
#[derive(Default)]
pub(super) struct StepActionInputs {
action: Option<&'static CommandInputAction>,
with_column: Option<usize>,
entry_column: Option<usize>,
entries: Vec<(String, String, usize)>,
}
impl StepActionInputs {
pub(super) fn observe(
&mut self,
step_column: usize,
column: usize,
indent: usize,
key: &str,
line: usize,
) {
if self.with_column.is_some_and(|with| indent <= with) {
self.with_column = None;
self.entry_column = None;
}
if column == step_column {
if let Some(uses) = strip_key(key, "uses") {
self.action = known_action(uses);
} else if strip_key(key, "with")
.is_some_and(|rest| rest.is_empty() || rest.starts_with('#'))
{
self.with_column = Some(column);
}
return;
}
if self.with_column.is_none() {
return;
}
let entry_column = *self.entry_column.get_or_insert(column);
if column != entry_column {
return;
}
if let Some((name, value)) = key.split_once(':') {
let value = super::ci::unquote(super::ci::strip_yaml_comment(value.trim()));
if !value.is_empty() && !value.starts_with(['|', '>']) {
self.entries
.push((name.trim().to_string(), value.to_string(), line));
}
}
}
pub(super) fn into_commands(self) -> Vec<CiCommand> {
let Some(action) = self.action else {
return Vec::new();
};
let working_directory = action.cwd_input.and_then(|cwd| {
self.entries
.iter()
.find(|(name, _, _)| name == cwd)
.map(|(_, value, _)| value.clone())
});
self.entries
.into_iter()
.filter(|(name, _, _)| action.inputs.contains(&name.as_str()))
.map(|(_, text, line)| CiCommand {
text,
working_directory: working_directory.clone(),
line,
})
.collect()
}
}
fn known_action(uses: &str) -> Option<&'static CommandInputAction> {
let uses = super::ci::unquote(super::ci::strip_yaml_comment(uses.trim()));
let name = uses.split_once('@').map_or(uses, |(name, _)| name);
COMMAND_INPUT_ACTIONS
.iter()
.find(|action| action.name.eq_ignore_ascii_case(name))
}
fn strip_key<'a>(line: &'a str, key: &str) -> Option<&'a str> {
line.strip_prefix(key)?.strip_prefix(':').map(str::trim)
}
#[cfg(test)]
mod tests {
use super::super::ci::extract_ci_commands;
#[test]
fn command_inputs_of_a_known_action_are_commands() {
let workflow = r#"
jobs:
release:
steps:
- uses: actions/checkout@v4
- name: Create release pull request
uses: changesets/action@v1 # pinned
with: # inputs
commit: "chore(release): version packages"
version: node scripts/version.js
publish: 'pnpm release'
env:
version: node scripts/env-only.js
- run: pnpm test
"#;
assert_eq!(
extract_ci_commands(workflow),
vec!["node scripts/version.js", "pnpm release", "pnpm test"]
);
}
#[test]
fn inputs_of_an_unknown_action_and_block_values_are_not_commands() {
let workflow = r"
steps:
- uses: some/other-action@v2
with:
version: node scripts/a.js
- with:
publish: |
node scripts/b.js
uses: changesets/action@v1
";
assert!(extract_ci_commands(workflow).is_empty());
}
#[test]
fn the_cwd_input_sets_the_working_directory() {
let workflow = r"
steps:
- uses: changesets/action@v1
with:
cwd: packages/web
publish: node scripts/publish.js
";
let steps = super::super::ci::extract_ci_steps(workflow);
assert_eq!(steps.len(), 1);
assert_eq!(steps[0].working_directory.as_deref(), Some("packages/web"));
}
}