# 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: EventListener
metadata:
  name: pipelines-as-code-interceptor
  namespace: pipelines-as-code
  labels:
    app.kubernetes.io/instance: default
    app.kubernetes.io/version: "devel"
    app.kubernetes.io/part-of: pipelines-as-code
spec:
  serviceAccountName: pipelines-as-code-sa-el
  triggers:

    # When you have a /retest in a comment to retest a PR
    - name: github-issue-comment-retest
      interceptors:
        - ref:
            name: "github"
          params:
            - name: "secretRef"
              value:
                secretName: "pipelines-as-code-secret"
                secretKey: "webhook.secret"
            - name: "eventTypes"
              value: ["issue_comment"]
        - name: "Handle /retest comment in issue"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                  body.action == 'created' &&
                  'pull_request' in body.issue &&
                  body.issue.state == 'open' &&
                  'installation' in body &&
                  body.comment.body.matches('(^|\\r\\n)/retest([ ]*$|$|\\r\\n)')
      bindings:
        - ref: pipelines-as-code-github-retest-comment
      template:
        ref: pipelines-as-code-github-retest-comment

    # When you have a /ok-to-test in a comment to allow CI on a non owner sender
    - name: github-issue-comment-ok-to-test
      interceptors:
        - ref:
            name: "github"
          params:
            - name: "secretRef"
              value:
                secretName: "pipelines-as-code-secret"
                secretKey: "webhook.secret"
            - name: "eventTypes"
              value: ["issue_comment"]
        - name: "Handle /ok-to-test comment in issue"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                  body.action == 'created' &&
                  'pull_request' in body.issue &&
                  body.issue.state == 'open' &&
                  'installation' in body &&
                  body.comment.body.matches('(^|\\r\\n)/ok-to-test([ ]*$|$|\\r\\n)')
      bindings:
        - ref: pipelines-as-code-github-ok-to-test-comment
      template:
        # Using the templateRef from retest since they are mostly the same
        ref: pipelines-as-code-github-retest-comment

    # Branch push using different binding but same triggertemplate as pullreq
    - name: github-branch-push
      interceptors:
        - ref:
            name: "github"
          params:
            - name: "secretRef"
              value:
                secretName: "pipelines-as-code-secret"
                secretKey: "webhook.secret"
            - name: "eventTypes"
              value: ["push"]
        - name: "Branch Push"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                'pusher' in body
      bindings:
        - ref: pipelines-as-code-github-push
      template:
        ref: pipelines-as-code-github-push

    # When using the UI and clicking on Re-run failed test
    - name: github-check-run-recheck
      interceptors:
        - ref:
            name: "github"
          params:
            - name: "secretRef"
              value:
                secretName: "pipelines-as-code-secret"
                secretKey: "webhook.secret"
            - name: "eventTypes"
              value: ["check_run"]
        - name: "UI rerun failed CI click"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                body.action in ['rerequested'] &&
                'check_run' in body
      bindings:
        - ref: pipelines-as-code-github-recheck
      template:
        ref: pipelines-as-code-github-recheck

    # GitHub When sending a new Pull Request
    - name: github-pull-request
      interceptors:
        - ref:
            name: "github"
          params:
            - name: "secretRef"
              value:
                secretName: "pipelines-as-code-secret"
                secretKey: "webhook.secret"
            - name: "eventTypes"
              value: ["pull_request"]
        - name: "Pull request - created/updated"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                body.action in ['created', 'synchronize', 'opened']
      bindings:
        - ref: pipelines-as-code-github-pullreq
      template:
        ref: pipelines-as-code-github-pullreq

    # Bitbucket Cloud event when sending a new Pull Request
    - name: bitbucket-cloud-pull-request
      bindings:
        - ref: pipelines-as-code-bitbucket-cloud-pullreq
        - ref: pipelines-as-code-bitbucket-cloud-pullreq-common
      interceptors:
        - ref:
            name: "bitbucket"
          params:
            - name: eventTypes
              value:
                - pullrequest:created
                - pullrequest:updated
      template:
        ref: pipelines-as-code-bitbucket-cloud-pullreq

    # When you have a /ok-to-test in a comment to allow CI on a non owner sender
    - name: bitbucket-cloud-issue-comment-ok-to-test
      interceptors:
        - ref:
            name: "bitbucket"
          params:
            - name: eventTypes
              value:
                - pullrequest:comment_created
        - name: "Handle /ok-to-test comment in issue"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                  body.comment.content.raw.matches('(^|\\r\\n)/ok-to-test([ ]*$|$|\\r\\n)')
      bindings:
        - ref: pipelines-as-code-bitbucket-cloud-ok-to-test-comment
        - ref: pipelines-as-code-bitbucket-cloud-pullreq-common
      template:
        # Using the templateRef from retest since they are mostly the same
        ref: pipelines-as-code-bitbucket-cloud-pullreq

    - name: bitbucket-cloud-issue-retest
      interceptors:
        - ref:
            name: "bitbucket"
          params:
            - name: eventTypes
              value:
                - pullrequest:comment_created
        - name: "Handle /retest comment in issue"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                  body.comment.content.raw.matches('(^|\\r\\n)/retest([ ]*$|$|\\r\\n)')
      bindings:
        - ref: pipelines-as-code-bitbucket-cloud-retest-comment
        - ref: pipelines-as-code-bitbucket-cloud-pullreq-common
      template:
        # Using the templateRef from retest since they are mostly the same
        ref: pipelines-as-code-bitbucket-cloud-pullreq

    # Bitbucket Cloud event when sending a new Pull Request
    - name: bitbucket-cloud-push
      bindings:
        - ref: pipelines-as-code-bitbucket-cloud-push
      interceptors:
        - ref:
            name: "bitbucket"
          params:
            - name: eventTypes
              value:
                - repo:push
      template:
        ref: pipelines-as-code-bitbucket-cloud-push

    # Bitbucket Server event when sending a new Pull Request
    - name: bitbucket-server-pull-request
      bindings:
        - ref: pipelines-as-code-bitbucket-server-pullreq-common
        - ref: pipelines-as-code-bitbucket-server-pullreq
      interceptors:
        - ref:
            name: "bitbucket"
          params:
            - name: secretRef
              value:
                secretName: "pipelines-as-code-secret"
                secretKey: "webhook.secret"
            - name: eventTypes
              value:
                - pr:from_ref_updated
                - pr:opened
      template:
        ref: pipelines-as-code-bitbucket-server-pullreq

    - name: bitbucket-server-push
      interceptors:
        - ref:
            name: "bitbucket"
          params:
            - name: "secretRef"
              value:
                secretName: "pipelines-as-code-secret"
                secretKey: "webhook.secret"
            - name: "eventTypes"
              value: ["repo:refs_changed"]
        - name: "Branch Push"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                'changes' in body
      bindings:
        - ref: pipelines-as-code-bitbucket-server-push
      template:
        ref: pipelines-as-code-bitbucket-server-push

    # When you have a /ok-to-test in a comment to allow CI on a non owner sender
    - name: bitbucket-server-issue-comment-ok-to-test
      interceptors:
        - ref:
            name: bitbucket
          params:
            - name: secretRef
              value:
                secretName: "pipelines-as-code-secret"
                secretKey: "webhook.secret"
            - name: eventTypes
              value:
                - pr:comment:added
                - pr:comment:edited
        - name: "Handle /ok-to-test comment in issue"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                  body.comment.text.matches('(^|\\r\\n)/ok-to-test([ ]*$|$|\\r\\n)')
      bindings:
        - ref: pipelines-as-code-bitbucket-server-ok-to-test-comment
        - ref: pipelines-as-code-bitbucket-server-pullreq-common
      template:
        # Using the templateRef from retest since they are mostly the same
        ref: pipelines-as-code-bitbucket-server-pullreq

    # When you have a /retest in a comment to let CI "rekick"
    - name: bitbucket-server-issue-comment-retest
      interceptors:
        - ref:
            name: bitbucket
          params:
            - name: "secretRef"
              value:
                secretName: "pipelines-as-code-secret"
                secretKey: "webhook.secret"
            - name: eventTypes
              value:
                - pr:comment:added
        - name: "Handle /retest comment in issue"
          ref:
            name: "cel"
          params:
            - name: "filter"
              value: >-
                  body.comment.text.matches('(^|\\r\\n)/retest([ ]*$|$|\\r\\n)')
      bindings:
        - ref: pipelines-as-code-bitbucket-server-retest-comment
        - ref: pipelines-as-code-bitbucket-server-pullreq-common
      template:
        # Using the templateRef from retest since they are mostly the same
        ref: pipelines-as-code-bitbucket-server-pullreq
