# For every push to the primary branch with .release-plan.json modified,
# runs release-plan.

name: Publish Stable

on:
  workflow_dispatch:
  push:
    branches:
      - main
      - master
    paths:
      - '.release-plan.json'

concurrency:
  group: publish-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

jobs:

  build:
    name: Build extensions
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm
      - run: pnpm install
      - name: Download panes
        run: node scripts/download-panes.js
        env:
          EMBER_ENV: production
      - name: Build
        run: pnpm build:production
      - name: Pack
        run: |
          VERSION="$(jq -r '.version' package.json)"

          pnpm pack
          mkdir -p dist/npm
          tar xvzf "ember-inspector-$VERSION.tgz" -C dist/npm --strip-components 1
      - name: Upload artifacts (bookmarklet)
        uses: actions/upload-artifact@v4
        with:
          name: bookmarklet
          path: dist/bookmarklet
      - name: Upload artifacts (Chrome)
        uses: actions/upload-artifact@v4
        with:
          name: chrome
          path: dist/chrome
      - name: Upload artifacts (Firefox)
        uses: actions/upload-artifact@v4
        with:
          name: firefox
          path: dist/firefox
      - name: Upload artifacts (npm)
        uses: actions/upload-artifact@v4
        with:
          name: npm
          path: dist/npm

  publish-bookmarklet:
    name: Publish bookmarklet
    needs:
      - build
    runs-on: ubuntu-latest
    steps:
      - name: Download artifacts (bookmarklet)
        uses: actions/download-artifact@v4
        with:
          name: bookmarklet
          path: bookmarklet
      - name: Upload to S3
        uses: jakejarvis/s3-sync-action@v0.5.1
        with:
          args: --acl public-read --cache-control "max-age=86400000,public"
        env:
          AWS_S3_BUCKET: ember-extension
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          SOURCE_DIR: bookmarklet
          DEST_DIR: dist_bookmarklet

  publish-chrome:
    name: Publish Chrome extension
    needs:
      - build
    runs-on: ubuntu-latest
    steps:
      - name: Set up node
        uses: actions/setup-node@v4
      - name: Install dependencies (chrome-webstore-upload-cli)
        run: npm install -g chrome-webstore-upload-cli
      - name: Download artifacts (Chrome)
        uses: actions/download-artifact@v4
        with:
          name: chrome
          path: chrome
      - name: Set Environment Variables
        run: |
          echo "EXTENSION_ID=bmdblncegkenkacieihfhpjfppoconhi" >> $GITHUB_ENV
          echo "CLIENT_ID=${{ secrets.GOOGLE_NIGHTLY_CLIENT_ID }}" >> $GITHUB_ENV
          echo "CLIENT_SECRET=${{ secrets.GOOGLE_NIGHTLY_CLIENT_SECRET }}" >> $GITHUB_ENV
          echo "REFRESH_TOKEN=${{ secrets.GOOGLE_NIGHTLY_REFRESH_TOKEN }}" >> $GITHUB_ENV
      - name: Upload to Chrome Web Store
        run: chrome-webstore-upload upload --source chrome --auto-publish

  publish-firefox:
    name: Publish Firefox extension
    needs:
      - build
    runs-on: ubuntu-latest
    steps:
      - name: Set up nod
        uses: actions/setup-node@v4
      - name: Install dependencies (web-ext)
        run: |
          npm install -g web-ext
          # https://github.com/mozilla/web-ext/issues/804
          npm install -g web-ext-submit
      - name: Download artifacts (Firefox)
        uses: actions/download-artifact@v4
        with:
          name: firefox
          path: firefox
      - name: Upload to AMO
        working-directory: firefox
        run: web-ext-submit --channel=listed
        env:
          WEB_EXT_API_KEY: ${{ secrets.FIREFOX_API_KEY }}
          WEB_EXT_API_SECRET: ${{ secrets.FIREFOX_API_SECRET }}

  publish:
    name: "NPM Publish"
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
      attestations: write

    steps:
      - uses: actions/checkout@v5
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v6
        with:
          node-version: 22
          registry-url: 'https://registry.npmjs.org'
          cache: pnpm
      - run: pnpm install --frozen-lockfile
      - run: npm install -g npm@latest # ensure that the globally installed npm is new enough to support OIDC
      - name: Publish to NPM
        run: NPM_CONFIG_PROVENANCE=true pnpm release-plan publish
        env:
          GITHUB_AUTH: ${{ secrets.GITHUB_TOKEN }}
