0

I want to append a new key: value pair in one of .yaml file as below:-

stages:
  - template: myproject/scops-java-service-v1.yaml@codeway
    parameters:
    **newKey: newValue**
      lintingParams:
        skipSchemaValidation: false

I tried with Yaml AppendToSequence recipe, but no change as result:

recipeList:
  - org.openrewrite.yaml.AppendToSequence:
      sequencePath: $.stages.parameters
      value: 'newKey: newValue'
#     existingSequenceValues: existingValue1
#     matchExistingSequenceValuesInAnyOrder: true

Also, can we chose a specific .yaml file in case we have multiple .yaml files in a single project?

1 Answers1

0

You might want to try with MergeYaml instead of AppendToSequence: https://docs.openrewrite.org/recipes/yaml/mergeyaml

Here's a link to an example that might be of use: https://github.com/openrewrite/rewrite/blob/e77eabd1cbe3479de4f4d6e52728db2d6877399d/rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java#L113

    @DocumentExample
    @Test
    void nonExistentBlock() {
        rewriteRun(
          spec -> spec.recipe(new MergeYaml(
            "$.spec",
            //language=yaml
            """
              lifecycleRule:
                  - action:
                        type: Delete
                    condition:
                        age: 7
              """,
            false,
            null
          )),
          yaml(
            """
              apiVersion: storage.cnrm.cloud.google.com/v1beta1
              kind: StorageBucket
              spec:
                  bucketPolicyOnly: true
              """,
            """
              apiVersion: storage.cnrm.cloud.google.com/v1beta1
              kind: StorageBucket
              spec:
                  bucketPolicyOnly: true
                  lifecycleRule:
                      - action:
                            type: Delete
                        condition:
                            age: 7
              """
          )
        );
    }

Or in Declarative YAML format:

recipeList:
  - org.openrewrite.yaml.MergeYaml:
      key: $.spec
      yaml: |-
        lifecycleRule:
          - action:
              type: Delete
            condition:
              age: 7
Tim
  • 19,793
  • 8
  • 70
  • 95
  • Thanks! Can we select a specific .yaml file for MergeYaml in case we have multiple .yaml files in a single project? Is there any declarative approach available ? – manish kumar Jul 20 '23 at 05:04
  • 1
    Not right now, so you'll want to be careful and be selective with your `sequencePath` matcher, but we're exploring options to allow recipes to only apply to certain files too. Keep an eye on the release notes & changelog for details there. – Tim Jul 21 '23 at 10:14