登录
首页 >  Golang >  Go问答

Argo Events:使用数据过滤器识别 mono-repo 中更改的路径

来源:stackoverflow

时间:2024-03-06 17:00:28 246浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《Argo Events:使用数据过滤器识别 mono-repo 中更改的路径》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我正在为我的 ci/cd 链使用 argo events 和 argo workflow,它的工作非常简洁。但我在为 mono 存储库的 github webhook 有效负载设置数据过滤器时遇到了一些麻烦。

我试图让传感器仅在某个子路径中的文件发生更改时触发定义的工作流程。有效负载包含三个字段添加删除修改。此处列出了在此提交中更改的文件 (webhook-events-and-payloads#push)。

我正在搜索的路径是 service/jobs/*service/common*/*

我定义的过滤器是:

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

我在一个小型 go 脚本中验证了我的过滤器,因为 argo events 使用 gjson 来应用数据过滤器。

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?
}

脚本给出了我期望的结果。但对于相同的 webhook 负载,在将数据过滤器添加到传感器时,不会触发工作流程。

有人有什么想法吗?

更新:

感谢您的提示,包括。路径中的body.

我最终设置了过滤器:

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

正确答案


  • 路径应以 body. 开头
  • 值应添加转义特殊字符 \\

所以数据过滤器应该是

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

以上就是《Argo Events:使用数据过滤器识别 mono-repo 中更改的路径》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>