use super::{CiCommand, strip_yaml_key};
pub(super) fn quoted_scalar_value(text: &str) -> Option<String> {
let mut chars = text.char_indices();
let (_, quote) = chars.next().filter(|(_, ch)| matches!(ch, '\'' | '"'))?;
let mut value = String::with_capacity(text.len());
while let Some((index, ch)) = chars.next() {
match ch {
'\'' if quote == '\'' => {
if text[index + 1..].starts_with('\'') {
chars.next();
value.push('\'');
continue;
}
return closes_scalar(&text[index + 1..]).then_some(value);
}
'"' if quote == '"' => return closes_scalar(&text[index + 1..]).then_some(value),
'\\' if quote == '"' => {
let (_, escaped) = chars.next()?;
value.push(match escaped {
'n' => '\n',
't' => '\t',
other => other,
});
}
other => value.push(other),
}
}
None
}
fn closes_scalar(rest: &str) -> bool {
let rest = rest.trim_start();
rest.is_empty() || rest.starts_with('#')
}
#[derive(Default)]
pub(super) struct MultilineRunState {
active: bool,
indent: usize,
folding: bool,
}
impl MultilineRunState {
pub(super) fn start(&mut self, key_column: usize, folding: bool) {
self.active = true;
self.indent = key_column;
self.folding = folding;
}
fn stop(&mut self) {
self.active = false;
self.folding = false;
}
}
pub(super) fn run_key_column(line: &str, trimmed: &str) -> usize {
let base = line.len() - line.trim_start().len();
match trimmed.strip_prefix("- ") {
Some(rest) => base + 2 + (rest.len() - rest.trim_start().len()),
None => base,
}
}
pub(super) fn push_multiline_run_command(
line: &str,
line_index: usize,
state: &mut MultilineRunState,
commands: &mut Vec<CiCommand>,
working_directory: Option<String>,
) -> bool {
if !state.active {
return false;
}
let trimmed = line.trim();
let indent = line.len() - line.trim_start().len();
if indent > state.indent && !trimmed.is_empty() {
if state.folding {
if let Some(last) = commands.last_mut() {
last.text.push(' ');
last.text.push_str(trimmed);
}
} else {
commands.push(CiCommand {
text: trimmed.to_string(),
working_directory,
line: line_index,
});
}
return true;
}
state.stop();
false
}
pub(super) fn yaml_run_value(trimmed: &str) -> Option<&str> {
strip_yaml_key(trimmed, "run")
.or_else(|| {
trimmed
.strip_prefix("- ")
.and_then(|rest| strip_yaml_key(rest.trim(), "run"))
})
.map(str::trim)
}
pub(super) fn is_multiline_run_marker(value: &str) -> bool {
let mut chars = value.chars();
if !matches!(chars.next(), Some('|' | '>')) {
return false;
}
let mut chomping = false;
let mut indentation = false;
for ch in chars {
match ch {
'-' | '+' if !chomping => chomping = true,
'1'..='9' if !indentation => indentation = true,
_ => return false,
}
}
true
}
#[cfg(test)]
mod tests {
use super::super::extract_ci_commands;
#[test]
fn github_actions_quoted_run_values() {
let content = r#"
jobs:
build:
steps:
- name: single
run: 'node scripts/single.js artifacts >> "$GITHUB_STEP_SUMMARY"'
- name: double
run: "node scripts/double.js"
- run: 'echo ''quoted'' && node scripts/escaped.js' # trailing comment
- run: "node scripts/escaped-double.js \"$ARG\""
"#;
let commands = extract_ci_commands(content);
assert_eq!(
commands,
[
r#"node scripts/single.js artifacts >> "$GITHUB_STEP_SUMMARY""#,
"node scripts/double.js",
"echo 'quoted' && node scripts/escaped.js",
r#"node scripts/escaped-double.js "$ARG""#,
]
);
}
#[test]
fn github_actions_quoted_run_value_over_lines() {
let content = r"
jobs:
build:
steps:
- run: 'node scripts/long.js
--flag value'
- run: npm test
";
let commands = extract_ci_commands(content);
assert_eq!(commands, ["node scripts/long.js --flag value", "npm test"]);
}
#[test]
fn gitlab_ci_quoted_script_items() {
let content = r#"
build:
script:
- 'node scripts/single.js'
- "node scripts/double.js"
"#;
let commands = extract_ci_commands(content);
assert_eq!(
commands,
["node scripts/single.js", "node scripts/double.js"]
);
}
}