# Copyright 2024 RustFS Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Publish helm chart to artifacthub

on:
  workflow_run:
    workflows: [ "Build and Release" ]
    types: [ completed ]
  workflow_dispatch:
    inputs:
      version:
        description: "Release version to publish, e.g. 1.0.0-beta.1 or v1.0.0-beta.1"
        required: true
        default: "1.0.0-beta.1"
        type: string

permissions:
  contents: read

jobs:
  build-helm-package:
    runs-on: ubicloud-standard-2
    if: |
      github.event_name == 'workflow_dispatch' ||
      (
        github.event.workflow_run.conclusion == 'success' &&
        github.event.workflow_run.event == 'push' &&
        contains(github.event.workflow_run.head_branch, '.')
      )

    outputs:
      raw_tag: ${{ steps.version.outputs.raw_tag }}
      app_version: ${{ steps.version.outputs.app_version }}
      chart_version: ${{ steps.version.outputs.chart_version }}

    steps:
      - name: Checkout helm chart repo
        uses: actions/checkout@v6

      - name: Normalize release version
        id: version
        run: |
          set -eux

          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            RAW="${{ github.event.inputs.version }}"
          else
            RAW="${{ github.event.workflow_run.head_branch }}"
          fi

          case "$RAW" in
            refs/tags/*)
              RAW_TAG="${RAW#refs/tags/}"
              ;;
            *)
              RAW_TAG="$RAW"
              ;;
          esac

          ./scripts/helm_chart_version.sh "$RAW_TAG"

      - name: Replace chart version and app version
        run: |
          set -eux
          sed -i -E 's/^version:.*/version: "${{ steps.version.outputs.chart_version }}"/' helm/rustfs/Chart.yaml
          sed -i -E 's/^appVersion:.*/appVersion: "${{ steps.version.outputs.app_version }}"/' helm/rustfs/Chart.yaml

      - name: Set up Helm
        uses: azure/setup-helm@v4.3.0

      - name: Test Helm Chart Templates
        run: ./scripts/test_helm_templates.sh

      - name: Package Helm Chart
        run: |
          set -eux
          cp helm/README.md helm/rustfs/
          helm package ./helm/rustfs \
            --destination helm/rustfs/ \
            --version "${{ steps.version.outputs.chart_version }}"

      - name: Upload helm package as artifact
        uses: actions/upload-artifact@v6
        with:
          name: helm-package
          path: helm/rustfs/*.tgz
          retention-days: 1

  publish-helm-package:
    runs-on: ubicloud-standard-2
    needs: [ build-helm-package ]
    if: needs.build-helm-package.result == 'success'

    steps:
      - name: Checkout helm package repo
        uses: actions/checkout@v6
        with:
          repository: rustfs/helm
          token: ${{ secrets.RUSTFS_HELM_PACKAGE }}

      - name: Download helm package
        uses: actions/download-artifact@v7
        with:
          name: helm-package
          path: ./

      - name: Set up helm
        uses: azure/setup-helm@v4.3.0

      - name: Generate index
        run: helm repo index . --url https://charts.rustfs.com

      - name: Push helm package and index file
        run: |
          set -eux
          git config --global user.name "${{ secrets.USERNAME }}"
          git config --global user.email "${{ secrets.EMAIL_ADDRESS }}"
          git add .
          git commit -m "Update rustfs helm package with ${{ needs.build-helm-package.outputs.app_version }}." || echo "No changes to commit"
          git push origin main
