{{- if not (lookup "batch/v1" "Job" .Release.Namespace "gerrit-configure-all-projects-repo") -}}
apiVersion: batch/v1
kind: Job
metadata:
  name: gerrit-configure-all-projects-repo-patched
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ .Values.gerrit.name }}
    {{- include "gerrit-operator.metaLabels" . | nindent 4 }}
spec:
  template:
    metadata:
      name: gerrit-configure-all-projects-repo
    spec:
      initContainers:
        - name: gerrit-ready
          image: {{ template "imageRegistry" . -}} curlimages/curl
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - '-c'
            - >-
              set -x;
              while [ $(curl -sw '%{http_code}' {{ template "admin-tools.gerritUrl" . }}/projects/ -o /dev/null) -ne 200 ]; do
              sleep 10; done;
      containers:
        - name: gerrit-configure-all-projects-repo
          image: {{ template "imageRegistry" . -}} bitnami/git
          imagePullPolicy: IfNotPresent
          command: ["/bin/bash", "-c", "/tmp/scripts/configure_all_project_repo.sh"]
          volumeMounts:
            - name: script-configure-all-projects-repo
              mountPath: /tmp/scripts/configure_all_project_repo.sh
              subPath: configure_all_project_repo.sh
          env:
            - name: GERRIT_ADMIN_USERNAME
              valueFrom:
                secretKeyRef:
                  name: gerrit-admin-password
                  key: user
            - name: GERRIT_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: gerrit-admin-password
                  key: password
      volumes:
        - name: script-configure-all-projects-repo
          configMap:
            name: script-configure-all-projects-repo
            defaultMode: 0755
      restartPolicy: Never
      serviceAccountName: {{ .Values.gerrit.serviceAccountName }}
{{- end -}}
