|
| 1 | +//! Job-level ADO template compiler. |
| 2 | +//! |
| 3 | +//! This compiler generates a reusable ADO YAML template with `jobs:` at root. |
| 4 | +//! Users include it in their existing pipelines via `- template: <path>`. |
| 5 | +//! |
| 6 | +//! Two inclusion patterns: |
| 7 | +//! - Directly in a flat pipeline's `jobs:` list |
| 8 | +//! - Inside a user-defined stage's `jobs:` list |
| 9 | +
|
| 10 | +use anyhow::{Context, Result}; |
| 11 | +use async_trait::async_trait; |
| 12 | +use log::{info, warn}; |
| 13 | +use std::path::Path; |
| 14 | + |
| 15 | +use super::Compiler; |
| 16 | +use super::common::{ |
| 17 | + AWF_VERSION, MCPG_VERSION, MCPG_IMAGE, MCPG_PORT, MCPG_DOMAIN, |
| 18 | + CompileConfig, compile_shared, |
| 19 | + generate_allowed_domains, |
| 20 | + generate_awf_mounts, |
| 21 | + generate_awf_path_step, |
| 22 | + collect_awf_path_prepends, |
| 23 | + generate_enabled_tools_args, |
| 24 | + generate_mcpg_config, generate_mcpg_docker_env, generate_mcpg_step_env, |
| 25 | + generate_stage_prefix, generate_template_parameters, |
| 26 | + generate_header_comment, |
| 27 | +}; |
| 28 | +use super::types::FrontMatter; |
| 29 | + |
| 30 | +/// Job-level template compiler. |
| 31 | +pub struct JobCompiler; |
| 32 | + |
| 33 | +#[async_trait] |
| 34 | +impl Compiler for JobCompiler { |
| 35 | + fn target_name(&self) -> &'static str { |
| 36 | + "job" |
| 37 | + } |
| 38 | + |
| 39 | + async fn compile( |
| 40 | + &self, |
| 41 | + input_path: &Path, |
| 42 | + output_path: &Path, |
| 43 | + front_matter: &FrontMatter, |
| 44 | + markdown_body: &str, |
| 45 | + skip_integrity: bool, |
| 46 | + debug_pipeline: bool, |
| 47 | + ) -> Result<String> { |
| 48 | + info!("Compiling for job template target"); |
| 49 | + |
| 50 | + if front_matter.on_config.is_some() { |
| 51 | + warn!("on: trigger configuration is ignored for target: job (triggers are the parent pipeline's concern)"); |
| 52 | + } |
| 53 | + |
| 54 | + // Collect extensions (needed before compile_shared for MCPG config) |
| 55 | + let extensions = super::extensions::collect_extensions(front_matter); |
| 56 | + |
| 57 | + // Build compile context for MCPG config generation |
| 58 | + let input_dir = input_path.parent().unwrap_or(std::path::Path::new(".")); |
| 59 | + let ctx = super::extensions::CompileContext::new(front_matter, input_dir).await?; |
| 60 | + |
| 61 | + // Generate stage prefix for job-name uniqueness |
| 62 | + let stage_prefix = generate_stage_prefix(&front_matter.name); |
| 63 | + |
| 64 | + // Generate template-level parameters |
| 65 | + let template_params = generate_template_parameters(front_matter)?; |
| 66 | + |
| 67 | + // Same AWF/MCPG values as standalone |
| 68 | + let allowed_domains = generate_allowed_domains(front_matter, &extensions)?; |
| 69 | + let awf_mounts = generate_awf_mounts(&extensions); |
| 70 | + let awf_paths = collect_awf_path_prepends(&extensions); |
| 71 | + let awf_path_step = generate_awf_path_step(&awf_paths); |
| 72 | + let enabled_tools_args = generate_enabled_tools_args(front_matter); |
| 73 | + |
| 74 | + let config_obj = generate_mcpg_config(front_matter, &ctx, &extensions)?; |
| 75 | + let mcpg_config_json = |
| 76 | + serde_json::to_string_pretty(&config_obj).context("Failed to serialize MCPG config")?; |
| 77 | + let mcpg_docker_env = generate_mcpg_docker_env(front_matter, &extensions); |
| 78 | + let mcpg_step_env = generate_mcpg_step_env(&extensions); |
| 79 | + |
| 80 | + let config = CompileConfig { |
| 81 | + template: include_str!("../data/job-base.yml").to_string(), |
| 82 | + extra_replacements: vec![ |
| 83 | + ("{{ stage_prefix }}".into(), stage_prefix), |
| 84 | + ("{{ template_parameters }}".into(), template_params), |
| 85 | + ("{{ firewall_version }}".into(), AWF_VERSION.into()), |
| 86 | + ("{{ mcpg_version }}".into(), MCPG_VERSION.into()), |
| 87 | + ("{{ mcpg_image }}".into(), MCPG_IMAGE.into()), |
| 88 | + ("{{ mcpg_port }}".into(), MCPG_PORT.to_string()), |
| 89 | + ("{{ mcpg_domain }}".into(), MCPG_DOMAIN.into()), |
| 90 | + ("{{ allowed_domains }}".into(), allowed_domains), |
| 91 | + ("{{ awf_mounts }}".into(), awf_mounts), |
| 92 | + ("{{ awf_path_step }}".into(), awf_path_step), |
| 93 | + ("{{ enabled_tools_args }}".into(), enabled_tools_args), |
| 94 | + ("{{ mcpg_config }}".into(), mcpg_config_json), |
| 95 | + ("{{ mcpg_docker_env }}".into(), mcpg_docker_env), |
| 96 | + ("{{ mcpg_step_env }}".into(), mcpg_step_env), |
| 97 | + ], |
| 98 | + skip_integrity, |
| 99 | + debug_pipeline, |
| 100 | + has_awf_paths: !awf_paths.is_empty(), |
| 101 | + skip_header: true, |
| 102 | + }; |
| 103 | + |
| 104 | + let yaml = compile_shared( |
| 105 | + input_path, output_path, front_matter, markdown_body, |
| 106 | + &extensions, &ctx, config, |
| 107 | + ).await?; |
| 108 | + |
| 109 | + // Prepend custom header with job-template usage instructions |
| 110 | + let header = generate_job_header(input_path, front_matter); |
| 111 | + Ok(format!("{}{}", header, yaml)) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// Generate the header comment block for job-level templates. |
| 116 | +fn generate_job_header(input_path: &Path, front_matter: &FrontMatter) -> String { |
| 117 | + let base_header = generate_header_comment(input_path); |
| 118 | + let source_path = input_path |
| 119 | + .to_string_lossy() |
| 120 | + .replace('\\', "/"); |
| 121 | + |
| 122 | + let mut header = base_header; |
| 123 | + header.push_str("#\n"); |
| 124 | + header.push_str("# Job-level ADO template. Include in your pipeline:\n"); |
| 125 | + header.push_str("#\n"); |
| 126 | + header.push_str(&format!("# jobs:\n")); |
| 127 | + header.push_str(&format!("# - template: {}\n", source_path.replace(".md", ".lock.yml"))); |
| 128 | + header.push_str("#\n"); |
| 129 | + header.push_str("# Or inside a stage in a multi-stage pipeline:\n"); |
| 130 | + header.push_str("#\n"); |
| 131 | + header.push_str("# stages:\n"); |
| 132 | + header.push_str("# - stage: AgenticReview\n"); |
| 133 | + header.push_str("# dependsOn: Build\n"); |
| 134 | + header.push_str("# jobs:\n"); |
| 135 | + header.push_str(&format!("# - template: {}\n", source_path.replace(".md", ".lock.yml"))); |
| 136 | + |
| 137 | + // Document required resources if agent uses repos |
| 138 | + if !front_matter.repos.is_empty() || !front_matter.repositories.is_empty() { |
| 139 | + header.push_str("#\n"); |
| 140 | + header.push_str("# Add these repositories to your pipeline's resources: block:\n"); |
| 141 | + header.push_str("#\n"); |
| 142 | + header.push_str("# resources:\n"); |
| 143 | + header.push_str("# repositories:\n"); |
| 144 | + for repo in &front_matter.repositories { |
| 145 | + header.push_str(&format!("# - repository: {}\n", repo.repository)); |
| 146 | + header.push_str(&format!("# type: {}\n", repo.repo_type)); |
| 147 | + header.push_str(&format!("# name: {}\n", repo.name)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + header.push('\n'); |
| 152 | + header |
| 153 | +} |
| 154 | + |
| 155 | +#[cfg(test)] |
| 156 | +mod tests { |
| 157 | + use super::*; |
| 158 | + use crate::compile::common::generate_stage_prefix; |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn test_generate_stage_prefix_basic() { |
| 162 | + assert_eq!(generate_stage_prefix("Daily Code Review"), "DailyCodeReview"); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn test_generate_stage_prefix_hyphens() { |
| 167 | + assert_eq!(generate_stage_prefix("my-agent-123"), "MyAgent123"); |
| 168 | + } |
| 169 | + |
| 170 | + #[test] |
| 171 | + fn test_generate_stage_prefix_empty() { |
| 172 | + assert_eq!(generate_stage_prefix(""), "Agent"); |
| 173 | + } |
| 174 | + |
| 175 | + #[test] |
| 176 | + fn test_generate_stage_prefix_leading_digit() { |
| 177 | + assert_eq!(generate_stage_prefix("123start"), "_123start"); |
| 178 | + } |
| 179 | + |
| 180 | + #[test] |
| 181 | + fn test_generate_stage_prefix_single_word() { |
| 182 | + assert_eq!(generate_stage_prefix("review"), "Review"); |
| 183 | + } |
| 184 | + |
| 185 | + #[test] |
| 186 | + fn test_generate_stage_prefix_underscores() { |
| 187 | + assert_eq!(generate_stage_prefix("code_review_agent"), "CodeReviewAgent"); |
| 188 | + } |
| 189 | +} |
0 commit comments