apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: git-commit-push
  labels:
    dagweave.io/catalog-item: git-commit-push
  annotations:
    dagweave.io/catalog-version: "1.0.2"
    dagweave.io/catalog-category: "Git"
spec:
  entrypoint: commit-push
  templates:
    - name: commit-push
      inputs:
        parameters:
          # Working directory containing the checked-out repo to commit.
          - name: path
            default: /work
          - name: commit-message
          - name: author-name
            default: workflow-bot
          - name: author-email
            default: workflow-bot@dagweave.com
          # Branch to push HEAD to.
          - name: branch
            default: main
          # Secret holding git credentials (username/password keys).
          - name: secret-name
            default: git-credentials
          - name: username-key
            default: username
          - name: password-key
            default: password
      container:
        # alpine/git:2.54.0
        image: alpine/git@sha256:ac6c828f15d82775d204d3d75112e1d34af8945da80ccf922713c9891ba68e7a
        command: [sh, -c]
        # Reproduces the old dagweave "Commit and push a working directory" blueprint: commit the
        # working tree and push it back to its remote. Same semantics — a no-op
        # exit 0 when there is nothing to commit, and `push origin HEAD:<branch>`.
        args:
          - |
            set -eu
            cd "${WORK_DIR}"
            git config user.name "${AUTHOR_NAME}"
            git config user.email "${AUTHOR_EMAIL}"
            git add -A
            git commit -m "${COMMIT_MESSAGE}" || { echo "nothing to commit"; exit 0; }
            git config credential.helper \
              '!f() { echo "username=${GIT_USER}"; echo "password=${GIT_PASSWORD}"; }; f'
            git push origin "HEAD:${BRANCH}"
        env:
          - name: WORK_DIR
            value: "{{inputs.parameters.path}}"
          - name: COMMIT_MESSAGE
            value: "{{inputs.parameters.commit-message}}"
          - name: AUTHOR_NAME
            value: "{{inputs.parameters.author-name}}"
          - name: AUTHOR_EMAIL
            value: "{{inputs.parameters.author-email}}"
          - name: BRANCH
            value: "{{inputs.parameters.branch}}"
          - name: GIT_USER
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.secret-name}}"
                key: "{{inputs.parameters.username-key}}"
          - name: GIT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.secret-name}}"
                key: "{{inputs.parameters.password-key}}"
