|
| 1 | +//! `pool: <string>` → explicit object form |
| 2 | +//! |
| 3 | +//! Non-1ES targets now support both self-hosted (`name`) and |
| 4 | +//! Microsoft-hosted (`vmImage`) pool syntax. This codemod rewrites the |
| 5 | +//! legacy scalar shorthand into an explicit object form so sources are |
| 6 | +//! unambiguous and easier to evolve. |
| 7 | +//! |
| 8 | +//! When the pool field is **absent** and the compiler version is at or |
| 9 | +//! above `INTRODUCED_IN` (the release that changed the implicit |
| 10 | +//! default from the 1ES self-hosted pool to `vmImage: ubuntu-latest`), |
| 11 | +//! the codemod pins the legacy default explicitly so existing |
| 12 | +//! pipelines are not silently broken. |
| 13 | +
|
| 14 | +use anyhow::Result; |
| 15 | +use serde_yaml::{Mapping, Value}; |
| 16 | + |
| 17 | +use super::{Codemod, CodemodContext}; |
| 18 | +use crate::compile::common::DEFAULT_ONEES_POOL; |
| 19 | + |
| 20 | +/// Version where the pool default changed from the legacy self-hosted |
| 21 | +/// pool to `vmImage: ubuntu-latest`. |
| 22 | +const INTRODUCED_IN: &str = "0.30.0"; |
| 23 | + |
| 24 | +pub static CODEMOD: Codemod = Codemod { |
| 25 | + id: "pool_object_form", |
| 26 | + summary: "pool: <string> -> pool object form (name/vmImage)", |
| 27 | + introduced_in: INTRODUCED_IN, |
| 28 | + apply: apply_codemod, |
| 29 | +}; |
| 30 | + |
| 31 | +/// Simple major.minor.patch comparison. Returns true when `version` |
| 32 | +/// is greater than or equal to `threshold`. |
| 33 | +fn version_gte(version: &str, threshold: &str) -> bool { |
| 34 | + let parse = |s: &str| -> (u32, u32, u32) { |
| 35 | + let mut parts = s.split('.'); |
| 36 | + let major = parts.next().and_then(|p| p.parse().ok()).unwrap_or(0); |
| 37 | + let minor = parts.next().and_then(|p| p.parse().ok()).unwrap_or(0); |
| 38 | + let patch = parts.next().and_then(|p| p.parse().ok()).unwrap_or(0); |
| 39 | + (major, minor, patch) |
| 40 | + }; |
| 41 | + parse(version) >= parse(threshold) |
| 42 | +} |
| 43 | + |
| 44 | +fn apply_codemod(fm: &mut Mapping, ctx: &CodemodContext) -> Result<bool> { |
| 45 | + let key = Value::String("pool".to_string()); |
| 46 | + |
| 47 | + let Some(pool_value) = fm.get(&key).cloned() else { |
| 48 | + // Pool absent — only inject the legacy default when the |
| 49 | + // compiler version is at or above the release that changed |
| 50 | + // the implicit default. Older binaries still carry the old |
| 51 | + // default in `resolve_pool_block`, so no rewrite is needed. |
| 52 | + if !version_gte(ctx.compiler_version, INTRODUCED_IN) { |
| 53 | + return Ok(false); |
| 54 | + } |
| 55 | + let mut mapped = Mapping::new(); |
| 56 | + mapped.insert( |
| 57 | + Value::String("name".to_string()), |
| 58 | + Value::String(DEFAULT_ONEES_POOL.to_string()), |
| 59 | + ); |
| 60 | + fm.insert(key, Value::Mapping(mapped)); |
| 61 | + return Ok(true); |
| 62 | + }; |
| 63 | + |
| 64 | + let Value::String(name) = pool_value else { |
| 65 | + // Already object-form (or invalid in another way) — no-op. |
| 66 | + return Ok(false); |
| 67 | + }; |
| 68 | + |
| 69 | + let mut mapped = Mapping::new(); |
| 70 | + mapped.insert(Value::String("name".to_string()), Value::String(name)); |
| 71 | + fm.insert(key, Value::Mapping(mapped)); |
| 72 | + Ok(true) |
| 73 | +} |
| 74 | + |
| 75 | +#[cfg(test)] |
| 76 | +mod tests { |
| 77 | + use super::*; |
| 78 | + |
| 79 | + /// Build a context with an explicit version for testing. |
| 80 | + fn ctx(version: &'static str) -> CodemodContext { |
| 81 | + CodemodContext { |
| 82 | + compiler_version: version, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn rewrites_scalar_pool_to_name_object() { |
| 88 | + let mut fm: Mapping = serde_yaml::from_str("name: x\ndescription: y\npool: MyPool").unwrap(); |
| 89 | + let changed = apply_codemod(&mut fm, &ctx("0.30.0")).expect("apply"); |
| 90 | + assert!(changed); |
| 91 | + assert_eq!( |
| 92 | + fm.get(Value::String("pool".into())).cloned(), |
| 93 | + Some(serde_yaml::from_str::<Value>("name: MyPool").unwrap()) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn noops_when_pool_is_already_mapping() { |
| 99 | + let mut fm: Mapping = |
| 100 | + serde_yaml::from_str("name: x\ndescription: y\npool:\n vmImage: ubuntu-latest") |
| 101 | + .unwrap(); |
| 102 | + let changed = apply_codemod(&mut fm, &ctx("0.30.0")).expect("apply"); |
| 103 | + assert!(!changed); |
| 104 | + assert_eq!( |
| 105 | + fm.get(Value::String("pool".into())).cloned(), |
| 106 | + Some(serde_yaml::from_str::<Value>("vmImage: ubuntu-latest").unwrap()) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn inserts_legacy_default_when_pool_absent_and_version_gte() { |
| 112 | + let mut fm: Mapping = serde_yaml::from_str("name: x\ndescription: y").unwrap(); |
| 113 | + let changed = apply_codemod(&mut fm, &ctx("0.30.0")).expect("apply"); |
| 114 | + assert!(changed); |
| 115 | + assert_eq!( |
| 116 | + fm.get(Value::String("pool".into())).cloned(), |
| 117 | + Some(serde_yaml::from_str::<Value>("name: AZS-1ES-L-MMS-ubuntu-22.04").unwrap()) |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn noops_when_pool_absent_and_version_below() { |
| 123 | + let mut fm: Mapping = serde_yaml::from_str("name: x\ndescription: y").unwrap(); |
| 124 | + let changed = apply_codemod(&mut fm, &ctx("0.29.0")).expect("apply"); |
| 125 | + assert!(!changed); |
| 126 | + assert!(!fm.contains_key(Value::String("pool".into()))); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn idempotent_after_inserting_legacy_default() { |
| 131 | + let mut fm: Mapping = serde_yaml::from_str("name: x\ndescription: y").unwrap(); |
| 132 | + let changed1 = apply_codemod(&mut fm, &ctx("0.30.0")).expect("first apply"); |
| 133 | + assert!(changed1); |
| 134 | + let changed2 = apply_codemod(&mut fm, &ctx("0.30.0")).expect("second apply"); |
| 135 | + assert!(!changed2, "second run must be a no-op"); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn rewrites_legacy_default_pool_to_name_object() { |
| 140 | + let mut fm: Mapping = |
| 141 | + serde_yaml::from_str("name: x\ndescription: y\npool: AZS-1ES-L-MMS-ubuntu-22.04") |
| 142 | + .unwrap(); |
| 143 | + let changed = apply_codemod(&mut fm, &ctx("0.30.0")).expect("apply"); |
| 144 | + assert!(changed); |
| 145 | + assert_eq!( |
| 146 | + fm.get(Value::String("pool".into())).cloned(), |
| 147 | + Some(serde_yaml::from_str::<Value>("name: AZS-1ES-L-MMS-ubuntu-22.04").unwrap()) |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn scalar_rewrite_applies_regardless_of_version() { |
| 153 | + // Scalar → object rewrite is unconditional; only the |
| 154 | + // absent-pool injection is version-gated. |
| 155 | + let mut fm: Mapping = |
| 156 | + serde_yaml::from_str("name: x\ndescription: y\npool: MyPool").unwrap(); |
| 157 | + let changed = apply_codemod(&mut fm, &ctx("0.28.0")).expect("apply"); |
| 158 | + assert!(changed); |
| 159 | + assert_eq!( |
| 160 | + fm.get(Value::String("pool".into())).cloned(), |
| 161 | + Some(serde_yaml::from_str::<Value>("name: MyPool").unwrap()) |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn version_gte_comparisons() { |
| 167 | + assert!(version_gte("0.30.0", "0.30.0")); |
| 168 | + assert!(version_gte("0.31.0", "0.30.0")); |
| 169 | + assert!(version_gte("1.0.0", "0.30.0")); |
| 170 | + assert!(version_gte("0.30.1", "0.30.0")); |
| 171 | + assert!(!version_gte("0.29.0", "0.30.0")); |
| 172 | + assert!(!version_gte("0.29.99", "0.30.0")); |
| 173 | + } |
| 174 | +} |
0 commit comments