Skip to content

Add MigrateOnlyToRules and MigrateExceptToRules recipes#38

Open
scuba10steve wants to merge 3 commits intomainfrom
scuba10steve/remove-deprecated-keywords
Open

Add MigrateOnlyToRules and MigrateExceptToRules recipes#38
scuba10steve wants to merge 3 commits intomainfrom
scuba10steve/remove-deprecated-keywords

Conversation

@scuba10steve
Copy link
Collaborator

@scuba10steve scuba10steve commented Feb 26, 2026

Summary

MigrateOnlyToRules: Replaces only: [refs...] with rules: [{if: ...}] blocks.
MigrateExceptToRules: Replaces except: [refs...] with rules: [{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, or changes sub-keys are left unchanged for safety.

  • Includes comprehensive test coverage (19 new tests, all passing)
  • Integrated into BestPractices composite recipe
  • Added document examples for both recipes
  • All 76 existing + new tests pass with zero regressions

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")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a stab at this.

public Yaml.Mapping visitMapping(Yaml.Mapping mapping, ExecutionContext ctx) {
Yaml.Mapping m = super.visitMapping(mapping, ctx);

int exceptIdx = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a stab at this.

script: make deploy
only:
- main
- tags
Copy link
Member

@timtebeek timtebeek Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants