Add MigrateOnlyToRules and MigrateExceptToRules recipes#38
Add MigrateOnlyToRules and MigrateExceptToRules recipes#38scuba10steve wants to merge 3 commits intomainfrom
Conversation
Replace deprecated `only`/`except` keywords with equivalent `rules` in .gitlab-ci.yml job definitions. Handles simple ref list forms; complex object forms are left unchanged for safety. Closes #31
| return new YamlIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public Yaml.Documents visitDocuments(Yaml.Documents documents, ExecutionContext ctx) { | ||
| if (!documents.getSourcePath().endsWith(".gitlab-ci.yml")) { |
There was a problem hiding this comment.
Tip: typically we do these checks via a FindSourceFiles used as a precondition, or even a named recipe like we have here:
https://github.com/openrewrite/rewrite-github-actions/blob/80040caa3cef2d568b38e14309d46e5a4a3f2c67/src/main/java/org/openrewrite/github/IsGitHubActionsWorkflow.java#L24-L36
There was a problem hiding this comment.
Took a stab at this.
| public Yaml.Mapping visitMapping(Yaml.Mapping mapping, ExecutionContext ctx) { | ||
| Yaml.Mapping m = super.visitMapping(mapping, ctx); | ||
|
|
||
| int exceptIdx = -1; |
There was a problem hiding this comment.
For some reason Claude loves using index fields instead of using ListUtils.map; I think the latter fits better here to do a one to one replacement of except with rules. Or do you think we'll ever see both except and only used, or one of those with preexisting rules ?
There was a problem hiding this comment.
I'll see what I can do to adjust this. Like in my other response, there are cases where both except and only can be present.
There was a problem hiding this comment.
Took a stab at this.
| script: make deploy | ||
| only: | ||
| - main | ||
| - tags |
There was a problem hiding this comment.
Do we ever expect to see both only and except used in one job? If so might be good to add a test for those cases too, as we don't want to end up with two rules blocks.
There was a problem hiding this comment.
There are cases where both can be present so yeah I can expand the test cases to include this situation.
…nd handle combined only+except - Replace visitDocuments file check with Preconditions.check(FindSourceFiles) - Replace index-based entry replacement with ListUtils.map - MigrateOnlyToRules now handles combined only+except on same job - MigrateExceptToRules defers to MigrateOnlyToRules when only is present - Extract shared extractRefs helper method
Summary
only/exceptkeywords to equivalentrulesin.gitlab-ci.ymljob definitions. Addresses issue Add search recipes for deprecatedonlyandexceptkeywords #31 suggestion to remove deprecated keywords rather than just find them.MigrateOnlyToRules: Replaces
only: [refs...]withrules: [{if: ...}]blocks.MigrateExceptToRules: Replaces
except: [refs...]withrules: [{if: ..., when: never}, {when: always}]blocks.Both recipes include a 11-ref-to-condition mapping (branches, tags, merge_requests, schedules, web, api, pipelines, triggers, pushes, regex patterns, literal branch names). Complex object forms with
refs,variables, orchangessub-keys are left unchanged for safety.