# Copyright 2021 Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
  name: pipelines-as-code-github-recheck
  namespace: pipelines-as-code
  labels:
    app.kubernetes.io/version: "devel"
    app.kubernetes.io/instance: default
    app.kubernetes.io/part-of: pipelines-as-code
spec:
  params:
    - name: action
      value: $(body.action)
    - name: head_branch
      value: $(body.check_run.check_suite.head_branch)
    - name: head_sha
      value: $(body.check_run.check_suite.head_sha)
    - name: trigger_target
      value: "issue-recheck"
    - name: event_type
      value: $(header.X-GitHub-Event)
    - name: "ghe_host"
      value: $(header.X-GitHub-Enterprise-Host)
    - name: owner
      value: $(body.repository.owner.login)
    - name: repository
      value: $(body.repository.name)
    - name: url
      value: $(body.repository.html_url)
    - name: default_branch
      value: $(body.repository.default_branch)
    - name: pull_request_number
      value: $(body.check_run.check_suite.pull_requests[?(@.number)].number)
    - name: sender
      value: $(body.sender.login)
    - name: installation_id
      value: $(body.installation.id)

---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
  name: pipelines-as-code-github-recheck
  namespace: pipelines-as-code
  labels:
    app.kubernetes.io/version: "devel"
    app.kubernetes.io/instance: default
    app.kubernetes.io/part-of: pipelines-as-code
spec:
  params:
    - name: action
    - name: head_branch
    - name: head_sha
    - name: event_type
    - name: owner
    - name: repository
    - name: default_branch
    - name: url
    - name: pull_request_number
    - name: sender
    - name: installation_id
      default: ""
    - name: trigger_target
    - name: ghe_host
      default: "api.github.com"
  resourcetemplates:
    - apiVersion: tekton.dev/v1beta1
      kind: TaskRun
      metadata:
        generateName: pipelines-as-code-run-
        labels:
          app.kubernetes.io/version: "devel"
          app.kubernetes.io/managed-by: pipelines-as-code
          pipelinesascode.tekton.dev/event: $(tt.params.event_type)
      spec:
        serviceAccountName: pipelines-as-code-sa-el
        timeout: 24h0m0s
        params:
          - name: action
            value: $(tt.params.action)
          - name: event_type
            value: $(tt.params.event_type)
          - name: head_branch
            value: $(tt.params.head_branch)
          - name: head_sha
            value: $(tt.params.head_sha)
          - name: owner
            value: $(tt.params.owner)
          - name: repository
            value: $(tt.params.repository)
          - name: default_branch
            value: $(tt.params.default_branch)
          - name: url
            value: $(tt.params.url)
          - name: pull_request_number
            value: $(tt.params.pull_request_number)
          - name: sender
            value: $(tt.params.sender)
          - name: installation_id
            value: $(tt.params.installation_id)
          - name: trigger_target
            value: $(tt.params.trigger_target)
          - name: ghe_host
            value: $(tt.params.ghe_host)
        taskSpec:
          params:
            - name: ghe_host
              type: string
            - name: action
              type: string
            - name: head_branch
              type: string
            - name: head_sha
              type: string
            - name: event_type
              type: string
            - name: owner
              type: string
            - name: repository
              type: string
            - name: default_branch
              type: string
            - name: url
              type: string
            - name: pull_request_number
              type: string
              default: "000"
            - name: sender
              type: string
            - name: trigger_target
              type: string
            - name: installation_id
              type: string
          steps:
            - name: apply-and-launch
              imagePullPolicy: Always
              image: "ko://github.com/openshift-pipelines/pipelines-as-code/cmd/pipelines-as-code"
              env:
                - name: PAC_GIT_PROVIDER_APIURL
                  value: $(params.ghe_host)
                - name: PAC_GIT_PROVIDER_TYPE
                  value: "github"
                - name: PAC_TRIGGER_TARGET
                  value: $(params.trigger_target)
                - name: PAC_WEBHOOK_TYPE
                  value: $(params.event_type)
                - name: PAC_INSTALLATION_ID
                  value: $(params.installation_id)
                - name: PAC_WORKSPACE_SECRET
                  value: $(workspaces.secrets.path)
                - name: PAC_PAYLOAD_FILE
                  value: "/tmp/payload.json"
                - name: PAC_NAMESPACE
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.namespace
              script: |
                #!/usr/bin/env bash
                set -euf

                prnumber=$(params.pull_request_number)
                [[ ${prnumber} != "[]" ]] && prnumber="[{\"number\": ${prnumber}}]"
                cat << EOF |tee ${PAC_PAYLOAD_FILE}
                {
                  "action": "$(params.action)",
                  "check_run": {
                    "check_suite": {
                      "head_branch": "$(params.head_branch)",
                      "head_sha": "$(params.head_sha)",
                      "pull_requests": ${prnumber}
                    }
                  },
                  "repository": {
                    "default_branch": "$(params.default_branch)",
                    "html_url": "$(params.url)",
                    "name": "$(params.repository)",
                    "owner": {
                      "login": "$(params.owner)"
                    }
                  },
                  "sender": {
                    "login": "$(params.sender)"
                  }
                }
                EOF
                env|grep '^PAC'
                pipelines-as-code
          workspaces:
            - name: secrets
        workspaces:
          - name: secrets
            secret:
              secretName: pipelines-as-code-secret
