-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.rs
More file actions
365 lines (315 loc) · 10.7 KB
/
mod.rs
File metadata and controls
365 lines (315 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//! Pipeline compilation module.
//!
//! This module provides compilation of agent markdown files into Azure DevOps pipeline YAML.
//! Two targets are supported:
//!
//! - **Standalone**: Full-featured pipeline with custom network proxy, MCP firewall, and safe outputs
//! - **1ES**: Integration with 1ES Pipeline Templates using the agencyJob type
mod common;
mod onees;
mod standalone;
mod types;
use anyhow::{Context, Result};
use async_trait::async_trait;
use log::{debug, info};
use std::path::{Path, PathBuf};
pub use common::parse_markdown;
pub use common::sanitize_filename;
pub use types::{CompileTarget, FrontMatter};
/// Trait for pipeline compilers.
///
/// Each target implements this trait to generate target-specific pipeline YAML.
#[async_trait]
pub trait Compiler: Send + Sync {
/// Compile the front matter and markdown body into pipeline YAML.
async fn compile(
&self,
input_path: &Path,
output_path: &Path,
front_matter: &FrontMatter,
markdown_body: &str,
) -> Result<String>;
/// Get the target name for logging.
fn target_name(&self) -> &'static str;
}
/// Main compilation function - entry point for the CLI.
///
/// Parses the input markdown file, determines the target, and delegates to the appropriate compiler.
pub async fn compile_pipeline(input_path: &str, output_path: Option<&str>) -> Result<()> {
let input_path = Path::new(input_path);
info!("Compiling pipeline from: {}", input_path.display());
// Read and parse input markdown
debug!("Reading input file");
let content = tokio::fs::read_to_string(input_path)
.await
.with_context(|| format!("Failed to read input file: {}", input_path.display()))?;
debug!("Input file size: {} bytes", content.len());
let (front_matter, markdown_body) = parse_markdown(&content)?;
info!("Parsed agent: '{}'", front_matter.name);
debug!("Description: {}", front_matter.description);
debug!("Target: {:?}", front_matter.target);
debug!("Engine model: {}", front_matter.engine.model());
debug!("Schedule: {:?}", front_matter.schedule);
debug!("Repositories: {}", front_matter.repositories.len());
debug!("MCP servers configured: {}", front_matter.mcp_servers.len());
// Validate checkout list against repositories
common::validate_checkout_list(&front_matter.repositories, &front_matter.checkout)?;
// Determine output path
let yaml_output_path = match output_path {
Some(p) => PathBuf::from(p),
None => input_path.with_extension("yml"),
};
// Select compiler based on target
let compiler: Box<dyn Compiler> = match front_matter.target {
CompileTarget::OneES => Box::new(onees::OneESCompiler),
CompileTarget::Standalone => Box::new(standalone::StandaloneCompiler),
};
info!("Using {} compiler", compiler.target_name());
// Compile
let pipeline_yaml = compiler
.compile(input_path, &yaml_output_path, &front_matter, &markdown_body)
.await?;
// Clean up spacing artifacts from empty placeholder replacements
let pipeline_yaml = clean_generated_yaml(&pipeline_yaml);
// Write output
tokio::fs::write(&yaml_output_path, &pipeline_yaml)
.await
.with_context(|| {
format!(
"Failed to write pipeline YAML: {}",
yaml_output_path.display()
)
})?;
println!(
"Generated {} pipeline: {}",
compiler.target_name(),
yaml_output_path.display()
);
Ok(())
}
/// Check that a compiled pipeline YAML matches its source markdown.
///
/// Compiles the source markdown fresh and compares (whitespace-normalized)
/// against the existing pipeline file. Returns an error if they differ.
pub async fn check_pipeline(source_path: &str, pipeline_path: &str) -> Result<()> {
let source_path = Path::new(source_path);
let pipeline_path = Path::new(pipeline_path);
info!(
"Checking pipeline integrity: {} -> {}",
source_path.display(),
pipeline_path.display()
);
// Compile fresh from source
let content = tokio::fs::read_to_string(source_path)
.await
.with_context(|| format!("Failed to read source file: {}", source_path.display()))?;
let (front_matter, markdown_body) = parse_markdown(&content)?;
common::validate_checkout_list(&front_matter.repositories, &front_matter.checkout)?;
let compiler: Box<dyn Compiler> = match front_matter.target {
CompileTarget::OneES => Box::new(onees::OneESCompiler),
CompileTarget::Standalone => Box::new(standalone::StandaloneCompiler),
};
let pipeline_yaml = compiler
.compile(source_path, pipeline_path, &front_matter, &markdown_body)
.await?;
let pipeline_yaml = clean_generated_yaml(&pipeline_yaml);
// Read existing pipeline file
let existing = tokio::fs::read_to_string(pipeline_path)
.await
.with_context(|| {
format!(
"Failed to read pipeline file: {}",
pipeline_path.display()
)
})?;
// Compare ignoring whitespace differences
if normalize_whitespace(&pipeline_yaml) != normalize_whitespace(&existing) {
anyhow::bail!(
"Integrity check failed: generated pipeline for '{}' does not match {}. \
Re-run compilation to update the pipeline file.",
front_matter.name,
pipeline_path.display()
);
}
println!("OK: {} is up to date", pipeline_path.display());
Ok(())
}
/// Normalize a string by removing all whitespace characters.
///
/// Used for integrity checks so that formatting-only differences
/// (trailing spaces, blank lines, indentation changes) are ignored.
fn normalize_whitespace(s: &str) -> String {
s.chars().filter(|c| !c.is_whitespace()).collect()
}
/// Clean up spacing artifacts in generated YAML.
///
/// After template placeholder replacement, empty placeholders leave behind
/// trailing whitespace and consecutive blank lines. This function:
/// 1. Strips trailing whitespace from each line
/// 2. Collapses runs of blank lines into a single blank line
/// 3. Trims leading/trailing blank lines from the file
fn clean_generated_yaml(yaml: &str) -> String {
let mut result = Vec::new();
let mut prev_blank = false;
for line in yaml.lines() {
let trimmed_end = line.trim_end();
let is_blank = trimmed_end.is_empty();
if is_blank && prev_blank {
continue;
}
result.push(trimmed_end);
prev_blank = is_blank;
}
// Trim leading/trailing blank lines
while result.first().is_some_and(|l| l.is_empty()) {
result.remove(0);
}
while result.last().is_some_and(|l| l.is_empty()) {
result.pop();
}
let mut output = result.join("\n");
output.push('\n');
output
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_minimal_front_matter() {
let content = r#"---
name: "Test Agent"
description: "A test agent"
---
## Instructions
Do something.
"#;
let (fm, body) = parse_markdown(content).unwrap();
assert_eq!(fm.name, "Test Agent");
assert_eq!(fm.description, "A test agent");
assert!(body.contains("## Instructions"));
}
#[test]
fn test_parse_with_target() {
let content = r#"---
name: "1ES Agent"
description: "Uses 1ES"
target: 1es
---
Body
"#;
let (fm, _) = parse_markdown(content).unwrap();
assert_eq!(fm.target, CompileTarget::OneES);
}
#[test]
fn test_pool_string_format() {
let content = r#"---
name: "Agent"
description: "Test"
pool: my-custom-pool
---
Body
"#;
let (fm, _) = parse_markdown(content).unwrap();
let pool = fm.pool.unwrap();
assert_eq!(pool.name(), "my-custom-pool");
assert_eq!(pool.os(), "linux"); // default
}
#[test]
fn test_pool_object_format() {
let content = r#"---
name: "Agent"
description: "Test"
pool:
name: my-custom-pool
os: windows
---
Body
"#;
let (fm, _) = parse_markdown(content).unwrap();
let pool = fm.pool.unwrap();
assert_eq!(pool.name(), "my-custom-pool");
assert_eq!(pool.os(), "windows");
}
#[test]
fn test_schedule_string_form() {
let content = r#"---
name: "Agent"
description: "Test"
schedule: daily around 14:00
---
Body
"#;
let (fm, _) = parse_markdown(content).unwrap();
let schedule = fm.schedule.unwrap();
assert_eq!(schedule.expression(), "daily around 14:00");
assert!(schedule.branches().is_empty());
}
#[test]
fn test_schedule_object_form() {
let content = r#"---
name: "Agent"
description: "Test"
schedule:
run: weekly on friday around 17:00
branches:
- main
- release/*
---
Body
"#;
let (fm, _) = parse_markdown(content).unwrap();
let schedule = fm.schedule.unwrap();
assert_eq!(schedule.expression(), "weekly on friday around 17:00");
assert_eq!(schedule.branches(), &["main", "release/*"]);
}
#[test]
fn test_schedule_object_form_no_branches() {
let content = r#"---
name: "Agent"
description: "Test"
schedule:
run: daily
---
Body
"#;
let (fm, _) = parse_markdown(content).unwrap();
let schedule = fm.schedule.unwrap();
assert_eq!(schedule.expression(), "daily");
assert!(schedule.branches().is_empty());
}
#[test]
fn test_generate_checkout_self_no_branch() {
let result = common::generate_checkout_self();
assert_eq!(result, "- checkout: self");
}
#[test]
fn test_normalize_whitespace_strips_all_whitespace() {
assert_eq!(normalize_whitespace("a b c"), "abc");
assert_eq!(normalize_whitespace("a\n b\n c\n"), "abc");
assert_eq!(normalize_whitespace(" hello world "), "helloworld");
}
#[test]
fn test_normalize_whitespace_identical_content_matches() {
let a = "key: value\nother: data\n";
let b = "key: value\nother: data\n";
assert_eq!(normalize_whitespace(a), normalize_whitespace(b));
}
#[test]
fn test_normalize_whitespace_ignores_trailing_spaces() {
let a = "key: value\nother: data\n";
let b = "key: value \nother: data \n";
assert_eq!(normalize_whitespace(a), normalize_whitespace(b));
}
#[test]
fn test_normalize_whitespace_ignores_blank_lines() {
let a = "key: value\nother: data\n";
let b = "key: value\n\n\nother: data\n\n";
assert_eq!(normalize_whitespace(a), normalize_whitespace(b));
}
#[test]
fn test_normalize_whitespace_detects_content_difference() {
let a = "key: value1\n";
let b = "key: value2\n";
assert_ne!(normalize_whitespace(a), normalize_whitespace(b));
}
}