name: "Gito: React to GitHub comment"

on:
  issue_comment:
    types: [created]

permissions:
  contents: write  # to make PR
  issues: write
  pull-requests: write
  # read: to download the code review artifact
  # write: to trigger other actions
  actions: write

jobs:
  process-comment:
    if: |
      github.event.issue.pull_request &&
      (
        github.event.comment.author_association == 'OWNER' ||
        github.event.comment.author_association == 'MEMBER' ||
        github.event.comment.author_association == 'COLLABORATOR'
      ) &&
      (
        startsWith(github.event.comment.body, '/') ||
        startsWith(github.event.comment.body, 'gito') ||
        startsWith(github.event.comment.body, 'ai') ||
        startsWith(github.event.comment.body, 'bot') ||
        contains(github.event.comment.body, '@gito') ||
        contains(github.event.comment.body, '@ai') ||
        contains(github.event.comment.body, '@bot')
      )
    runs-on: ubuntu-latest

    steps:
    - name: Get PR details
      id: pr
      uses: actions/github-script@v7
      with:
        script: |
          const pr = await github.rest.pulls.get({
            owner: context.repo.owner,
            repo: context.repo.repo,
            pull_number: context.issue.number
          });
          return {
            head_ref: pr.data.head.ref,
            head_sha: pr.data.head.sha,
            base_ref: pr.data.base.ref
          };

    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        {% raw -%}
        repository: ${{ github.repository }}
        token: ${{ secrets.GITHUB_TOKEN }}
        ref: ${{ fromJson(steps.pr.outputs.result).head_ref }}
        fetch-depth: 0
        {%- endraw %}

      {%- include("github_workflows/components/installs.j2") %}

    - name: Run Gito react
      env:
        # LLM config is needed only if answer_github_comments = true in .gito/config.toml
        # Otherwise, use LLM_API_TYPE: none
        {%- include("github_workflows/components/env-vars.j2") %}
      run: |
        {% raw %}gito react-to-comment ${{ github.event.comment.id }}{%- endraw %}
