name: Publish to AMO on FF version bump

on:
  push:
    branches: [main]
    paths: [firefox/extension/manifest.json]

jobs:
  check-version-to-publish:
    runs-on: ubuntu-latest
    steps:

      - name: Checkout adamlui/chatgpt-auto-continue
        uses: actions/checkout@v4
        with:
          sparse-checkout: firefox
          fetch-depth: 2

      - name: Check FF manifest for version bump
        run: |
          current_ver=$(cat firefox/extension/manifest.json | grep '"version"' | sed -E 's/.*"version": "(.*)".*/\1/')
          prev_ver=$(git show HEAD^:firefox/extension/manifest.json | grep '"version"' | sed -E 's/.*"version": "(.*)".*/\1/')
          if [ "$current_ver" != "$prev_ver" ] ; then
               echo "Extension updated from $prev_ver to $current_ver"
               echo "VERSION_CHANGED=true" >> $GITHUB_ENV
          else echo "VERSION_CHANGED=false" >> $GITHUB_ENV ; fi

      - name: Create zipball
        if: env.VERSION_CHANGED == 'true'
        run: |
          cd firefox/extension
          zip -r ../../extension.zip .

      - name: Publish to AMO
        if: env.VERSION_CHANGED == 'true'
        uses: cardinalby/webext-buildtools-firefox-addons-action@v1
        with:
          extensionId: 'chatgpt.auto.continue@chatgptevo.com'
          jwtIssuer: ${{ secrets.AMO_ISSUER }}
          jwtSecret: ${{ secrets.AMO_SECRET }}
          zipFilePath: extension.zip
        continue-on-error: true
