# The init section for bitbucket code-review pipeline
{{- define "bitbucket-review-start" -}}
- name: bitbucket-set-pending-status
  taskRef:
    kind: Task
    name: bitbucket-set-status
  params:
    - name: REPO_FULL_NAME
      value: $(params.gitfullrepositoryname)
    - name: SHA
      value: "$(params.git-source-revision)"
    - name: TARGET_URL
      value: $(params.pipelineUrl)
    - name: DESCRIPTION
      value: "Review Pipeline"
    - name: STATE
      value: "INPROGRESS"
    - name: BITBUCKET_TOKEN_SECRET_NAME
      value: ci-bitbucket
    - name: BITBUCKET_TOKEN_SECRET_KEY
      value: token
    - name: KEY
      value: "review"
    - name: NAME
      value: "Pipeline"

- name: fetch-repository
  taskRef:
    kind: Task
    name: git-clone
  runAfter:
    - bitbucket-set-pending-status
  params:
    - name: url
      value: "$(params.git-source-url)"
    - name: revision
      value: $(params.git-source-revision)
    - name: refspec
      value: $(params.git-refspec)
    - name: subdirectory
      value: source
  workspaces:
    - name: output
      workspace: shared-workspace
    - name: ssh-directory
      workspace: ssh-creds

- name: init-values
  taskRef:
    kind: Task
    name: init-values
  runAfter:
    - fetch-repository
  params:
    - name: CODEBASE_NAME
      value: $(params.CODEBASE_NAME)
    - name: BRANCH_NAME
      value: $(params.git-source-revision)
{{- end -}}


# final section for bitbucket build pipelines
{{- define "bitbucket-build-vote" -}}
- name: bitbucket-set-success-status
  when:
    - input: "$(tasks.status)"
      operator: in
      values: ["Succeeded"]
    - input: $(params.COMMIT_MESSAGE)
      operator: notin
      values: [""]
  taskRef:
    kind: Task
    name: bitbucket-set-status
  params:
    - name: REPO_FULL_NAME
      value: $(params.gitfullrepositoryname)
    - name: SHA
      value: "$(params.git-source-revision)"
    - name: TARGET_URL
      value: $(params.pipelineUrl)
    - name: DESCRIPTION
      value: "Build Pipeline"
    - name: STATE
      value: "SUCCESSFUL"
    - name: BITBUCKET_TOKEN_SECRET_NAME
      value: ci-bitbucket
    - name: BITBUCKET_TOKEN_SECRET_KEY
      value: token
    - name: KEY
      value: "build"
    - name: NAME
      value: "Pipeline (PASSED)"

- name: bitbucket-set-failure-status
  when:
    - input: "$(tasks.status)"
      operator: in
      values: ["Failed"]
    - input: $(params.COMMIT_MESSAGE)
      operator: notin
      values: [""]
  taskRef:
    kind: Task
    name: bitbucket-set-status
  params:
  - name: REPO_FULL_NAME
    value: $(params.gitfullrepositoryname)
  - name: SHA
    value: "$(params.git-source-revision)"
  - name: TARGET_URL
    value: $(params.pipelineUrl)
  - name: DESCRIPTION
    value: "Build Pipeline"
  - name: STATE
    value: "FAILED"
  - name: BITBUCKET_TOKEN_SECRET_NAME
    value: ci-bitbucket
  - name: BITBUCKET_TOKEN_SECRET_KEY
    value: token
  - name: KEY
    value: "build"
  - name: NAME
    value: "Pipeline (FAILED)"
{{- end -}}


# final section for bitbucket codereview pipelines
{{- define "bitbucket-review-vote" -}}
finally:
  - name: bitbucket-set-success-status
    when:
      - input: "$(tasks.status)"
        operator: in
        values: ["Succeeded"]
    taskRef:
      kind: Task
      name: bitbucket-set-status
    params:
      - name: REPO_FULL_NAME
        value: $(params.gitfullrepositoryname)
      - name: SHA
        value: "$(params.git-source-revision)"
      - name: TARGET_URL
        value: $(params.pipelineUrl)
      - name: DESCRIPTION
        value: "Review Pipeline"
      - name: STATE
        value: "SUCCESSFUL"
      - name: BITBUCKET_TOKEN_SECRET_NAME
        value: ci-bitbucket
      - name: BITBUCKET_TOKEN_SECRET_KEY
        value: token
      - name: KEY
        value: "review"
      - name: NAME
        value: "Pipeline (PASSED)"

  - name: bitbucket-set-failure-status
    when:
      - input: "$(tasks.status)"
        operator: in
        values: ["Failed"]
    taskRef:
      kind: Task
      name: bitbucket-set-status
    params:
      - name: REPO_FULL_NAME
        value: $(params.gitfullrepositoryname)
      - name: SHA
        value: "$(params.git-source-revision)"
      - name: TARGET_URL
        value: $(params.pipelineUrl)
      - name: DESCRIPTION
        value: "Review Pipeline"
      - name: STATE
        value: "FAILED"
      - name: BITBUCKET_TOKEN_SECRET_NAME
        value: ci-bitbucket
      - name: BITBUCKET_TOKEN_SECRET_KEY
        value: token
      - name: KEY
        value: "review"
      - name: NAME
        value: "Pipeline (FAILED)"
{{- end -}}

# The init section for bitbucket build pipeline
{{- define "bitbucket-build-start" -}}
- name: bitbucket-set-pending-status
  when:
    - input: $(params.COMMIT_MESSAGE)
      operator: notin
      values: [""]
  taskRef:
    kind: Task
    name: bitbucket-set-status
  params:
    - name: REPO_FULL_NAME
      value: $(params.gitfullrepositoryname)
    - name: SHA
      value: "$(params.git-source-revision)"
    - name: TARGET_URL
      value: $(params.pipelineUrl)
    - name: DESCRIPTION
      value: "Build Pipeline"
    - name: STATE
      value: "INPROGRESS"
    - name: BITBUCKET_TOKEN_SECRET_NAME
      value: ci-bitbucket
    - name: BITBUCKET_TOKEN_SECRET_KEY
      value: token
    - name: KEY
      value: "build"
    - name: NAME
      value: "Pipeline"

- name: fetch-repository
  taskRef:
    kind: Task
    name: git-clone
  params:
    - name: url
      value: $(params.git-source-url)
    - name: revision
      value: $(params.git-source-revision)
    - name: subdirectory
      value: source
  workspaces:
    - name: output
      workspace: shared-workspace
    - name: ssh-directory
      workspace: ssh-creds

- name: init-values
  taskRef:
    kind: Task
    name: init-values
  runAfter:
    - fetch-repository
  params:
    - name: CODEBASE_NAME
      value: $(params.CODEBASE_NAME)
    - name: BRANCH_NAME
      value: $(params.git-source-revision)
{{- end -}}
