name: Release on Commit (Canary)

on:
  push:
    branches:
      - 'renovate/*'

jobs:
  find-tags-to-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write # for creating tags
    outputs:
      release-tags: ${{ steps.list-release-tags.outputs.release-tags }}
      release-streams: ${{ steps.list-release-tags.outputs.release-streams }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Configure Git
        run: |
          git config user.name "${{ github.actor }}"
          git config user.email "${{ github.actor}}@users.noreply.github.com"

      - name: Get list of changed charts
        id: list-changed-charts
        uses: tj-actions/changed-files@v35.9.2
        with:
          files: charts/**
          dir_names: "true"
          dir_names_max_depth: "2"

      - name: List and create tags
        id: list-release-tags
        shell: bash
        run: |
          tags=()
          declare -A release_streams=()
          for chart_dir in ${{ steps.list-changed-charts.outputs.all_modified_files }}; do
            chart_name=$(echo ${chart_dir} | cut -d '/' -f 2)

            latest_version="$(git -c 'versionsort.suffix=-' tag --sort 'version:refname' --list "$chart_name-*" | grep -Ev '.*-[[:digit:]]+.[[:digit:]]+.[[:digit:]]+-.*' | tail -n1)"

            next_patch="$(echo "$latest_version" | sed -E 's/(.*-)?([[:digit:]]+).([[:digit:]]+).([[:digit:]]+)/echo \2.\3.$((\4 + 1))/e')"
            last_pre_tag="$(git -c 'versionsort.suffix=-' tag --sort 'version:refname' --list "$chart_name-$next_patch-*" | tail -n1)"
            if [ -z "$last_pre_tag" ]; then
              index=1
            else
              index=$(echo "$last_pre_tag" | sed -E 's/(.*-)?[[:digit:]]+.[[:digit:]]+.[[:digit:]]+-canary.([[:digit:]]+)(#.*)?/echo $((\2 + 1))/e')
            fi
            next_tag="$chart_name-$next_patch-canary.$index"

            tags+=("$next_tag")
            git tag -a "$next_tag" -m "Release $next_tag"

            release_streams["canary"]=1
          done

          git push --tags

          echo "release-tags<<EOF" >> $GITHUB_OUTPUT
          echo "$(jq -Rc '. / " "' <<< ${tags[*]})" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

          echo "release-streams<<EOF" >> $GITHUB_OUTPUT
          echo "$(jq -Rc '. / " "' <<< ${!release_streams[@]})" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

  call-release:
    needs: find-tags-to-release
    uses: ./.github/workflows/make-chart-release.yaml
    permissions:
      contents: write # for updating index.yaml
    strategy:
      matrix:
        tag: ${{ fromJson(needs.find-tags-to-release.outputs.release-tags) }}
      fail-fast: false
    if: ${{ needs.find-tags-to-release.outputs.release-tags != '[]' }}
    with:
      tag: ${{ matrix.tag }}

  call-update-helm-repo:
    needs:
      - call-release
      - find-tags-to-release
    uses: ./.github/workflows/update-helm-repo.yaml
    permissions:
      contents: write # for updating index.yaml
    strategy:
      matrix:
        repo: ${{ fromJson(needs.find-tags-to-release.outputs.release-streams) }}
    if: ${{ needs.find-tags-to-release.outputs.release-streams != '[]' }}
    with:
      repo: ${{ matrix.repo }}