name: Helm linter
on:
 pull_request:
  types: ["opened", "edited", "reopened", "synchronize"]
  branches:
   - 'master'
  paths:
   - "charts/**"
jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Helm tool installer
        uses: Azure/setup-helm@v3
      - name: Setup Kubeval
        uses: lra/setup-kubeval@v1.0.1
      #check, was helm version increased in Chart.yaml?
      - name: Check version
        shell: bash
        run: |
          helm_version_new=$(cat charts/kafka-ui/Chart.yaml  | grep version | awk  '{print $2}')
          helm_version_old=$(curl -s https://raw.githubusercontent.com/provectus/kafka-ui/master/charts/kafka-ui/Chart.yaml |   grep version | awk  '{print $2}' )
          echo $helm_version_old
          echo $helm_version_new
          if [[ "$helm_version_new" > "$helm_version_old" ]]; then exit 0 ; else exit 1 ; fi
      - name: Run kubeval
        shell: bash
        run: |
          sed -i "s@enabled: false@enabled: true@g" charts/kafka-ui/values.yaml
          K8S_VERSIONS=$(git ls-remote --refs --tags https://github.com/kubernetes/kubernetes.git | cut -d/ -f3 | grep -e '^v1\.[0-9]\{2\}\.[0]\{1,2\}$' | grep -v -e  '^v1\.1[0-7]\{1\}' | cut -c2-)
          echo "NEXT K8S VERSIONS ARE GOING TO BE TESTED: $K8S_VERSIONS"
          echo ""
          for version in $K8S_VERSIONS
            do
              echo $version;
              helm template --kube-version $version --set ingress.enabled=true charts/kafka-ui -f charts/kafka-ui/values.yaml | kubeval --additional-schema-locations https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master --strict -v $version;
            done
