apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: semver
spec:
  timeouts:
    pipeline: "2h0m0s"
    tasks: "1h0m0s"
    finally: "0h30m0s"
  taskRunTemplate:
    serviceAccountName: build-bot
  pipelineSpec:
    params:
      - name: REPO_OWNER
        description: Owner of the repo on gitea
        type: string
      - name: REPO_NAME
        description: Name of the repo on gitea
        type: string
      - name: PULL_BASE_SHA
        description: The commit sha
        type: string
      - name: JOB_NAME
        description: The name of the job to report to gitea
        type: string
    tasks:
      - name: calculate-tag
        runAfter:
          - fetch-repository
        workspaces:
          - name: source
            workspace: git-source
        taskSpec:
          metadata: {}
          stepTemplate:
            image: alpine:3.20
            computeResources:
              requests:
                cpu: 10m
                memory: 600Mi
            workingDir: /workspace/source
          results:
            - name: tag
              description: The tag to use for the docker container
          steps:
            - image: alpine/git:2.43.0
              name: calculate-tag
              script: |
                #!/usr/bin/env sh
                set -euo pipefail
                git config --global --add safe.directory $(workspaces.source.path)
                git fetch --tags
                current_tag=$(git tag --points-at HEAD --list 'v*.*.*')
                if [ -z "$current_tag" ]; then
                  prev_tag=$(git tag --list 'v*.*.*' | sort -V -r | head -n 1)
                  if [ -n "$prev_tag" ]; then
                    last_bit=$(echo "$prev_tag" | cut -d '.' -f 3)
                    incremented=$((last_bit + 1))
                    prefix=$(echo "$prev_tag" | grep -oE 'v[0-9]*\.[0-9]*\.')
                    final_tag="${prefix}${incremented}"
                  else
                    final_tag="v0.0.1"
                  fi
                  echo -n "${final_tag}" | tee $(results.tag.path)
                  git tag "${final_tag}"
                  git push origin "${final_tag}"
                else
                  echo -n "${current_tag}" | tee $(results.tag.path)
                fi
      - name: report-pending
        taskRef:
          resolver: git
          params:
            - name: url
              value: https://code.fizz.buzz/mirror/catalog.git # mirror of https://github.com/tektoncd/catalog.git
            - name: revision
              value: df36b3853a5657fd883015cdbf07ad6466918acf
            - name: pathInRepo
              value: task/gitea-set-status/0.1/gitea-set-status.yaml
        params:
          - name: CONTEXT
            value: "$(params.JOB_NAME)"
          - name: REPO_FULL_NAME
            value: "$(params.REPO_OWNER)/$(params.REPO_NAME)"
          - name: GITEA_HOST_URL
            value: code.fizz.buzz
          - name: SHA
            value: "$(tasks.fetch-repository.results.commit)"
          - name: DESCRIPTION
            value: "Build $(params.JOB_NAME) has started"
          - name: STATE
            value: pending
          - name: TARGET_URL
            value: "https://tekton.fizz.buzz/#/namespaces/$(context.pipelineRun.namespace)/pipelineruns/$(context.pipelineRun.name)"
      - name: fetch-repository
        taskRef:
          resolver: git
          params:
            - name: url
              value: https://code.fizz.buzz/mirror/catalog.git # mirror of https://github.com/tektoncd/catalog.git
            - name: revision
              value: df36b3853a5657fd883015cdbf07ad6466918acf
            - name: pathInRepo
              value: task/git-clone/0.9/git-clone.yaml
        workspaces:
          - name: output
            workspace: git-source
        params:
          - name: url
            value: $(params.REPO_URL)
          - name: revision
            value: $(params.PULL_BASE_SHA)
          - name: deleteExisting
            value: "true"
    finally:
      - name: report-success
        when:
          - input: "$(tasks.status)"
            operator: in
            values: ["Succeeded", "Completed"]
        taskRef:
          resolver: git
          params:
            - name: url
              value: https://code.fizz.buzz/mirror/catalog.git # mirror of https://github.com/tektoncd/catalog.git
            - name: revision
              value: df36b3853a5657fd883015cdbf07ad6466918acf
            - name: pathInRepo
              value: task/gitea-set-status/0.1/gitea-set-status.yaml
        params:
          - name: CONTEXT
            value: "$(params.JOB_NAME)"
          - name: REPO_FULL_NAME
            value: "$(params.REPO_OWNER)/$(params.REPO_NAME)"
          - name: GITEA_HOST_URL
            value: code.fizz.buzz
          - name: SHA
            value: "$(tasks.fetch-repository.results.commit)"
          - name: DESCRIPTION
            value: "Build $(params.JOB_NAME) has succeeded"
          - name: STATE
            value: success
          - name: TARGET_URL
            value: "https://tekton.fizz.buzz/#/namespaces/$(context.pipelineRun.namespace)/pipelineruns/$(context.pipelineRun.name)"
      - name: report-failure
        when:
          - input: "$(tasks.status)"
            operator: in
            values: ["Failed"]
        taskRef:
          resolver: git
          params:
            - name: url
              value: https://code.fizz.buzz/mirror/catalog.git # mirror of https://github.com/tektoncd/catalog.git
            - name: revision
              value: df36b3853a5657fd883015cdbf07ad6466918acf
            - name: pathInRepo
              value: task/gitea-set-status/0.1/gitea-set-status.yaml
        params:
          - name: CONTEXT
            value: "$(params.JOB_NAME)"
          - name: REPO_FULL_NAME
            value: "$(params.REPO_OWNER)/$(params.REPO_NAME)"
          - name: GITEA_HOST_URL
            value: code.fizz.buzz
          - name: SHA
            value: "$(tasks.fetch-repository.results.commit)"
          - name: DESCRIPTION
            value: "Build $(params.JOB_NAME) has failed"
          - name: STATE
            value: failure
          - name: TARGET_URL
            value: "https://tekton.fizz.buzz/#/namespaces/$(context.pipelineRun.namespace)/pipelineruns/$(context.pipelineRun.name)"
    workspaces:
      - name: git-source
  workspaces:
    - name: git-source
      volumeClaimTemplate:
        spec:
          storageClassName: "nfs-client"
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 10Gi
      subPath: source
  params: []