---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: pipeline-as-code-on-push
  annotations:
    pipelinesascode.tekton.dev/on-event: "[push]"
    pipelinesascode.tekton.dev/on-target-branch: "[main]"
    pipelinesascode.tekton.dev/task: "[git-clone]"
    pipelinesascode.tekton.dev/task-1: "[https://git.io/Jn9Ee]"  # send-slack-notifications task
spec:
  params:
    - name: repo_url
      value: "{{repo_url}}"
    - name: revision
      value: "{{revision}}"
  pipelineSpec:
    params:
      - name: repo_url
      - name: revision
    tasks:
      - name: fetch
        params:
          - name: url
            value: $(params.repo_url)
          - name: revision
            value: $(params.revision)
        taskRef:
          name: git-clone
        workspaces:
          - name: output
            workspace: source

      - name: unit
        runAfter:
          - fetch
        workspaces:
          - name: source
            workspace: source
        taskSpec:
          workspaces:
            - name: source
          steps:
            - image: registry.access.redhat.com/ubi8/go-toolset@sha256:75144067a7135ac3a43d37cd74a8b46507c897f285680ed85cf1f2031cc0fbf3
              name: get-cache
              workingDir: $(workspaces.source.path)
              env:
                - name: GOCACHE
                  value: $(workspaces.source.path)/go-build-cache/cache
                - name: GOMODCACHE
                  value: $(workspaces.source.path)/go-build-cache/mod
                - name: GOLANGCILINT_CACHE
                  value: $(workspaces.source.path)/go-build-cache/golangci-cache
              script: |
                #!/usr/bin/env bash
                set -ex
                mkdir -p ${GOCACHE} ${GOMODCACHE} ${GOLANGCILINT_CACHE}
                cd $(dirname ${GOCACHE})

                curl -fsI http://uploader:8080/golang-cache.tar || {
                    echo "no cache found"
                    exit 0
                }

                echo "Getting cache"
                curl http://uploader:8080/golang-cache.tar|tar -x -f-
            - name: unittest
              image: registry.access.redhat.com/ubi8/go-toolset@sha256:75144067a7135ac3a43d37cd74a8b46507c897f285680ed85cf1f2031cc0fbf3
              workingDir: $(workspaces.source.path)
              env:
                - name: GOCACHE
                  value: $(workspaces.source.path)/go-build-cache/cache
                - name: GOMODCACHE
                  value: $(workspaces.source.path)/go-build-cache/mod
              script: |
                #!/usr/bin/env bash
                make test \
                    GO_TEST_FLAGS="-v -race -coverprofile=coverage.txt -covermode=atomic"
      - name: codecov
        runAfter:
          - unit
        workspaces:
          - name: source
            workspace: source
        taskSpec:
          workspaces:
            - name: source
          steps:
            - name: codecov-run
              # Has everything we need in there and we already fetched it!
              image: registry.access.redhat.com/ubi8/go-toolset@sha256:75144067a7135ac3a43d37cd74a8b46507c897f285680ed85cf1f2031cc0fbf3
              workingDir: $(workspaces.source.path)
              env:
                - name: CODECOV_TOKEN
                  valueFrom:
                    secretKeyRef:
                      name: pipelines-as-code-codecov-token
                      key: token
              script: |
                #!/usr/bin/env bash
                bash <(curl https://codecov.io/bash)

      - name: upload-release-yaml
        runAfter:
          - fetch
        taskSpec:
          steps:
            - image: registry.access.redhat.com/ubi8/python-39@sha256:86466a8a2b3981c339f84e791e170055eb3f520e8419f955efa98e93ccaaf5b9
              name: release
              env:
                - name: HUB_TOKEN
                  valueFrom:
                    secretKeyRef:
                      name: "nightly-ci-github-hub-token"
                      key: "hub-token"
              script: |
                #!/usr/bin/env bash
                set -eu
                set +x # Do not show TOKEN in logs
                hack/upload-file-to-github.py \
                --message "Release yaml generated from {{repo_url}}/commit/{{revision}}" \
                --branch-ref refs/heads/nightly \
                --owner-repository openshift-pipelines/pipelines-as-code \
                --token ${HUB_TOKEN} \
                -f <(./hack/generate-releaseyaml.sh):release.k8s.yaml  \
                -f <(env TARGET_OPENSHIFT=true ./hack/generate-releaseyaml.sh):release.yaml
              workingDir: $(workspaces.source.path)
          workspaces:
            - name: source
        workspaces:
          - name: source
            workspace: source
    workspaces:
      - name: source
    finally:
      - name: finally
        taskRef:
          name: send-slack-notification
        params:
          - name: log_url
            value: "https://console-openshift-console.apps.paac.devcluster.openshift.com"
          - name: openshift
            value: "true"
  workspaces:
    - name: source
      volumeClaimTemplate:
        metadata:
          creationTimestamp: null
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 2Gi
