{{ if has "bitbucket" .Values.global.gitProviders }}
{{ if or ( .Values.pipelines.deployableResources.java.java8 ) ( .Values.pipelines.deployableResources.java.java11 ) ( .Values.pipelines.deployableResources.java.java17 )}}
{{- $raw := include "edp-tekton.resourceMapping.maven" . | fromYaml -}}
{{- $rawSonar := include "edp-tekton.resourceMapping.mavenSonar" . | fromYaml -}}
{{- range $framework, $image := $raw }}
{{- $sonarImage := pluck $framework $rawSonar | first -}}
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: bitbucket-maven-{{ $framework }}-aut-build-edp
  labels:
    app.edp.epam.com/pipelinetype: build
    {{- include "edp-tekton.labels" $ | nindent 4 }}
spec:
  description: "The Build pipeline for building {{ $framework }} automation tests with Maven (EDP versioning)"

  workspaces:
    - name: shared-workspace
    - name: ssh-creds

  params:
    - name: pipelineUrl
      default: https://portal-{{ $.Release.Namespace }}.{{ $.Values.global.dnsWildCard }}/c/main/pipelines/$(context.pipelineRun.namespace)/$(context.pipelineRun.name)
      type: string
    - name: git-source-url
      default: "https://github.com/sergk/spring-petclinic"
      description: git url to clone
      type: string
    - name: git-source-revision
      description: 'git revision to checkout (branch, tag, sha, ref…)'
      default: "edp"
      type: string
    - name: CODEBASE_NAME
      default: '{{ $framework }}-maven'
      description: "Project name"
      type: string
    - name: CODEBASEBRANCH_NAME
      description: "Codebasebranch name"
      type: string
    - name: changeNumber
      description: Change number from Merge Request
      default: ""
      type: string
    - name: gitfullrepositoryname
      description: "repository full name"
      type: string
    - name: sonar_image
      default: '{{ $sonarImage }}'
      description: "sonar image version"
      type: string
    - name: COMMIT_MESSAGE
      description: "Commit message"
      default: ""

{{- include "build-result" $ | nindent 2 }}

  tasks:

{{- include "bitbucket-build-start" $ | nindent 4 }}

    - name: get-version
      taskRef:
        kind: Task
        name: get-version-edp
      runAfter:
        - init-values
      params:
        - name: CODEBASEBRANCH_NAME
          value: $(params.CODEBASEBRANCH_NAME)

    - name: sonar
      taskRef:
        kind: Task
        name: sonarqube-maven
      runAfter:
        - get-version
      params:
        - name: SONAR_PROJECT_KEY
          value: $(params.CODEBASE_NAME)
        - name: SONAR_PROJECT_NAME
          value: $(params.CODEBASE_NAME)
        - name: MAVEN_IMAGE
          value: $(params.sonar_image)
        - name: EXTRA_COMMANDS
          value:
            - -B
            - '-Dsonar.projectKey=$(params.CODEBASE_NAME)'
            - '-Dsonar.projectName=$(params.CODEBASE_NAME)'
            - '-Dsonar.branch.name=$(params.git-source-revision)'
            - '-Dsonar.qualitygate.wait=true'
            - verify
            - sonar:sonar
      workspaces:
        - name: source
          workspace: shared-workspace

    - name: git-tag
      taskRef:
        kind: Task
        name: git-cli
      runAfter:
        - get-version
      params:
        - name: GIT_USER_EMAIL
          value: edp-ci@edp.ci-user
        - name: GIT_USER_NAME
          value: edp-ci
        - name: GIT_SCRIPT
          value: |
            git tag -a "$(tasks.get-version.results.VCS_TAG)" -m "Tag is added automatically by CI user"
            git push --tags
      workspaces:
        - name: source
          workspace: shared-workspace
          subPath: source
        - name: ssh-directory
          workspace: ssh-creds

  finally:

{{ include "bitbucket-build-vote" . | nindent 4 }}
---
{{ end }}
{{ end }}
{{ end }}
