{{- define "drone.providerEnvs" -}}
{{- if eq .Values.sourceControl.provider "github" -}}
          - name: DRONE_GITHUB_CLIENT_ID
            value: {{ .Values.sourceControl.github.clientID }}
          - name: DRONE_GITHUB_SERVER
            value: {{ .Values.sourceControl.github.server }}
          - name: DRONE_GITHUB_CLIENT_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ template "drone.sourceControlSecret" . }}
                key: {{ .Values.sourceControl.github.clientSecretKey }}
{{- end -}}
{{- if eq .Values.sourceControl.provider "gitlab" -}}
          - name: DRONE_GITLAB_CLIENT_ID
            value: {{ .Values.sourceControl.gitlab.clientID }}
          - name: DRONE_GITLAB_SERVER
            value: {{ .Values.sourceControl.gitlab.server }}
          - name: DRONE_GITLAB_CLIENT_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ template "drone.sourceControlSecret" . }}
                key: {{ .Values.sourceControl.gitlab.clientSecretKey }}
{{- end -}}
{{- if eq .Values.sourceControl.provider "gitea" -}}
          - name: DRONE_GITEA_CLIENT_ID
            value: {{ .Values.sourceControl.gitea.clientID }}
          - name: DRONE_GITEA_SERVER
            value: {{ .Values.sourceControl.gitea.server }}
          - name: DRONE_GITEA_CLIENT_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ template "drone.sourceControlSecret" . }}
                key: {{ .Values.sourceControl.gitea.clientSecretKey }}
{{- end -}}
{{- if eq .Values.sourceControl.provider "gogs" -}}
          - name: DRONE_GOGS_SERVER
            value: {{ .Values.sourceControl.gogs.server }}
{{- end -}}
{{- if eq .Values.sourceControl.provider "bitbucketCloud" -}}
          - name: DRONE_BITBUCKET_CLIENT_ID
            value: {{ .Values.sourceControl.bitbucketCloud.clientID }}
          - name: DRONE_BITBUCKET_CLIENT_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ template "drone.sourceControlSecret" . }}
                key: {{ .Values.sourceControl.bitbucketCloud.clientSecretKey }}
{{- end -}}
{{- if eq .Values.sourceControl.provider "bitbucketServer" -}}
          - name: DRONE_STASH_SERVER
            value: {{ .Values.sourceControl.bitbucketServer.server }}
          - name: DRONE_GIT_USERNAME
            value: {{ .Values.sourceControl.bitbucketServer.username }}
          - name: DRONE_GIT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ template "drone.sourceControlSecret" . }}
                key: {{ .Values.sourceControl.bitbucketServer.passwordKey }}
          - name: DRONE_STASH_CONSUMER_KEY
            valueFrom:
              secretKeyRef:
                name: {{ template "drone.sourceControlSecret" . }}
                key: {{ .Values.sourceControl.bitbucketServer.consumerKey }}
          - name: DRONE_STASH_PRIVATE_KEY
            value: /etc/bitbucket/key.pem
{{- end -}}
{{- end -}}

{{/*
Check if a valid source control provider has been set
*/}}
{{- define "drone.providerOK" -}}
{{- if .Values.sourceControl.provider -}}
  {{- if eq .Values.sourceControl.provider "github" -}}
    {{- if and .Values.sourceControl.github.server .Values.sourceControl.github.clientID -}}
      true
    {{- end -}}
  {{- else if eq .Values.sourceControl.provider "gitlab" -}}
    {{- if and .Values.sourceControl.gitlab.server .Values.sourceControl.gitlab.clientID -}}
      true
    {{- end -}}
  {{- else if eq .Values.sourceControl.provider "gitea" -}}
    {{- if .Values.sourceControl.gitea.server -}}
      true
    {{- end -}}
  {{- else if eq .Values.sourceControl.provider "gogs" -}}
    {{- if .Values.sourceControl.gogs.server -}}
      true
    {{- end -}}
  {{- else if eq .Values.sourceControl.provider "bitbucketCloud" -}}
    {{- if .Values.sourceControl.bitbucketCloud.clientID -}}
      true
    {{- end -}}
  {{- else if eq .Values.sourceControl.provider "bitbucketServer" -}}
    {{- if and .Values.sourceControl.bitbucketServer.server .Values.sourceControl.bitbucketServer.username -}}
      true
    {{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
