name: Scrape Evals
on:
  issue_comment:
    types: [created]
  pull_request:
    types: [opened]

jobs:
  quality-eval:
    if: |
      (github.event_name == 'issue_comment' &&
       github.event.issue.pull_request != null &&
       (contains(github.event.comment.body, '#scrape-quality-eval') ||
        contains(github.event.issue.title, '#scrape-quality-eval'))) ||
      (github.event_name == 'pull_request' &&
       (contains(github.event.pull_request.body, '#scrape-quality-eval') ||
        contains(github.event.pull_request.title, '#scrape-quality-eval')))
    runs-on: ubuntu-latest
    steps:
      - name: Check org membership
        id: check-member
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.SCRAPE_EVALS_PAT }}
          script: |
            try {
              await github.rest.orgs.checkMembershipForUser({
                org: 'firecrawl',
                username: context.actor,
              });
              return true;
            } catch {
              return false;
            }

      - name: Dispatch quality eval
        if: steps.check-member.outputs.result == 'true'
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.SCRAPE_EVALS_PAT }}
          script: |
            let prNumber, prBranch, commitSha, headRepo;
            if (context.eventName === 'pull_request') {
              prNumber = context.payload.pull_request.number;
              prBranch = context.payload.pull_request.head.ref;
              commitSha = context.payload.pull_request.head.sha;
              headRepo = context.payload.pull_request.head.repo.full_name;
            } else {
              const pr = await github.rest.pulls.get({
                owner: context.repo.owner,
                repo: context.repo.repo,
                pull_number: context.issue.number,
              });
              prNumber = pr.data.number;
              prBranch = pr.data.head.ref;
              commitSha = pr.data.head.sha;
              headRepo = pr.data.head.repo.full_name;
            }
            await github.rest.repos.createDispatchEvent({
              owner: 'firecrawl',
              repo: 'scrape-evals',
              event_type: 'firecrawl-pr-eval',
              client_payload: {
                pr_number: prNumber,
                pr_branch: prBranch,
                commit_sha: commitSha,
                repo: headRepo,
              },
            });

  ocr-eval:
    if: |
      (github.event_name == 'issue_comment' &&
       github.event.issue.pull_request != null &&
       (contains(github.event.comment.body, '#scrape-ocr-eval') ||
        contains(github.event.issue.title, '#scrape-ocr-eval'))) ||
      (github.event_name == 'pull_request' &&
       (contains(github.event.pull_request.body, '#scrape-ocr-eval') ||
        contains(github.event.pull_request.title, '#scrape-ocr-eval')))
    runs-on: ubuntu-latest
    steps:
      - name: Check org membership
        id: check-member
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.SCRAPE_EVALS_PAT }}
          script: |
            try {
              await github.rest.orgs.checkMembershipForUser({
                org: 'firecrawl',
                username: context.actor,
              });
              return true;
            } catch {
              return false;
            }

      - name: Dispatch OCR eval
        if: steps.check-member.outputs.result == 'true'
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.SCRAPE_EVALS_PAT }}
          script: |
            let prNumber, prBranch, commitSha, headRepo;
            if (context.eventName === 'pull_request') {
              prNumber = context.payload.pull_request.number;
              prBranch = context.payload.pull_request.head.ref;
              commitSha = context.payload.pull_request.head.sha;
              headRepo = context.payload.pull_request.head.repo.full_name;
            } else {
              const pr = await github.rest.pulls.get({
                owner: context.repo.owner,
                repo: context.repo.repo,
                pull_number: context.issue.number,
              });
              prNumber = pr.data.number;
              prBranch = pr.data.head.ref;
              commitSha = pr.data.head.sha;
              headRepo = pr.data.head.repo.full_name;
            }
            await github.rest.repos.createDispatchEvent({
              owner: 'firecrawl',
              repo: 'scrape-evals',
              event_type: 'firecrawl-ocr-eval',
              client_payload: {
                pr_number: prNumber,
                pr_branch: prBranch,
                commit_sha: commitSha,
                repo: headRepo,
              },
            });
