Remove .git in repo URL for ArgoCD notification webhook template

280 views Asked by At

I am trying to remove .git from the end of a repo URL, for example, https://gitlab.com/group/repo.git.

I am using Argo CD webhook notifications inside a Kubernetes ConfigMap:

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
data:
  service.webhook.velocity: |
    url: https://velocity.codeclimate.com/deploys
    headers:
    - name: Content-Type
      value: application/x-www-form-urlencoded

    template.velocity-update: |
      webhook:
        velocity:
          method: POST
          body: repository_url={{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS | call .strings.ReplaceAll "git" ""}}

  trigger.sync-operation-change: |
    - when: app.status.operationState.phase in ['Succeeded']
      oncePer: app.status.sync.revision
      send: [velocity-update]

According to documentation .strings.ReplaceAll is the one to use, however, when testing, I get the following output:

https://github.com/group/internal-platform-ops.gitghttps://github.com/group/repo.gitihttps://github.com/group/repo.gitthttps://github.com/group/repo.git

It looks like the text repeats. Unfortunately, the examples from Argo CD's documentation are limited.

Is there a way to make this work with ReplaceAll, or perhaps there is a better way?

1

There are 1 answers

0
Nikita Belonogov On

ReplaceAll require source string as a first argument https://pkg.go.dev/strings#ReplaceAll

You should ether:

{{ call .strings.ReplaceAll (.app.spec.source.repoURL | call .repo.RepoURLToHTTPS) ".git" "" }}

or just use a builtin trimSuffix to avoid unintentional replacements:

{{ .app.spec.source.repoURL | call .repo.RepoURLToHTTPS | trimSuffix ".git" }}