[code-simplifier] refactor: extract awfVersionAtLeast helper to deduplicate version-check functions#31627
Conversation
…ck functions The four awfSupports* functions (ExcludeEnv, CliProxy, AllowHostPorts, DockerHostPathPrefix) shared identical structure: resolve the effective AWF version, handle 'latest' and default version, then compare against a minimum version. Extract awfVersionAtLeast() as the shared implementation and simplify each awfSupports* function to a one-liner. This eliminates ~60 lines of duplicate logic while preserving identical behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors AWF version feature-gating by extracting a shared awfVersionAtLeast helper and converting multiple near-identical awfSupports* functions into one-line wrappers.
Changes:
- Added
awfVersionAtLeasthelper to centralize AWF version comparison logic (incl. default version +"latest"handling). - Simplified
awfSupportsExcludeEnv,awfSupportsCliProxy,awfSupportsAllowHostPorts, andawfSupportsDockerHostPathPrefixto delegate to the helper. - Minor formatting cleanup in
NewCompilerstruct initialization.
Show a summary per file
| File | Description |
|---|---|
| pkg/workflow/awf_helpers.go | Extracts shared version-check helper and refactors AWF feature support checks to use it |
| pkg/workflow/compiler_types.go | Formatting-only adjustment in compiler initialization |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/2 changed files
- Comments generated: 1
| minVersion := string(constants.AWFCliProxyMinVersion) | ||
| return semverutil.Compare(versionStr, minVersion) >= 0 | ||
| // awfSupportsCliProxy returns true when the effective AWF version supports --difc-proxy-host | ||
| // and --difc-proxy-ca-cert (introduced in AWF v0.26.0). |
There was a problem hiding this comment.
Skills-Based Review 🧠
Applied /zoom-out and /improve-codebase-architecture — appropriate for this refactor that consolidates four near-identical version-check functions into a shared helper.
Key Themes
- Solid deduplication —
awfVersionAtLeastis a genuinely deep module: a small interface hiding the fallback-to-default /"latest"/ semver-compare logic that was previously copy-pasted four times. - One implicit invariant — the behavioral equivalence for the nil-config path in
awfSupportsExcludeEnvdepends onDefaultFirewallVersion >= AWFExcludeEnvMinVersion, which is not tested (see inline comment). - Helper declaration order — minor readability nit:
awfVersionAtLeastis currently declared after its first caller (see inline comment).
Positive Highlights
- ✅ Deletion test passes: deleting the four old bodies would have scattered the
latest/semver/fallback logic back across four callers — the new helper clearly earns its keep. - ✅ All existing tests pass and cover the four public wrappers thoroughly.
- ✅ The PR description honestly flags the one subtle equivalence assumption and explains why it holds.
- ✅ Clean commit — no unrelated changes.
Verdict
Approving. The two inline observations are non-blocking suggestions; the refactor is correct and improves locality.
🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · ● 4.3M
| func awfSupportsCliProxy(firewallConfig *FirewallConfig) bool { | ||
| // If firewallConfig has no version set, DefaultFirewallVersion is used. "latest" always | ||
| // returns true. Non-semver strings (e.g. branch names) return false (conservative). | ||
| func awfVersionAtLeast(firewallConfig *FirewallConfig, minVersion constants.Version) bool { |
There was a problem hiding this comment.
[/improve-codebase-architecture] The helper is declared after its first caller (awfSupportsExcludeEnv at line 638). In Go the compiler does not care about declaration order within a package, but by convention helper functions are defined before the functions that use them (or at least grouped together). Moving awfVersionAtLeast above awfSupportsExcludeEnv would make the file easier to read top-to-bottom without having to scroll down to find what a one-liner delegates to.
| // Normalise the v-prefix for semverutil.Compare. | ||
| minVersion := string(constants.AWFExcludeEnvMinVersion) | ||
| return semverutil.Compare(versionStr, minVersion) >= 0 | ||
| return awfVersionAtLeast(firewallConfig, constants.AWFExcludeEnvMinVersion) |
There was a problem hiding this comment.
[/zoom-out] The behavioral equivalence for awfSupportsExcludeEnv with a nil/empty config relies on the invariant DefaultFirewallVersion >= AWFExcludeEnvMinVersion (currently v0.25.43 >= v0.25.3 ✅). This invariant is implicit — nothing enforces it at compile-time or in tests.
If DefaultFirewallVersion were ever rolled back below v0.25.3, this function would silently return false for unconfigured workflows instead of true, a subtle behavioral regression. Consider asserting it in the constants spec test:
// DefaultFirewallVersion must be >= all feature-flag minimum versions
assert.GreaterOrEqual(t, semverutil.Compare(string(DefaultFirewallVersion), string(AWFExcludeEnvMinVersion)), 0)This makes the invariant visible and caught at test time rather than discovered in production.
Code Simplification - 2026-05-12
This PR simplifies recently introduced code to improve clarity and maintainability while preserving all functionality.
Files Simplified
pkg/workflow/awf_helpers.go— extracted sharedawfVersionAtLeasthelper, replaced four near-identical version-check functions with one-liner wrappersImprovements Made
Eliminated ~60 lines of duplicate logic
The four
awfSupports*functions (ExcludeEnv,CliProxy,AllowHostPorts,DockerHostPathPrefix) were structurally identical:Replaced with a shared helper:
Changes Based On
Recent changes from:
--docker-host-path-prefixin generated workflows #31614 — Auto-detect ARC/DinD and emit AWF--docker-host-path-prefixin generated workflows (addedawfSupportsDockerHostPathPrefix, making the 4th near-identical function)Testing
TestAWFSupportsExcludeEnv— all 8 cases passTestAWFSupportsCliProxy— all cases passTestAWFSupportsAllowHostPorts— all 7 cases passTestAWFSupportsDockerHostPathPrefix— all 5 cases passmake build)Review Focus
Please verify:
awfVersionAtLeastcorrectly captures the shared behavior of all four original functionsawfSupportsExcludeEnvsimplification: the old code returnedtrueimmediately for nil/empty config (instead of using DefaultFirewallVersion). This is semantically equivalent sinceDefaultFirewallVersion >= AWFExcludeEnvMinVersion, as confirmed by all existing tests passing.Automated by Code Simplifier Agent - analyzing code from the last 24 hours