apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: semver-bump
  labels:
    dagweave.io/catalog-item: semver-bump
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.0"
    dagweave.io/catalog-category: "CI/CD"
spec:
  entrypoint: bump
  templates:
    - name: bump
      inputs:
        parameters:
          # The current version to bump from, e.g. "1.4.2" or "v1.4.2".
          - name: current-version
          # Which part to increment: major, minor or patch.
          - name: bump
            default: patch
      outputs:
        parameters:
          # The computed next version, exposed as an Argo output parameter so
          # downstream steps can consume it via
          # {{steps.<name>.outputs.parameters.next-version}} (or tasks.* in a DAG).
          - name: next-version
            valueFrom:
              path: /tmp/next-version
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # alpine:3.24 — POSIX shell is enough to parse and increment a semver.
        image: alpine@sha256:be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a
        command: [sh, -c]
        # The current version and bump type are passed as env vars (below), never
        # interpolated from Argo into the shell. The result is written to
        # /tmp/next-version, which the outputs.parameters valueFrom.path reads.
        args:
          - |
            set -eu
            V="${CURRENT#v}"
            MAJOR="${V%%.*}"
            REST="${V#*.}"
            MINOR="${REST%%.*}"
            PATCH="${REST#*.}"
            # Drop any pre-release / build metadata suffix on the patch component.
            PATCH="${PATCH%%[-+]*}"
            case "$BUMP" in
              major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;;
              minor) MINOR=$((MINOR + 1)); PATCH=0 ;;
              patch) PATCH=$((PATCH + 1)) ;;
              *) echo "invalid bump type: $BUMP (want major|minor|patch)" >&2; exit 1 ;;
            esac
            printf '%s.%s.%s' "$MAJOR" "$MINOR" "$PATCH" | tee /tmp/next-version
        env:
          - name: CURRENT
            value: "{{inputs.parameters.current-version}}"
          - name: BUMP
            value: "{{inputs.parameters.bump}}"
