name: Snap workflow
on:
  workflow_call:
    inputs:
      deploy:
        required: true
        type: boolean

permissions:
  contents: read

jobs:
  build-snap:
    runs-on: ubuntu-latest
    outputs:
      snap_name: ${{ steps.snapcraft.outputs.snap }}
      channel: ${{ steps.channel.outputs.channel }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
      - id: channel
        run: |
          if git describe --exact-match; then
            echo "channel=candidate" >> $GITHUB_OUTPUT
          else
            echo "channel=edge" >> $GITHUB_OUTPUT
          fi
      - id: prep
        run: |
          mkdir -p snap
          ln -s ../contrib/snap/snapcraft.yaml snap/snapcraft.yaml
      - uses: snapcore/action-build@3bdaa03e1ba6bf59a65f84a751d943d549a54e79 # v1.3.0
        id: snapcraft
      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: snap
          path: ${{ steps.snapcraft.outputs.snap }}

  test-snap:
    needs: build-snap
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        id: download
        with:
          name: snap
      - id: install-snap
        run: |
          sudo apt remove fwupd -y
          sudo snap install --dangerous ${{ needs.build-snap.outputs.snap_name }}
          sudo snap connect fwupd:polkit :polkit
          sudo fwupd.fwupdtool enable-test-devices
      - name: Set up snap specific environment
        run: |
          # location which can be used as TMPDIR and is accessible from both snap
          # sandbox and from the host
          SNAP_TEST_TMPDIR=/var/snap/fwupd/common/fwupd-tests
          sudo mkdir -p "$SNAP_TEST_TMPDIR"
          echo "SNAP_TEST_TMPDIR=$SNAP_TEST_TMPDIR" >> "$GITHUB_ENV"
      - name: Run fwupdmgr tests
        run: |
          sudo TMPDIR="$SNAP_TEST_TMPDIR" \
              /snap/fwupd/current/share/installed-tests/fwupd/fwupdmgr.sh
      - name: Run fwupd tests
        run: |
          sudo TMPDIR="$SNAP_TEST_TMPDIR" \
              /snap/fwupd/current/share/installed-tests/fwupd/fwupd.sh
      - name: Run fwupdtool tests
        run: |
          sudo TMPDIR="$SNAP_TEST_TMPDIR" \
              /snap/fwupd/current/share/installed-tests/fwupd/fwupdtool.sh
      - name: Run fwupdtool efiboot specific tests
        run: |
          sudo TMPDIR="$SNAP_TEST_TMPDIR" \
              /snap/fwupd/current/share/installed-tests/fwupd/fwupdtool-efiboot.sh

  deploy-store:
    needs: [build-snap, test-snap]
    runs-on: ubuntu-latest
    if: ${{ inputs.deploy }}
    steps:
      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        id: download
        with:
          name: snap
      - uses: snapcore/action-publish@214b86e5ca036ead1668c79afb81e550e6c54d40 # v1.2.0
        env:
          SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.STORE_LOGIN }}
        with:
          snap: ${{ needs.build-snap.outputs.snap_name }}
          release: ${{ needs.build-snap.outputs.channel }}
