# The init section for github build pipeline
{{- define "github-build-start" -}}
- name: github-set-pending-status
  taskRef:
    kind: Task
    name: github-set-status
  when:
    - input: $(params.gitsha)
      operator: notin
      values: ["$(tt.params.gitsha)"]
  params:
    - name: REPO_FULL_NAME
      value: $(params.gitfullrepositoryname)
    - name: DESCRIPTION
      value: "Pipeline (IN PROGRESS)"
    - name: STATE
      value: "pending"
    - name: CONTEXT
      value: "Build Pipeline"
    - name: AUTH_TYPE
      value: Token
    - name: GITHUB_TOKEN_SECRET_NAME
      value: ci-github
    - name: GITHUB_TOKEN_SECRET_KEY
      value: token
    - name: SHA
      value: $(params.gitsha)
    - name: TARGET_URL
      value: $(params.pipelineUrl)

- 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 -}}

# The init section for github code-review pipeline
{{- define "github-review-start" -}}
- name: github-set-pending-status
  taskRef:
    kind: Task
    name: github-set-status
  params:
    - name: REPO_FULL_NAME
      value: $(params.gitfullrepositoryname)
    - name: DESCRIPTION
      value: "Pipeline (IN PROGRESS)"
    - name: STATE
      value: "pending"
    - name: CONTEXT
      value: "Review Pipeline"
    - name: AUTH_TYPE
      value: Token
    - name: GITHUB_TOKEN_SECRET_NAME
      value: ci-github
    - name: GITHUB_TOKEN_SECRET_KEY
      value: token
    - name: SHA
      value: $(params.gitsha)
    - name: TARGET_URL
      value: $(params.pipelineUrl)

- name: fetch-repository
  taskRef:
    kind: Task
    name: git-clone
  runAfter:
    - github-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 github codereview pipelines
{{- define "github-review-vote" -}}
finally:
  - name: github-set-success-status
    when:
      - input: "$(tasks.status)"
        operator: in
        values: ["Succeeded"]
    taskRef:
      kind: Task
      name: github-set-status
    params:
      - name: REPO_FULL_NAME
        value: $(params.gitfullrepositoryname)
      - name: DESCRIPTION
        value: "Pipeline (PASSED)"
      - name: STATE
        value: "success"
      - name: CONTEXT
        value: "Review Pipeline"
      - name: AUTH_TYPE
        value: Token
      - name: GITHUB_TOKEN_SECRET_NAME
        value: ci-github
      - name: GITHUB_TOKEN_SECRET_KEY
        value: token
      - name: SHA
        value: $(params.gitsha)
      - name: TARGET_URL
        value: $(params.pipelineUrl)

  - name: github-set-failure-status
    when:
      - input: "$(tasks.status)"
        operator: in
        values: ["Failed"]
    taskRef:
      kind: Task
      name: github-set-status
    params:
      - name: REPO_FULL_NAME
        value: $(params.gitfullrepositoryname)
      - name: DESCRIPTION
        value: "Pipeline (FAILED)"
      - name: STATE
        value: "failure"
      - name: CONTEXT
        value: "Review Pipeline"
      - name: AUTH_TYPE
        value: Token
      - name: GITHUB_TOKEN_SECRET_NAME
        value: ci-github
      - name: GITHUB_TOKEN_SECRET_KEY
        value: token
      - name: SHA
        value: $(params.gitsha)
      - name: TARGET_URL
        value: $(params.pipelineUrl)
{{- end -}}

{{- define "github-build-vote" -}}
- name: github-set-success-status
  when:
    - input: "$(tasks.status)"
      operator: in
      values: ["Succeeded"]
    - input: $(params.gitsha)
      operator: notin
      values: ["$(tt.params.gitsha)"]
  taskRef:
    kind: Task
    name: github-set-status
  params:
    - name: REPO_FULL_NAME
      value: $(params.gitfullrepositoryname)
    - name: DESCRIPTION
      value: "Pipeline (PASSED)"
    - name: STATE
      value: "success"
    - name: CONTEXT
      value: "Build Pipeline"
    - name: AUTH_TYPE
      value: Token
    - name: GITHUB_TOKEN_SECRET_NAME
      value: ci-github
    - name: GITHUB_TOKEN_SECRET_KEY
      value: token
    - name: SHA
      value: $(params.gitsha)
    - name: TARGET_URL
      value: $(params.pipelineUrl)

- name: github-set-failure-status
  when:
    - input: "$(tasks.status)"
      operator: in
      values: ["Failed"]
    - input: $(params.gitsha)
      operator: notin
      values: ["$(tt.params.gitsha)"]
  taskRef:
    kind: Task
    name: github-set-status
  params:
    - name: REPO_FULL_NAME
      value: $(params.gitfullrepositoryname)
    - name: DESCRIPTION
      value: "Pipeline (FAILED)"
    - name: STATE
      value: "failure"
    - name: CONTEXT
      value: "Build Pipeline"
    - name: AUTH_TYPE
      value: Token
    - name: GITHUB_TOKEN_SECRET_NAME
      value: ci-github
    - name: GITHUB_TOKEN_SECRET_KEY
      value: token
    - name: SHA
      value: $(params.gitsha)
    - name: TARGET_URL
      value: $(params.pipelineUrl)
{{- end -}}
