name: AI Code Review

on:
  pull_request_target:
    types: [opened, reopened, synchronize, ready_for_review]
  issue_comment:
    types: [created]
  workflow_dispatch:
    inputs:
      pr_number:
        description: PR number to review
        required: true
        type: string

concurrency:
  group: >-
    pr-agent-${{
      github.event_name == 'workflow_dispatch' && format('dispatch-{0}', github.run_id) ||
      (
        github.event_name == 'issue_comment' && (
          github.event.comment.body != '/review' ||
          (
            github.event.sender.type == 'Bot' &&
            github.event.sender.login != 'github-actions[bot]' &&
            github.event.sender.login != 'ccs-reviewer[bot]'
          )
        )
      ) && format('skip-{0}', github.run_id) ||
      github.event.pull_request.number ||
      github.event.issue.number ||
      github.event.inputs.pr_number ||
      github.run_id
    }}
  cancel-in-progress: true

jobs:
  dispatch-review:
    name: Queue /review comment
    if: github.event_name == 'workflow_dispatch'
    runs-on: [self-hosted, cliproxy]
    permissions:
      issues: write

    steps:
      - name: Generate App Token
        id: app-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.CCS_REVIEWER_APP_ID }}
          private-key: ${{ secrets.CCS_REVIEWER_PRIVATE_KEY }}

      - name: Post /review command to the PR
        uses: actions/github-script@v7
        env:
          PR_NUMBER: ${{ github.event.inputs.pr_number }}
        with:
          github-token: ${{ steps.app-token.outputs.token }}
          script: |
            const issue_number = Number(process.env.PR_NUMBER);
            if (!Number.isInteger(issue_number) || issue_number <= 0) {
              core.setFailed(`Invalid PR number: ${process.env.PR_NUMBER}`);
              return;
            }

            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number,
              body: '/review',
            });

  pr-agent:
    name: PR-Agent Review
    if: >-
      github.event_name != 'workflow_dispatch' &&
      (
        github.event_name == 'pull_request_target' ||
        (
          github.event_name == 'issue_comment' &&
          github.event.issue.pull_request &&
          github.event.comment.body == '/review' &&
          (
            contains(fromJSON('["COLLABORATOR","MEMBER","OWNER"]'), github.event.comment.author_association) ||
            github.event.sender.login == 'github-actions[bot]' ||
            github.event.sender.login == 'ccs-reviewer[bot]'
          )
        )
      )
    runs-on: [self-hosted, cliproxy]
    timeout-minutes: 20
    permissions:
      issues: write
      pull-requests: write
      contents: read
    env:
      OPENAI_KEY: ${{ secrets.AI_REVIEW_API_KEY }}
      "OPENAI.API_BASE": ${{ vars.AI_REVIEW_BASE_URL }}
      "config.model": ${{ vars.AI_REVIEW_MODEL }}
      "github_action_config.auto_review": "true"
      "github_action_config.auto_describe": "false"
      "github_action_config.auto_improve": "false"

    steps:
      - name: Generate App Token
        id: pr-agent-app-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.CCS_REVIEWER_APP_ID }}
          private-key: ${{ secrets.CCS_REVIEWER_PRIVATE_KEY }}

      - name: Run PR-Agent
        uses: qodo-ai/pr-agent@v0.34
        env:
          GITHUB_TOKEN: ${{ steps.pr-agent-app-token.outputs.token }}
