# 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-pullreq
  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: "pull-request"
    - 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: "default_branch"
      value: $(body.repository.default_branch)
    - name: "url"
      value: $(body.repository.html_url)
    - name: "sender"
      value: $(body.pull_request.user.login)
    - name: "base_ref"
      value: $(body.pull_request.base.ref)
    - name: "pull_request_number"
      value: $(body.pull_request.number)
    - name: "pull_request_html_url"
      value: $(body.pull_request.html_url)
    - name: "sha"
      value: $(body.pull_request.head.sha)
    - name: "head_ref"
      value: $(body.pull_request.head.ref)
    - name: "installation_id"
      value: $(body.installation.id)
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
  name: pipelines-as-code-github-pullreq
  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: event_type
    - name: owner
    - name: repository
    - name: default_branch
    - name: url
    - name: sender
    - name: base_ref
    - name: pull_request_number
    - name: pull_request_html_url
    - name: sha
    - name: head_ref
    - 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: event_type
            value: $(tt.params.event_type)
          - name: trigger_target
            value: $(tt.params.trigger_target)
          - 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: sender
            value: $(tt.params.sender)
          - name: base_ref
            value: $(tt.params.base_ref)
          - name: pull_request_number
            value: $(tt.params.pull_request_number)
          - name: pull_request_html_url
            value: $(tt.params.pull_request_html_url)
          - name: sha
            value: $(tt.params.sha)
          - name: head_ref
            value: $(tt.params.head_ref)
          - name: installation_id
            value: $(tt.params.installation_id)
        taskSpec:
          params:
            - name: ghe_host
              type: string
            - name: trigger_target
              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: sender
              type: string
            - name: base_ref
              type: string
            - name: pull_request_number
              type: string
            - name: pull_request_html_url
              type: string
            - name: sha
              type: string
            - name: head_ref
              type: string
            - name: installation_id
              type: string
          steps:
            - name: apply-and-launch
              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_PAYLOAD_FILE
                  value: "/tmp/payload.json"
                - name: PAC_TRIGGER_TARGET
                  value: "$(params.trigger_target)"
                - name: PAC_WEBHOOK_TYPE
                  value: "$(params.event_type)"
                - name: PAC_NAMESPACE
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.namespace
              imagePullPolicy: Always
              image: "ko://github.com/openshift-pipelines/pipelines-as-code/cmd/pipelines-as-code"
              script: |
                #!/usr/bin/env bash
                set -euf

                cat << EOF|tee ${PAC_PAYLOAD_FILE}
                {
                    "repository": {
                        "owner": {
                            "login": "$(params.owner)"
                        },
                        "name": "$(params.repository)",
                        "default_branch": "$(params.default_branch)",
                        "html_url": "$(params.url)"
                    },
                    "pull_request": {
                        "number": $(params.pull_request_number),
                        "html_url": "$(params.pull_request_html_url)",
                        "user": {
                            "login": "$(params.sender)"
                        },
                        "base": {
                            "ref": "$(params.base_ref)"
                        },
                        "head": {
                            "sha": "$(params.sha)",
                            "ref": "$(params.head_ref)"
                        }
                    }
                }
                EOF
                env|grep '^PAC'
                pipelines-as-code
          workspaces:
            - name: secrets
        workspaces:
          - name: secrets
            secret:
              secretName: pipelines-as-code-secret
