name: Trigger release workflows upon Publish

on:
  release:
    types: [published, released]

permissions:
  contents: write
  packages: write

jobs:
  build-docker:
    uses: ./.github/workflows/docker_manifest.yml
    with:
      release_channel: |-
        ${{ contains(github.event.release.name, 'Beta') && 'beta' || contains(github.event.release.name, 'Alpha') && 'alpha' }}
    secrets: inherit

  package-ppa:
    strategy:
      fail-fast: false
      matrix:
        series: [plucky, oracular, noble, jammy]
    uses: ./.github/workflows/package_ppa.yml
    with:
      ppa_repo: |-
        ppa:meshtastic/${{ contains(github.event.release.name, 'Beta') && 'beta' || contains(github.event.release.name, 'Alpha') && 'alpha' }}
      series: ${{ matrix.series }}
    secrets: inherit

  package-obs:
    uses: ./.github/workflows/package_obs.yml
    with:
      obs_project: |-
        network:Meshtastic:${{ contains(github.event.release.name, 'Beta') && 'beta' || contains(github.event.release.name, 'Alpha') && 'alpha' }}
      series: |-
        ${{ contains(github.event.release.name, 'Beta') && 'beta' || contains(github.event.release.name, 'Alpha') && 'alpha' }}
    secrets: inherit

  hook-copr:
    uses: ./.github/workflows/hook_copr.yml
    with:
      copr_project: |-
        ${{ contains(github.event.release.name, 'Beta') && 'beta' || contains(github.event.release.name, 'Alpha') && 'alpha' }}
    secrets: inherit

  # Create a PR to bump version when a release is Published
  bump-version:
    if: github.event.action == 'published'
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
      contents: write
    defaults:
      run:
        shell: bash
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: 3.x

      - name: Bump version.properties
        run: |
          # Bump version.properties
          chmod +x ./bin/bump_version.py
          ./bin/bump_version.py

      - name: Get new release version string
        run: |
          echo "short=$(./bin/buildinfo.py short)" >> $GITHUB_OUTPUT
        id: new_version

      - name: Ensure debian deps are installed
        run: |
          sudo apt-get update -y --fix-missing
          sudo apt-get install -y devscripts

      - name: Update debian changelog
        run: |
          # Update debian changelog
          chmod +x ./debian/ci_changelog.sh
          ./debian/ci_changelog.sh

      - name: Bump org.meshtastic.meshtasticd.metainfo.xml
        run: |
          # Bump org.meshtastic.meshtasticd.metainfo.xml
          pip install -r bin/bump_metainfo/requirements.txt -q
          chmod +x ./bin/bump_metainfo/bump_metainfo.py
          ./bin/bump_metainfo/bump_metainfo.py --file bin/org.meshtastic.meshtasticd.metainfo.xml "${{ steps.new_version.outputs.short }}"
        env:
          PIP_DISABLE_PIP_VERSION_CHECK: 1

      - name: Create Bumps pull request
        uses: peter-evans/create-pull-request@v7
        with:
          base: ${{ github.event.repository.default_branch }}
          title: Bump release version
          commit-message: automated bumps
          add-paths: |
            version.properties
            debian/changelog
            bin/org.meshtastic.meshtasticd.metainfo.xml
