{{- define "python-build-common" -}}
- name: build
  taskRef:
    kind: Task
    name: python
  runAfter:
    - update-build-number
    - get-cache
  params:
    - name: BASE_IMAGE
      value: $(params.image)
    - name: EXTRA_COMMANDS
      value: |
        # we checkout the source code in the /source directory
        # and hold cache in the /cache directory

        python setup.py clean build install --user
  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: BASE_IMAGE
      value: 'python:3.8-slim'
    - name: EXTRA_COMMANDS
      value: |
        pip install -r 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
{{- end -}}

{{- define "python-review-gerrit-common" -}}
- name: build
  taskRef:
    kind: Task
    name: python
  runAfter:
    - init-values
  params:
    - name: BASE_IMAGE
      value: $(params.image)
    - name: EXTRA_COMMANDS
      value: |
        # we checkout the source code in the /source directory
        # and hold cache in the /cache directory

        python setup.py clean build install --user
        pip3 install .
        [ -f run_service.py ] && python run_service.py &
        python setup.py pytest
  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: target-branch
      value: $(params.targetBranch)
    - name: source-branch
      value: $(params.git-refspec)
    - name: key-id
      value: $(params.changeNumber)
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source
{{- end -}}

{{- define "python-review-common" -}}
- name: build
  taskRef:
    kind: Task
    name: python
  runAfter:
    - init-values
  params:
    - name: BASE_IMAGE
      value: $(params.image)
    - name: EXTRA_COMMANDS
      value: |
        # we checkout the source code in the /source directory
        # and hold cache in the /cache directory

        python setup.py clean build install --user
        pip3 install .
        [ -f run_service.py ] && python run_service.py &
        python setup.py pytest
  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: target-branch
      value: $(params.targetBranch)
    - name: source-branch
      value: $(params.git-source-revision)
    - name: key-id
      value: $(params.changeNumber)
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source
{{- end -}}

{{- define "dockerbuild-verify-python" -}}
- name: dockerbuild-verify
  taskRef:
    kind: Task
    name: dockerbuild-verify
  runAfter:
    - sonar
    - dockerfile-lint
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source
{{- end -}}

{{- define "build-pipeline-python-image-build-push-common" -}}
- name: kaniko-build
  taskRef:
    kind: Task
    name: kaniko
  runAfter:
    - push
  params:
    - name: codebase-name
      value: "$(params.CODEBASE_NAME)"
    - name: image-tag
      value: "$(tasks.get-version.results.IS_TAG)"
    - name: image-tar
      value: "$(params.CODEBASE_NAME)_$(tasks.get-version.results.IS_TAG)"
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source
{{- end -}}

{{- define "save-cache-pyhton" -}}
- name: save-cache
  taskRef:
    kind: Task
    name: save-cache
  runAfter:
    - push
  params:
    - name: CACHE_NAME
      value: $(params.CODEBASE_NAME)
  workspaces:
    - name: cache
      workspace: shared-workspace
      subPath: cache
{{- end -}}

{{- define "ansible-check-review-common" -}}

- name: ansible-lint
  taskRef:
    kind: Task
    name: ansible
  runAfter:
    - init-values
  params:
    - name: EXTRA_COMMANDS
      value: |
        ansible-lint
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source

- name: ansible-tests
  taskRef:
    kind: Task
    name: ansible
  runAfter:
    -  ansible-lint
  params:
    - name: EXTRA_COMMANDS
      value: |
        ansible-playbook tests/*.*
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source

{{- end -}}

{{- define "ansible-check-build-common" -}}

- name: ansible-lint
  taskRef:
    kind: Task
    name: ansible
  runAfter:
    - init-values
  params:
    - name: EXTRA_COMMANDS
      value: |
        ansible-lint
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source

- name: ansible-tests
  taskRef:
    kind: Task
    name: ansible
  runAfter:
    -  ansible-lint
  params:
    - name: EXTRA_COMMANDS
      value: |
        ansible-playbook tests/*.*
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source

- name: git-tag
  taskRef:
    kind: Task
    name: git-cli
  runAfter:
    - ansible-tests
  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

{{- end -}}
