{{- define "js-npm-build-common" -}}
- name: build
  taskRef:
    kind: Task
    name: edp-npm
  runAfter:
    - update-build-number
    - get-cache
  params:
    - name: BASE_IMAGE
      value: $(params.image)
  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: npm
  runAfter:
    - sonar
  params:
    - name: BASE_IMAGE
      value: $(params.image)
    - name: EXTRA_COMMANDS
      value: |
        export upBase64=$(echo -n ${CI_USERNAME}:${CI_PASSWORD} | base64)
        # Get Nexus repository name
        versionLowerCase=$(echo $(tasks.get-version.results.VERSION) | tr '[:upper:]' '[:lower:]')

        export NEXUS_HOST="//${NEXUS_HOST_URL#*://}"

        if [[ "$versionLowerCase" == *"snapshot"* ]]; then
            export npm_config_userconfig=/var/configmap/.npmrc-publish-snapshots
        else
            export npm_config_userconfig=/var/configmap/.npmrc-publish-releases
        fi
        npm publish
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source
{{- end -}}

{{- define "js-npm-review-common" -}}
- name: build
  taskRef:
    kind: Task
    name: edp-npm
  runAfter:
    - get-cache
  params:
    - name: BASE_IMAGE
      value: $(params.image)
  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 "build-pipeline-js-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 "dockerbuild-verify-js" -}}
- name: dockerbuild-verify
  taskRef:
    kind: Task
    name: dockerbuild-verify
  runAfter:
    - sonar
    - dockerfile-lint
  workspaces:
    - name: source
      workspace: shared-workspace
      subPath: source
{{- end -}}

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

{{- define "build-pipeline-antora-image-build-push-common" -}}
- name: kaniko-build
  taskRef:
    kind: Task
    name: kaniko
  runAfter:
    - build
  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 -}}
