name: Update Flathub

on:
  workflow_dispatch:
    inputs:
      tag:
        description: Release tag to publish to Flathub
        required: true
        type: string

permissions:
  contents: read

jobs:
  update-flathub:
    name: Update Flathub manifest
    runs-on: ubuntu-latest
    steps:
      - name: Checkout app repo
        uses: actions/checkout@v4

      - name: Checkout Flathub repo
        uses: actions/checkout@v4
        with:
          repository: flathub/app.yaak.Yaak
          token: ${{ secrets.FLATHUB_TOKEN }}
          path: flathub-repo

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22"

      - name: Install source generators
        run: |
          pip install flatpak-node-generator tomlkit aiohttp
          git clone --depth 1 https://github.com/flatpak/flatpak-builder-tools flatpak/flatpak-builder-tools

      - name: Run update-manifest.sh
        run: bash flatpak/update-manifest.sh "${{ inputs.tag }}" flathub-repo

      - name: Commit and push to Flathub
        working-directory: flathub-repo
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add -A
          git diff --cached --quiet && echo "No changes to commit" && exit 0
          git commit -m "Update to ${{ inputs.tag }}"
          git push
