name: Publish Release

on:
  workflow_dispatch:
    inputs:
      chart:
        description: 'Chart version (e.g. 2.11.3)'
        required: true
        type: string

permissions:
  contents: write
  pull-requests: write

jobs:
  chart-pr:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0

      - name: Config Git
        run: |
          git config user.name "$GITHUB_ACTOR"
          git config user.email "$GITHUB_ACTOR@users.noreply.github.com"

      - name: Fetch Versions
        run: |
          echo NEW_VERSION=$(git describe --tags --match "v[0-9]*" --abbrev=0 | tr -d v) >> "$GITHUB_ENV"
          echo PREV_VERSION=$(grep appVersion helm/sealed-secrets/Chart.yaml | grep -o '[0-9.]*') >> "$GITHUB_ENV"

      - name: Update Version
        run: |
          sed -i "s/version: .*/version: ${{ inputs.chart }}/" helm/sealed-secrets/Chart.yaml
          sed -i "s/appVersion: .*/appVersion: $NEW_VERSION/" helm/sealed-secrets/Chart.yaml
          sed -i "s/tag: .*/tag: $NEW_VERSION/" helm/sealed-secrets/values.yaml
          sed -i "s/\`$PREV_VERSION\`/\`$NEW_VERSION\`/" helm/sealed-secrets/README.md
          git checkout -B 'release-chart-${{ inputs.chart }}'
          git add helm/sealed-secrets/Chart.yaml helm/sealed-secrets/values.yaml helm/sealed-secrets/README.md
          git commit -sm 'Release chart ${{ inputs.chart }}'
          git push origin 'release-chart-${{ inputs.chart }}'

      - name: Create PR
        run: gh pr create --fill --base main --repo $GITHUB_REPOSITORY
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
