# 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-retest-comment
  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: trigger_target
      value: "retest-comment"
    - name: action
      value: $(body.action)
    - 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: sender
      value: $(body.sender.login)
    - name: repository
      value: $(body.repository.name)
    - name: pull_request_url
      value: $(body.issue.pull_request.html_url)
    - name: installation_id
      value: $(body.installation.id)
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
  name: pipelines-as-code-github-retest-comment
  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: event_type
    - name: owner
    - name: sender
    - name: repository
    - name: pull_request_url
    - 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:
        timeout: 24h0m0s
        serviceAccountName: pipelines-as-code-sa-el
        params:
          - name: ghe_host
            value: $(tt.params.ghe_host)
          - name: action
            value: $(tt.params.action)
          - name: event_type
            value: $(tt.params.event_type)
          - name: trigger_target
            value: $(tt.params.trigger_target)
          - name: owner
            value: $(tt.params.owner)
          - name: sender
            value: $(tt.params.sender)
          - name: repository
            value: $(tt.params.repository)
          - name: pull_request_url
            value: $(tt.params.pull_request_url)
          - name: installation_id
            value: $(tt.params.installation_id)
        taskSpec:
          params:
            - name: action
              type: string
            - name: ghe_host
              type: string
            - name: event_type
              type: string
            - name: owner
              type: string
            - name: sender
              type: string
            - name: repository
              type: string
            - name: pull_request_url
              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_INSTALLATION_ID
                  value: $(params.installation_id)
                - name: PAC_WORKSPACE_SECRET
                  value: $(workspaces.secrets.path)
                - name: PAC_TRIGGER_TARGET
                  value: "$(params.trigger_target)"
                - name: PAC_WEBHOOK_TYPE
                  value: "$(params.event_type)"
                - name: PAC_PAYLOAD_FILE
                  value: "/tmp/payload.json"
                - name: PAC_NAMESPACE
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.namespace
              script: |
                #!/usr/bin/env bash
                set -euf

                cat << EOF|tee ${PAC_PAYLOAD_FILE}
                {
                  "action": "$(params.action)",
                  "issue": {
                    "pull_request": {
                      "html_url": "$(params.pull_request_url)"
                    }
                  },
                  "sender": {
                     "login": "$(params.sender)"
                   },
                  "repository": {
                      "name": "$(params.repository)",
                      "owner": {
                        "login": "$(params.owner)"
                      }
                  }
                }
                EOF
                env|grep '^PAC'
                pipelines-as-code
          workspaces:
            - name: secrets
        workspaces:
          - name: secrets
            secret:
              secretName: pipelines-as-code-secret
