|
| 1 | +# codegraph PR review workflow — TEMPLATE |
| 2 | +# |
| 3 | +# This file lives OUTSIDE .github/workflows/ on purpose: GitHub rejects |
| 4 | +# pushes to .github/workflows/* from OAuth tokens that lack the `workflow` |
| 5 | +# scope. By keeping the template here, the repo can be cloned + pushed-to |
| 6 | +# with the default `repo` scope; activating the workflow is a one-time |
| 7 | +# manual step (see README → "PR review CI"). |
| 8 | +# |
| 9 | +# To activate: |
| 10 | +# gh auth refresh -h github.com -s workflow |
| 11 | +# cp .github/ci-templates/pr-review.workflow.yml .github/workflows/pr-review.yml |
| 12 | +# git add .github/workflows/pr-review.yml |
| 13 | +# git commit -m "ci: activate codegraph PR review" |
| 14 | +# git push |
| 15 | + |
| 16 | +name: codegraph PR review |
| 17 | + |
| 18 | +on: |
| 19 | + pull_request: |
| 20 | + branches: [main] |
| 21 | + types: [opened, synchronize, reopened] |
| 22 | + |
| 23 | +permissions: |
| 24 | + contents: read |
| 25 | + pull-requests: write |
| 26 | + |
| 27 | +concurrency: |
| 28 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} |
| 29 | + cancel-in-progress: true |
| 30 | + |
| 31 | +jobs: |
| 32 | + review: |
| 33 | + runs-on: ubuntu-latest |
| 34 | + timeout-minutes: 15 |
| 35 | + |
| 36 | + steps: |
| 37 | + - name: Checkout PR head |
| 38 | + uses: actions/checkout@v4 |
| 39 | + with: |
| 40 | + fetch-depth: 0 |
| 41 | + |
| 42 | + - name: Setup Python |
| 43 | + uses: actions/setup-python@v5 |
| 44 | + with: |
| 45 | + python-version: "3.10" |
| 46 | + cache: pip |
| 47 | + |
| 48 | + - name: Install codegraph (PR head) |
| 49 | + run: pip install -e . |
| 50 | + |
| 51 | + - name: Build baseline graph from main |
| 52 | + run: | |
| 53 | + set -euxo pipefail |
| 54 | + mkdir -p .baseline-checkout |
| 55 | + git worktree add .baseline-checkout origin/main |
| 56 | + ( |
| 57 | + cd .baseline-checkout |
| 58 | + pip install -e . --quiet |
| 59 | + codegraph build --no-incremental |
| 60 | + codegraph baseline save --output ../.codegraph/baseline.db |
| 61 | + ) |
| 62 | + if [ ! -f .codegraph/baseline.db ]; then |
| 63 | + echo "BASELINE_MISSING=1" >> "$GITHUB_ENV" |
| 64 | + echo "::warning::No baseline could be saved from main; running first-time review." |
| 65 | + fi |
| 66 | +
|
| 67 | + - name: Build PR head graph |
| 68 | + run: codegraph build --no-incremental |
| 69 | + |
| 70 | + - name: Run codegraph review |
| 71 | + run: | |
| 72 | + set -uo pipefail |
| 73 | + if [ -n "${BASELINE_MISSING:-}" ]; then |
| 74 | + cat > review.md <<'EOF' |
| 75 | + ## codegraph PR review |
| 76 | +
|
| 77 | + No baseline graph from `main` was available for this run, so a |
| 78 | + full diff review was skipped. The PR head was still parsed |
| 79 | + successfully — codegraph review will activate from the next PR. |
| 80 | + EOF |
| 81 | + exit 0 |
| 82 | + fi |
| 83 | + set +e |
| 84 | + codegraph review \ |
| 85 | + --format markdown \ |
| 86 | + --output review.md \ |
| 87 | + --fail-on high \ |
| 88 | + --baseline .codegraph/baseline.db |
| 89 | + rc=$? |
| 90 | + set -e |
| 91 | + if [ "$rc" -ne 0 ]; then |
| 92 | + echo "REVIEW_FAILED=1" >> "$GITHUB_ENV" |
| 93 | + fi |
| 94 | +
|
| 95 | + - name: Wrap review in comment template |
| 96 | + env: |
| 97 | + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 98 | + run: | |
| 99 | + set -e |
| 100 | + { |
| 101 | + echo "## codegraph PR review" |
| 102 | + echo |
| 103 | + echo "<details open>" |
| 104 | + echo "<summary><b>Diff vs main</b> · severity ≤ <code>high</code></summary>" |
| 105 | + echo |
| 106 | + cat review.md |
| 107 | + echo |
| 108 | + echo "</details>" |
| 109 | + echo |
| 110 | + echo "<sub>Triggered by codegraph CI · <a href=\"${RUN_URL}\">last run</a></sub>" |
| 111 | + } > comment.md |
| 112 | +
|
| 113 | + - name: Post or update PR comment |
| 114 | + uses: peter-evans/create-or-update-comment@v4 |
| 115 | + with: |
| 116 | + issue-number: ${{ github.event.pull_request.number }} |
| 117 | + body-file: comment.md |
| 118 | + edit-mode: replace |
| 119 | + comment-tag: codegraph-review |
| 120 | + |
| 121 | + - name: Upload review artifact |
| 122 | + if: always() |
| 123 | + uses: actions/upload-artifact@v4 |
| 124 | + with: |
| 125 | + name: codegraph-review |
| 126 | + path: | |
| 127 | + review.md |
| 128 | + comment.md |
| 129 | + .codegraph/graph.db |
| 130 | + retention-days: 7 |
| 131 | + |
| 132 | + - name: Fail if findings exceed threshold |
| 133 | + if: env.REVIEW_FAILED == '1' |
| 134 | + run: | |
| 135 | + echo "::error::codegraph review found high-or-critical findings. See PR comment for details." |
| 136 | + exit 1 |
0 commit comments