---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: pipelines-operator-nightly
spec:
  pipelineSpec:
    workspaces:
      - name: source
    tasks:
      - name: fetch-repository
        taskRef:
          name: git-clone
        workspaces:
          - name: output
            workspace: source
        params:
          - name: url
            value: https://github.com/openshift/tektoncd-operator
          - name: revision
            value: master
          - name: subdirectory
            value: ""
          - name: deleteExisting
            value: "true"
      - name: create-pr
        runAfter:
          - fetch-repository
        workspaces:
          - workspace: source
            name: source
        taskSpec:
          workspaces:
          - name: source
          steps:
            - name: create-pr
              workingDir: $(workspaces.source.path)
              env:
                - name: HUB_VERSION
                  value: "true"
                - name: GITHUB_TOKEN
                  valueFrom:
                    secretKeyRef:
                      name: nightly-ci-github-hub-token
                      key: hub-token
              image: gcr.io/tekton-releases/dogfooding/hub:latest
              script: |
                #!/usr/bin/env bash
                set -xe

                # TODO: oct 04, 2021 | NT: separate make steps and hub steps into separate tasks
                apk add make || true # add this true or remove -x from this script
                apk add curl || true # add this true or remove -x from this script
                apk add --no-cache python3 py3-pip || true
                pip3 install pyyaml

                # Configure git email and name
                git config user.email "pipelines-dev@redhat.com"
                git config user.name "OpenShift Pipelines"

                ## Make sure we can push to the branch with our GITHUB_TOKEN (disable logging to not leak)
                set +x
                git remote add upstream $(echo "https://github.com/tektoncd/operator.git"|sed "s,https://github.com/,https://${GITHUB_TOKEN}@github.com/,")
                git remote add openshift $(echo "https://github.com/openshift/tektoncd-operator.git"|sed "s,https://github.com/,https://${GITHUB_TOKEN}@github.com/,")
                set -x
                # Launch script
                [ -d .bin ] || mkdir .bin
                openshift/release/update-to-head.sh
    finally:
      - name: finally
        taskSpec:
          steps:
            - name: send-to-slack
              env:
                - name: SLACK_WEBHOOK_URL
                  valueFrom:
                    secretKeyRef:
                      name: slack-tektoncd-operator-ci-webhook
                      key: hook_url
                - name: PIPELINERUN
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.labels['tekton.dev/pipelineRun']
                - name: LABEL_TO_CHECK
                  value: "nightly-ci"
                - name: SUCCESS_URL_ICON
                  value: "https://github.com/tektoncd.png"
                - name: FAILURE_URL_ICON
                  value: "https://user-images.githubusercontent.com/4288561/114842214-eecf6c80-9dd8-11eb-8924-86288b1a501c.jpeg"
                - name: SUCCESS_SUBJECT
                  value: "Tektoncd Operator CI nightly sync with upstream ran successfully  :pipelines: :dance_cat: :yay2:"
                - name: FAILURE_SUBJECT
                  value: "Tektoncd Operator CI nightly sync with upstream has failed :pipeline: :sadparrot: :failed:"
                - name: LOG_URL
                  value: "https://console-openshift-console.apps.cicd.tekton.codereadyqe.com/k8s/ns/nightly-ci/tekton.dev~v1beta1~PipelineRun/pipelines-operator-nightly"

              image: quay.io/chmouel/tekton-asa-code:latest
              command: [ "/code/misc/send-slack-notifications.py" ]
  workspaces:
    - name: source
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 500Mi
