---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: pipeline-as-code-on-pull-request
  annotations:
    pipelinesascode.tekton.dev/on-event: "[pull_request]"
    pipelinesascode.tekton.dev/on-target-branch: "[main]"
    pipelinesascode.tekton.dev/task: "[git-clone]"
    pipelinesascode.tekton.dev/max-keep-runs: "2"
spec:
  params:
    - name: repo_url
      value: "{{repo_url}}"
    - name: revision
      value: "{{revision}}"
  pipelineSpec:
    params:
      - name: repo_url
      - name: revision
    tasks:
      - name: fetchit
        params:
          - name: url
            value: $(params.repo_url)
          - name: revision
            value: $(params.revision)
        taskRef:
          name: git-clone
        workspaces:
          - name: output
            workspace: source

      - name: yamllint
        runAfter:
          - fetchit
        taskSpec:
          workspaces:
            - name: source
          steps:
            - name: yaml-lint
              image: quay.io/mhutter/yamllint
              workingDir: $(workspaces.source.path)
              script: |
                yamllint -c .yamllint $(find . -type f -regex ".*y[a]ml" -print)
        workspaces:
          - name: source
            workspace: source

      - name: unitlint
        runAfter:
          - fetchit
        taskSpec:
          workspaces:
            - name: source
          steps:
            - image: registry.access.redhat.com/ubi8/go-toolset@sha256:75144067a7135ac3a43d37cd74a8b46507c897f285680ed85cf1f2031cc0fbf3
              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
              name: get-cache
              workingDir: $(workspaces.source.path)
              script: |
                #!/usr/bin/env bash
                set -eux
                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
              # we get bumped out when usingh the official image with docker.io
              # ratelimit so workaround this.
              image: registry.access.redhat.com/ubi8/go-toolset@sha256:75144067a7135ac3a43d37cd74a8b46507c897f285680ed85cf1f2031cc0fbf3
              env:
                - name: GOCACHE
                  value: $(workspaces.source.path)/go-build-cache/cache
                - name: GOMODCACHE
                  value: $(workspaces.source.path)/go-build-cache/mod
              workingDir: $(workspaces.source.path)
              script: |
                #!/usr/bin/env bash
                set -eux
                make test GO_TEST_FLAGS="-v -race -coverprofile=coverage.txt -covermode=atomic"

            - name: lint
              image: quay.io/app-sre/golangci-lint:v1.44.0
              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 -eux
                make lint-go
        workspaces:
          - name: source
            workspace: source

      - name: savecache
        runAfter:
          - unitlint
        workspaces:
          - name: source
            workspace: source
        taskSpec:
          workspaces:
            - name: source
          steps:
            - name: save-cache
              # Has everything we need in there and we already fetched it!
              image: registry.access.redhat.com/ubi8/python-39@sha256:86466a8a2b3981c339f84e791e170055eb3f520e8419f955efa98e93ccaaf5b9
              workingDir: $(workspaces.source.path)
              script: |
                #!/usr/bin/env bash
                set -eux
                curl -o/dev/null -s -f -X POST -F path=test -F file=@/etc/motd  http://uploader:8080/upload || {
                    echo "No cache server found"
                    exit 0
                }

                lm="$(curl -fsI http://uploader:8080/golang-cache.tar|sed -n '/Last-Modified/ { s/Last-Modified: //;s/\r//; p}')"
                if [[ -n ${lm} ]];then
                    expired=$(python -c "import datetime, sys;print(datetime.datetime.now() > datetime.datetime.strptime(sys.argv[1], '%a, %d %b %Y %X %Z') + datetime.timedelta(days=1))" "${lm}")
                    [[ ${expired} == "False" ]] && {
                      echo "Cache is younger than a day"
                      exit
                    }
                fi

                cd $(workspaces.source.path)/go-build-cache
                tar cf - . |curl -# -L -f -F path=golang-cache.tar -X POST -F "file=@-" http://uploader:8080/upload

      - name: codecov
        runAfter:
          - unitlint
        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)
    workspaces:
      - name: source
  workspaces:
    - name: source
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 3Gi
