# 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-bitbucket-cloud-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
      value: pull_request

    - name: trigger_target
      value: pull_request

---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
  name: pipelines-as-code-bitbucket-cloud-pullreq-common
  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: "source_ip"
      value: $(header.X-Forwarded-For)
    - name: "workspace_slug"
      value: $(body.repository.workspace.slug)
    - name: "repository_name"
      value: $(body.repository.name)
    - name: "repository_html_link"
      value: $(body.repository.links.html.href)
    - name: "pullrequest_account_id"
      value: $(body.pullrequest.author.account_id)
    - name: "pullrequest_author_nickname"
      value: $(body.pullrequest.author.nickname)
    - name: "destination_branch_name"
      value: $(body.pullrequest.destination.branch.name)
    - name: "source_branch"
      value: $(body.pullrequest.source.branch.name)
    - name: "source_commit_hash"
      value: $(body.pullrequest.source.commit.hash)
    - name: "pullrequest_id"
      value: $(body.pullrequest.id)
---
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
  name: pipelines-as-code-bitbucket-cloud-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
    - name: event_type
    - name: source_ip
    - name: workspace_slug
    - name: repository_name
    - name: repository_html_link
    - name: pullrequest_account_id
    - name: pullrequest_author_nickname
    - name: destination_branch_name
    - name: source_branch
    - name: source_commit_hash
    - name: pullrequest_id
  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
          # TODO: maybe do a truncate here to at least know the pull_request event
          # pipelinesascode.tekton.dev/event: $(tt.params.event_type)
      spec:
        serviceAccountName: pipelines-as-code-sa-el
        timeout: 24h0m0s
        params:
          - name: source_ip
            value: $(tt.params.source_ip)
          - name: trigger_target
            value: $(tt.params.trigger_target)
          - name: event_type
            value: $(tt.params.event_type)
          - name: workspace_slug
            value: $(tt.params.workspace_slug)
          - name: repository_name
            value: $(tt.params.repository_name)
          - name: repository_html_link
            value: $(tt.params.repository_html_link)
          - name: pullrequest_account_id
            value: $(tt.params.pullrequest_account_id)
          - name: pullrequest_author_nickname
            value: $(tt.params.pullrequest_author_nickname)
          - name: destination_branch_name
            value: $(tt.params.destination_branch_name)
          - name: source_branch
            value: $(tt.params.source_branch)
          - name: source_commit_hash
            value: $(tt.params.source_commit_hash)
          - name: pullrequest_id
            value: $(tt.params.pullrequest_id)
        taskSpec:
          params:
            - name: trigger_target
              type: string
            - name: source_ip
              type: string
            - name: event_type
              type: string
            - name: workspace_slug
              type: string
            - name: repository_name
              type: string
            - name: repository_html_link
              type: string
            - name: pullrequest_account_id
              type: string
            - name: pullrequest_author_nickname
              type: string
            - name: destination_branch_name
              type: string
            - name: source_branch
              type: string
            - name: source_commit_hash
              type: string
            - name: pullrequest_id
              type: string
          steps:
            - name: apply-and-launch
              env:
                - name: PAC_BITBUCKET_CLOUD_CHECK_SOURCE_IP
                  valueFrom:
                    configMapKeyRef:
                      name: pipelines-as-code
                      key: bitbucket-cloud-check-source-ip
                - name: PAC_BITBUCKET_CLOUD_ADDITIONAL_SOURCE_IP
                  valueFrom:
                    configMapKeyRef:
                      name: pipelines-as-code
                      key: bitbucket-cloud-additional-source-ip
                - name: PAC_GIT_PROVIDER_TYPE
                  value: "bitbucket-cloud"
                - 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_SOURCE_IP
                  value: $(params.source_ip)
                - 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": {
                        "workspace": {
                            "slug": "$(params.workspace_slug)"
                        },
                        "name": "$(params.repository_name)",
                        "links": {
                            "html": {
                                "href": "$(params.repository_html_link)"
                            }
                        }
                    },
                    "pullrequest": {
                        "id":  $(params.pullrequest_id),
                        "author": {
                            "account_id": "$(params.pullrequest_account_id)",
                            "nickname": "$(params.pullrequest_author_nickname)"
                        },
                        "destination": {
                            "branch": {
                                "name": "$(params.destination_branch_name)"
                            }
                        },
                        "source": {
                            "branch": {
                                "name": "$(params.source_branch)"
                            },
                            "commit": {
                                "hash": "$(params.source_commit_hash)"
                            }
                        }
                    }
                }
                EOF
                env|grep '^PAC'
                pipelines-as-code
