name: review

on:
  issue_comment:
    types: [created]

jobs:
  check-guidelines:
    if: |
      github.event.issue.pull_request &&
      startsWith(github.event.comment.body, '/review') &&
      contains(fromJson('["OWNER","MEMBER"]'), github.event.comment.author_association)
    runs-on: blacksmith-4vcpu-ubuntu-2404
    permissions:
      contents: read
      pull-requests: write
    steps:
      - name: Get PR number
        id: pr-number
        run: |
          if [ "${{ github.event_name }}" = "pull_request_target" ]; then
            echo "number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
          else
            echo "number=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
          fi

      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - uses: ./.github/actions/setup-bun

      - name: Install opencode
        run: curl -fsSL https://opencode.ai/install | bash

      - name: Get PR details
        id: pr-details
        run: |
          gh api /repos/${{ github.repository }}/pulls/${{ steps.pr-number.outputs.number }} > pr_data.json
          echo "title=$(jq -r .title pr_data.json)" >> $GITHUB_OUTPUT
          echo "sha=$(jq -r .head.sha pr_data.json)" >> $GITHUB_OUTPUT
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Check PR guidelines compliance
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          OPENCODE_PERMISSION: '{ "bash": {  "*": "deny", "gh*": "allow", "gh pr review*": "deny" } }'
          PR_TITLE: ${{ steps.pr-details.outputs.title }}
        run: |
          PR_BODY=$(jq -r .body pr_data.json)
          opencode run -m anthropic/claude-opus-4-5 "A new pull request has been created: '${PR_TITLE}'

          <pr-number>
          ${{ steps.pr-number.outputs.number }}
          </pr-number>

          <pr-description>
          $PR_BODY
          </pr-description>

          Please check all the code changes in this pull request against the style guide, also look for any bugs if they exist. Diffs are important but make sure you read the entire file to get proper context. Make it clear the suggestions are merely suggestions and the human can decide what to do

          When critiquing code against the style guide, be sure that the code is ACTUALLY in violation, don't complain about else statements if they already use early returns there. You may complain about excessive nesting though, regardless of else statement usage.
          When critiquing code style don't be a zealot, we don't like "let" statements but sometimes they are the simplest option, if someone does a bunch of nesting with let, they should consider using iife (see packages/opencode/src/util.iife.ts)

          Use the gh cli to create comments on the files for the violations. Try to leave the comment on the exact line number. If you have a suggested fix include it in a suggestion code block.
          If you are writing suggested fixes, BE SURE THAT the change you are recommending is actually valid typescript, often I have seen missing closing "}" or other syntax errors.
          Generally, write a comment instead of writing suggested change if you can help it.

          Command MUST be like this.
          \`\`\`
          gh api \
            --method POST \
            -H \"Accept: application/vnd.github+json\" \
            -H \"X-GitHub-Api-Version: 2022-11-28\" \
            /repos/${{ github.repository }}/pulls/${{ steps.pr-number.outputs.number }}/comments \
            -f 'body=[summary of issue]' -f 'commit_id=${{ steps.pr-details.outputs.sha }}' -f 'path=[path-to-file]' -F \"line=[line]\" -f 'side=RIGHT'
          \`\`\`

          Only create comments for actual violations. If the code follows all guidelines, comment on the issue using gh cli: 'lgtm' AND NOTHING ELSE!!!!."
