name: Build & Test for feature PR
##
# This workflow tests, builds, and uploads the extension code for each PR
#
# It should also keep an updated comment on the PR showing where the upload is
#
on:
  pull_request:
    branches:
      - develop
jobs:
  build:
    if: ${{ !startsWith(github.head_ref, 'release/') && !startsWith(github.head_ref, 'e2etest/') && !github.event.pull_request.draft == true }}
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v4
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          always-auth: true
          node-version: 22
          registry-url: https://npm.pkg.github.com
          scope: '@secretkeylabs'
          cache: npm
      - name: Install dependencies
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GH_PACKAGE_REGISTRY_TOKEN }}
        run: npm ci
      - name: Test
        run: |
          npm run knip
          npx eslint .
          npx tsc --noEmit
      - name: Build
        env:
          TRANSAC_API_KEY: ${{ secrets.TRANSAC_API_KEY }}
          MOON_PAY_API_KEY: ${{ secrets.MOON_PAY_API_KEY }}
          MIX_PANEL_TOKEN: ${{ secrets.MIX_PANEL_TOKEN }}
          MIX_PANEL_EXPLORE_APP_TOKEN: ${{ secrets.MIX_PANEL_EXPLORE_APP_TOKEN }}
          SKIP_ANIMATION_WALLET_STARTUP: 'false'
        run: npm run build --if-present
      - name: Install Playwright Browsers
        run: npx playwright install chromium --with-deps
      - name: Run E2E test suite
        env:
          SEED_WORDS1: ${{ secrets.SEED_WORDS1 }}
          SEED_WORDS2: ${{ secrets.SEED_WORDS2 }}
        run: xvfb-run --auto-servernum --server-args="-screen 0 360x360x24" npm run e2etest:smoketest --reporter=html
      - name: Upload Playwright report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report
          path: playwright-report/
          retention-days: 4
      - name: Save Filename
        run: |
          BRANCH_NAME=$(echo ${{ github.head_ref }} | sed 's/\//-/g')
          GIT_SHA_SHORT=$(git rev-parse --short ${{ github.event.pull_request.head.sha }})
          echo "FILENAME=xverse-extension.$BRANCH_NAME.$GIT_SHA_SHORT" >> $GITHUB_ENV
      - name: Upload Archive
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.FILENAME }}
          path: ./build
          retention-days: 30
          if-no-files-found: error
  comment-on-pr:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    env:
      GH_TOKEN: ${{ github.token }}
    steps:
      - uses: actions/checkout@v4
      - name: Get artifact URL
        env:
          OWNER: ${{ github.repository_owner }}
          REPO: ${{ github.event.repository.name }}
          WORKFLOW_ID: ${{ github.run_id }}
        run: |
          ARTIFACT_URL="https://github.com/$OWNER/$REPO/actions/runs/$WORKFLOW_ID"
          echo "ARTIFACT_URL=$ARTIFACT_URL" >> $GITHUB_ENV
      - name: Delete old bot comments
        env:
          PR_ID: ${{ github.event.pull_request.number }}
        run: |
          gh api \
            -H "Accept: application/vnd.github+json" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            /repos/{owner}/{repo}/issues/$PR_ID/comments \
          | jq ".[] | select(.user.login==\"github-actions[bot]\") | .id" \
          | xargs -I %q gh api \
            --method DELETE \
            -H "Accept: application/vnd.github+json" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            /repos/{owner}/{repo}/issues/comments/%q
      - name: Post test package PR comment
        env:
          PR_ID: ${{ github.event.pull_request.number }}
        run: |
          gh api \
            --method POST \
            -H "Accept: application/vnd.github+json" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            /repos/{owner}/{repo}/issues/$PR_ID/comments \
            -f body="Test with build here: $ARTIFACT_URL"
