name: Test Plugin Conversion

on:
  # push:
  # pull_request:
  workflow_dispatch:

concurrency:
  group: convert-${{ github.ref }}
  cancel-in-progress: true

jobs:
  get-plugins:
    runs-on: ubuntu-latest
    steps:
      - id: plugin_names
        run: echo "::set-output name=plugins::$(curl -s https://api.napari-hub.org/plugins | jq -c 'keys')"
    outputs:
      plugins: ${{ steps.plugin_names.outputs.plugins }}

  convert:
    needs: get-plugins
    name: convert ${{ matrix.plugin }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        # plugin: ["napari-dv"]
        plugin: ${{ fromJson(needs.get-plugins.outputs.plugins) }}

    steps:
      - uses: tlambert03/setup-qt-libs@19e4ef2d781d81f5f067182e228b54ec90d23b76 # v1.8
      - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: 3.9

      - name: Install dependencies
        run: |
          pip install -U pip
          # just in case... we ask them not to depend on this or Pyside
          # since it's up to the enduser to have with napari
          pip install PyQt5
          pip install git+https://github.com/napari/npe2.git@refs/pull/60/head#egg=npe2

      - name: Fetch repo URL
        run: |
          URL=$(curl -s https://api.napari-hub.org/plugins/${{ matrix.plugin }} | jq '.code_repository')
          URL=${URL#'"https://github.com/'}
          URL=${URL%'"'}
          echo "plugin_repo=$URL" >> $GITHUB_ENV

      - name: Checkout plugin repo
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.plugin_repo }}
          path: 'plugin_repo'
          fetch-depth: 0

      - name: Install ${{ matrix.plugin }}
        run: pip install -e ./plugin_repo

      - name: Test Conversion
        id: test-without-napari
        uses: aganders3/headless-gui@f85dd6316993505dfc5f21839d520ae440c84816 # v2.2
        continue-on-error: true
        with:
          run: npe2 convert ./plugin_repo

      - name: Install napari
        if: ${{ steps.test-without-napari.outcome == 'failure' }}
        run: pip install napari

      - name: Test Conversion again with napari
        id: test-with-napari
        if: ${{ steps.test-without-napari.outcome == 'failure' }}
        uses: aganders3/headless-gui@f85dd6316993505dfc5f21839d520ae440c84816 # v2.2
        with:
          run: npe2 convert ./plugin_repo

      - name: Test Conversion again with napari
        if: ${{ steps.test-without-napari.outcome == 'failure' && steps.test-with-napari.outcome == 'failure' }}
        uses: aganders3/headless-gui@f85dd6316993505dfc5f21839d520ae440c84816 # v2.2
        with:
          # try without modifying directory
          run: npe2 convert -n ${{ matrix.plugin }}

      # this won't work, we'd need to first fork the repo somewhere we have write permissions
      # then push changes that that repository, and then create a PR to the original repo
      # - name: Create Pull Request
      #   if: success()
      #   uses: peter-evans/create-pull-request@v3
      #   with:
      #     commit-message: convert plugin to npe2 format
      #     title: 'Convert to npe2 plugin'
      #     body: |
      #       This PR adds an (autogenerated) npe2 manifest, and updates setup.cfg (if setup.cfg is used).
      #       If you use setup.py instead, please update the entry_point manually:
      #         entry_points = {'napari.manifest': "your-package = your_package:napari.yaml"}
