{{ if has "bitbucket" .Values.global.gitProviders }}
{{ if or ( .Values.pipelines.deployableResources.python.flask ) ( .Values.pipelines.deployableResources.python.fastapi ) }}
{{- $raw := include "edp-tekton.resourceMapping.python" . | trimAll "[]" -}}
{{- $resources := $raw | split " " -}}
{{- range $framework := $resources }}
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: bitbucket-python-{{ $framework }}-app-build-default
  labels:
    app.edp.epam.com/pipelinetype: build
    {{- include "edp-tekton.labels" $ | nindent 4 }}
spec:
  description: "The Build pipeline for building {{ $framework }} application (default 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: "python-python-python-3.8"
      description: git url to clone
      type: string
    - name: git-source-revision
      description: 'git revision to checkout (branch, tag, sha, ref…)'
      default: "master"
      type: string
    - name: CODEBASE_NAME
      default: "python-app"
      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: TICKET_NAME_PATTERN
      description: "Ticket name pattern"
      default: ""
    - name: COMMIT_MESSAGE_PATTERN
      description: "Pattern to validate a commit message"
      default: ""
    - name: COMMIT_MESSAGE
      description: "Commit message"
      default: ""
    - name: JIRA_ISSUE_METADATA_PAYLOAD
      description: "Jira Payload"
      default: ""
    - name: JIRA_SERVER
      description: "Jira server name"
      default: ""

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

  tasks:

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

    - name: get-version
      taskRef:
        kind: Task
        name: get-version-default
      runAfter:
        - init-values
      params:
        - name: BRANCH_NAME
          value: $(params.git-source-revision)

{{- include "get-cache" $ | nindent 4 }}

    - name: update-build-number
      taskRef:
        kind: Task
        name: update-build-number-python-default
      runAfter:
        - get-version
      params:
        - name: VERSION
          value: $(tasks.get-version.results.VERSION)
      workspaces:
        - name: source
          workspace: shared-workspace
          subPath: source

{{ include "security" $ | nindent 4 }}

    - name: build
      taskRef:
        kind: Task
        name: python
      runAfter:
        - get-cache
      params:
        - name: EXTRA_COMMANDS
          value: |
            # we checkout the source code in the /source directory
            # and hold cache in the /cache directory

            # build
            python setup.py clean build sdist bdist_wheel

            # lint
            pip3 install -r test-requirements.txt
            pylint --output-format=colorized *.py
            flake8 --exclude .local --filename=*.py
            # test
            pytest -sv --color=yes
      workspaces:
        - name: source
          workspace: shared-workspace

    - name: sonar
      taskRef:
        kind: Task
        name: sonarqube-general
      runAfter:
        - build
      params:
        - name: SONAR_PROJECT_KEY
          value: $(params.CODEBASE_NAME)
        - name: SONAR_PROJECT_NAME
          value: $(params.CODEBASE_NAME)
        - name: branch
          value: $(params.git-source-revision)
      workspaces:
        - name: source
          workspace: shared-workspace
          subPath: source

    - name: push
      taskRef:
        kind: Task
        name: python
      runAfter:
        - sonar
      params:
        - name: EXTRA_COMMANDS
          value: |
            pip3 install -r test-requirements.txt
            python setup.py sdist

            # Get package version from the get-version task
            versionLowerCase=$(echo $(tasks.get-version.results.VERSION) | tr '[:upper:]' '[:lower:]')

            # # Define a repository for publishing the package
            if echo "$versionLowerCase" | grep -q "snapshot"; then
                TWINE_REPOSITORY_URL="${REPOSITORY_URL_SNAPSHOTS}"
            else
                TWINE_REPOSITORY_URL="${REPOSITORY_URL_RELEASES}"
            fi

            echo "[TEKTON][INFO] TWINE_REPOSITORY_URL contains ${TWINE_REPOSITORY_URL}"

            twine upload dist/*
      workspaces:
        - name: source
          workspace: shared-workspace

{{- include "build-pipeline-python-image-build-push-common" $ | nindent 4 }}

{{- include "save-cache-pyhton" $ | nindent 4 }}

{{ include "build-pipeline-end" $ | nindent 4 }}

{{ include "finally-block-default" $ | nindent 2 }}

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

---
{{- end }}
{{ end }}
{{ end }}
