|
| 1 | +name: PR Copilot Review |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, reopened, ready_for_review, synchronize] |
| 6 | + |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + copilot_pr_review: |
| 14 | + name: Copilot PR Review |
| 15 | + runs-on: ubuntu-latest |
| 16 | + env: |
| 17 | + REVIEW_TABLE_FALLBACK: | |
| 18 | + | File | Concern | Recommendation | Severity | |
| 19 | + | --- | --- | --- | --- | |
| 20 | + | All files | No actionable feedback generated | None | info | |
| 21 | + AZURE_OPENAI_BASE_URL: ${{ secrets.AZURE_OPENAI_BASE_URL }} |
| 22 | + AZURE_OPENAI_MODEL: ${{ secrets.AZURE_OPENAI_MODEL || 'gpt-5-codex' }} |
| 23 | + AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} |
| 24 | + steps: |
| 25 | + - name: Checkout code |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 0 |
| 29 | + |
| 30 | + - name: Set up Node.js |
| 31 | + uses: actions/setup-node@v4 |
| 32 | + with: |
| 33 | + node-version: '22' |
| 34 | + |
| 35 | + - name: Compute diff and changed files |
| 36 | + id: diff |
| 37 | + run: | |
| 38 | + BASE_SHA="${{ github.event.pull_request.base.sha }}" |
| 39 | + HEAD_SHA="${{ github.event.pull_request.head.sha }}" |
| 40 | +
|
| 41 | + git diff --name-only "$BASE_SHA" "$HEAD_SHA" > changed_files.txt |
| 42 | + git diff --unified=3 "$BASE_SHA" "$HEAD_SHA" > diff.patch |
| 43 | +
|
| 44 | + if [ ! -s changed_files.txt ]; then |
| 45 | + echo "no_changes=true" >> "$GITHUB_OUTPUT" |
| 46 | + else |
| 47 | + echo "no_changes=false" >> "$GITHUB_OUTPUT" |
| 48 | + fi |
| 49 | +
|
| 50 | + - name: Prepare fallback review |
| 51 | + if: steps.diff.outputs.no_changes == 'true' |
| 52 | + run: | |
| 53 | + printf '%s' "$REVIEW_TABLE_FALLBACK" > raw_review.md |
| 54 | + cp raw_review.md review_result.md |
| 55 | +
|
| 56 | + - name: Install Copilot CLI |
| 57 | + env: |
| 58 | + GITHUB_TOKEN: ${{ secrets.COPILOT_CLI_PAT }} |
| 59 | + if: steps.diff.outputs.no_changes == 'false' |
| 60 | + run: | |
| 61 | + npm install -g @github/copilot |
| 62 | + copilot --version |
| 63 | +
|
| 64 | + - name: Run Copilot code review |
| 65 | + env: |
| 66 | + GITHUB_TOKEN: ${{ secrets.COPILOT_CLI_PAT }} |
| 67 | + if: steps.diff.outputs.no_changes == 'false' |
| 68 | + run: | |
| 69 | + if [ -z "$AZURE_OPENAI_API_KEY" ]; then |
| 70 | + echo "AZURE_OPENAI_API_KEY secret is not configured." >&2 |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | +
|
| 74 | + HEADER="You are an expert software engineer performing a strict code review for the provided pull request diff." |
| 75 | + RULES="Rules:\n- Output ONLY a GitHub-flavored Markdown table with exactly these columns: File | Concern | Recommendation | Severity.\n- Every row must reference a real file path from the Changed files list.\n- Severity must be one of: info, minor, major, critical.\n- If no issues are found, return a single table row with 'All files' in the File column and 'No issues found' in the Concern column.\n- Cite line numbers from the diff using the format L<line>.\n- Do not wrap the table in backticks or add any prose before or after the table.\n- Focus on actionable feedback specific to the diff." |
| 76 | +
|
| 77 | + CHANGED_FILES_SECTION="Changed files:\n$(cat changed_files.txt)" |
| 78 | + DIFF_SECTION="Unified diff:\n$(cat diff.patch)" |
| 79 | +
|
| 80 | + export COPILOT_PROMPT="$HEADER\n\n$RULES\n\n$CHANGED_FILES_SECTION\n\n$DIFF_SECTION" |
| 81 | +
|
| 82 | + printf '%s\n' "$COPILOT_PROMPT" |
| 83 | +
|
| 84 | + copilot -p "$COPILOT_PROMPT" | tee copilot_raw.txt >/dev/null |
| 85 | +
|
| 86 | + printf '\n\nRaw Copilot Output:\n%s\n' "$(cat copilot_raw.txt)" |
| 87 | +
|
| 88 | + sed -E 's/\x1B\[[0-9;]*[A-Za-z]//g' copilot_raw.txt | tr -d '\r' > raw_review.md |
| 89 | +
|
| 90 | + if ! grep -q '|' raw_review.md; then |
| 91 | + printf '%s\n' "$REVIEW_TABLE_FALLBACK" > raw_review.md |
| 92 | + fi |
| 93 | +
|
| 94 | + - name: Normalize review table with Azure OpenAI |
| 95 | + if: steps.diff.outputs.no_changes == 'false' |
| 96 | + run: | |
| 97 | + python3 -m pip install --quiet --upgrade pip 'openai>=1.45.0' |
| 98 | + python3 scripts/normalize_review_result.py |
| 99 | +
|
| 100 | + - name: Post review as PR comment |
| 101 | + env: |
| 102 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 103 | + run: | |
| 104 | + if [ -f review_result.md ]; then |
| 105 | + BODY_FILE=review_result.md |
| 106 | + elif [ -f raw_review.md ]; then |
| 107 | + BODY_FILE=raw_review.md |
| 108 | + else |
| 109 | + printf '%s' "$REVIEW_TABLE_FALLBACK" > review_result.md |
| 110 | + BODY_FILE=review_result.md |
| 111 | + fi |
| 112 | +
|
| 113 | + # Append attribution footer to the comment body |
| 114 | + printf '\n\nReviewed by GitHub Copilot CLI\n' >> "$BODY_FILE" |
| 115 | +
|
| 116 | + gh pr comment ${{ github.event.pull_request.number }} --body-file "$BODY_FILE" |
| 117 | +
|
| 118 | + - name: Upload Copilot raw output |
| 119 | + if: steps.diff.outputs.no_changes == 'false' |
| 120 | + uses: actions/upload-artifact@v4 |
| 121 | + with: |
| 122 | + name: copilot-review-logs |
| 123 | + path: | |
| 124 | + copilot_raw.txt |
| 125 | + raw_review.md |
| 126 | + review_result.md |
| 127 | + diff.patch |
| 128 | + changed_files.txt |
0 commit comments