diff --git a/.github/workflows/code-review.yaml b/.github/workflows/code-review.yaml new file mode 100644 index 00000000..55211cc8 --- /dev/null +++ b/.github/workflows/code-review.yaml @@ -0,0 +1,66 @@ +name: Code Review + +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + +permissions: + contents: read + pull-requests: write + +jobs: + explain-change: + if: ${{ !github.event.pull_request.draft }} + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Install script deps + run: | + python -m pip install --upgrade pip + pip install git+https://github.com/sbekkerm/llm-code-review.git@main + + - name: Gather PR diff + id: diff + run: | + BASE_SHA="${{ github.event.pull_request.base.sha }}" + HEAD_SHA="${{ github.event.pull_request.head.sha }}" + git diff --unified=0 "$BASE_SHA" "$HEAD_SHA" > pr.diff || true + echo "diff_path=pr.diff" >> "$GITHUB_OUTPUT" + + - name: Run LLM review + id: explain + env: + LLM_API_URL: ${{ secrets.LLM_API_URL }} + LLM_API_KEY: ${{ secrets.LLM_API_KEY }} + LLM_MODEL_NAME: ${{ secrets.LLM_MODEL_NAME }} + LLM_TIMEOUT_SECONDS: 60 + LLM_MAX_TOKENS: 700 + LLM_MAX_CHARS_PER_CHUNK: 12000 + run: | + llm-code-review --diff "${{ steps.diff.outputs.diff_path }}" --out review.md + echo "review<> $GITHUB_OUTPUT + cat review.md >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + - name: Comment on PR with review + uses: actions/github-script@v7 + with: + script: | + const fs = require('fs'); + const body = fs.readFileSync('review.md', 'utf8'); + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body + })