name: Issue Analyzer

permissions: read-all

on:
  issues:
    types: [opened, reopened, edited]

concurrency:
  group: issue-${{ format('issue-{0}', github.event.issue.number) }}
  cancel-in-progress: true

jobs:
  analyze-issue:
    name: Analyze Issues
    runs-on: ubuntu-latest
    if: ${{ contains(github.event.issue.labels.*.name, 'bug') && !contains(github.event.issue.labels.*.name, 'disable-analyzer') && !contains(github.event.issue.labels.*.name, 'confirmed') && !contains(github.event.issue.labels.*.name, '@puppeteer/browsers') && !contains(github.event.issue.labels.*.name, '@puppeteer/ng-schematics') && github.event.issue.number > 9481 }}
    env:
      ISSUE_BODY: ${{ toJson(github.event.issue.body) }}
    outputs:
      runsOn: ${{ steps.issue-analysis.outputs.runsOn }}
      nodeVersion: ${{ steps.issue-analysis.outputs.nodeVersion }}
      packageManager: ${{ steps.issue-analysis.outputs.packageManager }}
      errorMessage: ${{ steps.issue-analysis.outputs.errorMessage }}
    permissions:
      issues: write
    steps:
      - name: Remove labels
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            if (${{ contains(github.event.issue.labels.*.name, 'not-reproducible') }}) {
              github.rest.issues.removeLabel({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                name: ["not-reproducible"]
              })
            }
            if (${{ contains(github.event.issue.labels.*.name, 'needs-feedback') }}) {
              github.rest.issues.removeLabel({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                name: ["needs-feedback"]
              })
            }
            if (${{ contains(github.event.issue.labels.*.name, 'invalid') }}) {
              github.rest.issues.removeLabel({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                name: ["invalid"]
              })
            }
      - name: Check out repository
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
      - name: Set up Node.js
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version-file: '.nvmrc'
      - name: Install dependencies
        run: npm ci
      - name: Analyze issue
        id: issue-analysis
        run: echo $ISSUE_BODY | ./tools/analyze_issue.mjs >> $GITHUB_OUTPUT
      - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
        with:
          name: issue-files
          path: out/

  verify-issue:
    name: Verify Issue
    needs: analyze-issue
    runs-on: ${{ needs.analyze-issue.outputs.runsOn }}
    permissions:
      issues: write
    env:
      PACKAGE_MANAGER: ${{ needs.analyze-issue.outputs.packageManager }}
      NODE_VERSION: ${{ needs.analyze-issue.outputs.nodeVersion }}
    steps:
      - uses: actions/download-artifact@eaceaf801fd36c7dee90939fad912460b18a1ffe # v4.1.2
        with:
          name: issue-files
          path: '.'
      - name: Set up Node.js
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version: ${{ env.NODE_VERSION }}
      - name: Enable corepack
        run: corepack enable
      - name: Install dependencies
        run: ${{ env.PACKAGE_MANAGER }} install
        env:
          YARN_ENABLE_IMMUTABLE_INSTALLS: false
      - name: Install dependencies Firefox
        run: node ./node_modules/puppeteer/install.mjs
        env:
          PUPPETEER_PRODUCT: firefox
      - name: Verify issue
        timeout-minutes: 10
        run: ${{ env.PACKAGE_MANAGER }} run verify
        env:
          DEBUG: 'puppeteer:*'
      - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
        if: success() || failure()
        with:
          name: runtime-output
          path: |
            *.log
            **/*.pdf
          if-no-files-found: error

  label-verified-issue:
    needs: [verify-issue]
    if: success()
    name: Label verified issue
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - name: Add labels
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            github.rest.issues.addLabels({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              labels: ["confirmed"]
            })
      - name: Find Comment Id
        uses: peter-evans/find-comment@d5fe37641ad8451bdd80312415672ba26c86575e # v3.0.0
        id: find-comment-id
        with:
          issue-number: ${{ github.event.issue.number }}
          comment-author: 'github-actions[bot]'
      - name: Upsert comment
        uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
        with:
          comment-id: ${{ steps.find-comment-id.outputs.comment-id }}
          issue-number: ${{ github.event.issue.number }}
          edit-mode: replace
          body: |
            The issue has been labeled as **confirmed** by the automatic analyser.
            Someone from the Puppeteer team will take a look soon!

            ---
            [Analyzer run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})

  label-invalid-issue:
    needs: [analyze-issue]
    if: failure() && needs.analyze-issue.outputs.errorMessage != ''
    name: Label invalid issue
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - name: Add labels
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            github.rest.issues.addLabels({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              labels: ["invalid"]
            })
      - name: Find Comment Id
        uses: peter-evans/find-comment@d5fe37641ad8451bdd80312415672ba26c86575e # v3.0.0
        id: find-comment-id
        with:
          issue-number: ${{ github.event.issue.number }}
          comment-author: 'github-actions[bot]'
      - name: Upsert comment
        uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
        with:
          comment-id: ${{ steps.find-comment-id.outputs.comment-id }}
          issue-number: ${{ github.event.issue.number }}
          edit-mode: replace
          body: |
            ${{ needs.analyze-issue.outputs.errorMessage }}

            ---
            [Analyzer run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})

  label-unverifiable-issue:
    needs: [analyze-issue, verify-issue]
    if: failure() && needs.analyze-issue.outputs.errorMessage == ''
    name: Label unverifiable issue
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - name: Add labels
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            github.rest.issues.addLabels({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              labels: ["not-reproducible", "needs-feedback"]
            })
      - name: Find Comment Id
        uses: peter-evans/find-comment@d5fe37641ad8451bdd80312415672ba26c86575e # v3.0.0
        id: find-comment-id
        with:
          issue-number: ${{ github.event.issue.number }}
          comment-author: 'github-actions[bot]'
      - name: Upsert comment
        uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
        with:
          comment-id: ${{ steps.find-comment-id.outputs.comment-id }}
          issue-number: ${{ github.event.issue.number }}
          edit-mode: replace
          body: |
            This issue was not reproducible. Please check that your example runs locally and the following:

             - Ensure the script does not rely on dependencies outside of `puppeteer` and `puppeteer-core`.
             - Ensure the error string is just the error message.
               - Bad:

                 ```ts
                 Error: something went wrong
                   at Object.<anonymous> (/Users/username/repository/script.js:2:1)
                   at Module._compile (node:internal/modules/cjs/loader:1159:14)
                   at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
                   at Module.load (node:internal/modules/cjs/loader:1037:32)
                   at Module._load (node:internal/modules/cjs/loader:878:12)
                   at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
                   at node:internal/main/run_main_module:23:47
                 ```

               - Good: `Error: something went wrong`.
             - Ensure your configuration file (if applicable) is valid.
             - If the issue is flaky (does not reproduce all the time), make sure 'Flaky' is checked.
             - If the issue is not expected to error, make sure to write 'no error'.

            Once the above checks are satisfied, please edit your issue with the changes and we will 
            try to reproduce the bug again.

            ---
            [Analyzer run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
