---
apiVersion: batch/v1
kind: Job
metadata:
  name: argocd-config
  namespace: argocd
spec:
  template:
    metadata:
      generateName: argocd-config-
    spec:
      restartPolicy: OnFailure
      containers:
        - name: push
          image: docker.io/library/ubuntu:22.04
          env:
            - name: FORGEJO_USER
              valueFrom:
                secretKeyRef:
                  name: forgejo-access-token
                  key: forgejo_username
            - name: FORGEJO_TOKEN
              valueFrom:
                secretKeyRef:
                  name: forgejo-access-token
                  key: forgejo_token
          command: ["/bin/bash", "-c"]
          args:
            - |
              #! /bin/bash

              apt -qq update
              apt -qq install git wget -y
              if [[ "$(uname -m)" == "x86_64" ]]; then
                wget https://github.com/mikefarah/yq/releases/download/v4.44.3/yq_linux_amd64
                install yq_linux_amd64 /usr/local/bin/yq
                rm yq_linux_amd64
              else
                wget https://github.com/mikefarah/yq/releases/download/v4.44.3/yq_linux_arm64
                install yq_linux_arm64 /usr/local/bin/yq
                rm yq_linux_arm64
              fi

              git config --global user.email "bot@bots.de"
              git config --global user.name "bot"
              
              git clone https://${FORGEJO_USER}:${FORGEJO_TOKEN}@gitea.otc.c-one-infra.de/giteaAdmin/edfbuilder.git
              cd edfbuilder
              yq eval '.configs.cm."oidc.config" = "name: Keycloak\nissuer: https://otc.c-one-infra.de/keycloak/realms/cnoe\nclientID: argocd\nclientSecret: $auth-generic-oauth-secret:client_secret\nrequestedScopes: [\"openid\", \"profile\", \"email\", \"groups\"]"' -i stacks/core/argocd/values.yaml
              
              git add stacks/core/argocd/values.yaml
              git commit -m "adds Forgejo SSO config"
              git push
  backoffLimit: 99           