Argo Events: Use data filter in sensor to identify modified/added/removed path in mono-repo

Issue

I’m using Argo Events and Argo Workflow for my CI/CD chain, which works pretty neat. But I’m having some troubles setting up the data filter for the GitHub webhook payloads of my mono repo.

I’m trying to let the sensor only trigger the defined workflow if files were changed in a certain subpath. The payload contains three fields added, removed, modified. There the files are listed which were changed in this commit (webhook-events-and-payloads#push).

The paths I’m searching for is service/jobs/* and service/common*/*.

The filter I defined is:

          - path: "[commits.#.modified.#(%\"*service*\")#,commits.#.added.#(%\"*service*\")#,commits.#.removed.#(%\"*service*\")#]"
            type: string
            value:
              - "(\bservice/jobs\b)|(\bservice/common*)"

I valididated my filter in a tiny Go script, as gjson is used by Argo Events to apply the data filter.

package main

import (
    "github.com/tidwall/gjson"
    "regexp"
)

const json = `{
    "commits": [
      {
        "added": [
  
        ],
        "removed": [
  
        ],
        "modified": [
          "service/job-manager/README.md"
        ]
      },
      {
        "added": [
  
        ],
        "removed": [
            "service/joby/something.md"
        ],
        "modified": [
          "service/job-manager/something.md"
        ]
      },
      {
        "added": [
  
        ],
        "removed": [
            "service/joby/something.md"
        ],
        "modified": [
          "service/joby/someother.md"
        ]
      }
    ],
    "head_commit": {
      "added": [
        "service/job-manager/something.md"
      ],
      "removed": [
        "service/joby/something.md"
      ],
      "modified": [
        "service/job-manager/README.md"
      ]
    }
  }`

func main() {
    value := gjson.Get(json, "[commits.#.modified.#(%\"*service*\")#,commits.#.added.#(%\"*service*\")#,commits.#.removed.#(%\"*service*\")#]")
    println(value.String())

    matched, _ := regexp.MatchString(`(\bservice/job-manager\b)|(\bservice/common*)`, value.String())
    println(matched) // string is contained?
}

The script gives me the results I expect. But for the same webhook payload the workflow is not triggered, when adding the data filter to the sensor.

Someone any ideas?


UPDATED:

Thanks for the hint incl. body. in the paths.

I ended up setting the filters:

- path: "[body.commits.#.modified.#()#,body.commits.#.added.#()#,body.commits.#.removed.#()#]"
  type: string
  value:
    - ".*service/jobs.*"
    - ".*service/common.*"

Solution

  • Path shoud start with body.
  • Value should add escape special character with \\

So the data filter should be

- path: "[body.commits.#.modified.#(%\"*service*\")#,body.commits.#.added.#(%\"*service*\")#,body.commits.#.removed.#(%\"*service*\")#]"
  type: string
  value:
    - "(\\bservice/jobs\\b)|(\\bservice/common*)"

Answered By – quang nguyen

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.