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@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
      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@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
      with:
        name: snap
        path: ${{ steps.snapcraft.outputs.snap }}

  test-snap:
    needs: build-snap
    runs-on: ubuntu-latest
    steps:
    - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
      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
