{{ if .Values.pipelines.deployableResources.tasks }}
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: helm-docs
  labels:
    app.kubernetes.io/version: "0.1"
  annotations:
    tekton.dev/categories: CI
    tekton.dev/pipelines.minVersion: "0.41.0"
    tekton.dev/tags: helm
    tekton.dev/platforms: "linux/amd64"
spec:
  description: >-
   This task generates documentation for Helm charts using `helm-docs` and validates if the generated README.md is up-to-date.
  workspaces:
    - description: A workspace that contains fetched git repo.
      name: source
  params:
    - name: CHART_DIR
      description: The directory in source that contains the helm chart
      default: "."
    - name: helm_docs_image
      description: "Specify a specific helm-docs image"
      default: "{{ include "edp-tekton.registry" . }}/jnorwood/helm-docs:v1.13.1"
    - name: git_image
      description: "Specify a specific git image"
      default: "{{ include "edp-tekton.registry" . }}/alpine/git:v2.26.2"
    - name: user_home
      description: |
        Absolute path to the user's home directory. Set this explicitly if you are running the image as a non-root user
      type: string
      default: "/tekton/home"
  steps:
    - name: helm-docs
      image: $(params.helm_docs_image)
      workingDir: $(workspaces.source.path)
      env:
        - name: CHART_DIR
          value: $(params.CHART_DIR)
        - name: HOME
          value: $(params.user_home)
      script: |
        set -ex

        README_FILE_PATH="${CHART_DIR}/README.md"
        if [ -f "${README_FILE_PATH}" ]; then
            echo "[TEKTON][INFO] The file has been found at the given location \"${README_FILE_PATH}\""
        else
            echo "[TEKTON][ERROR] The file has not been found at the given location \"${README_FILE_PATH}\""
            exit 1
        fi

        helm-docs --chart-search-root ${CHART_DIR}
    - name: validate-helm-docs
      image: $(params.git_image)
      workingDir: $(workspaces.source.path)
      env:
        - name: CHART_DIR
          value: $(params.CHART_DIR)
        - name: HOME
          value: $(params.user_home)
      script: |
        set -ex

        git config --global --add safe.directory $(pwd)
        git diff -s --exit-code ${CHART_DIR}/README.md || (echo "Run 'helm-docs' to address the issue." && git diff && exit 1)
{{ end }}
