-
Notifications
You must be signed in to change notification settings - Fork 5
121 lines (103 loc) · 4.36 KB
/
review.yml
File metadata and controls
121 lines (103 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
name: AI PR Review
# The pull_request_target is an important feature. It is running on files from `main`.
# This is important to avoid changes which would execute code from the PR.
# See postmortem here https://github.com/Galvanized-Pukeko/gaunt-sloth-assistant/discussions/304
on:
pull_request_target:
types: [opened, synchronize, reopened]
branches:
- 'main'
jobs:
ai-code-review:
environment: review
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
issues: write
if: ${{ !startsWith(github.head_ref, 'dependabot') }}
steps:
- uses: actions/checkout@v6
- name: Use Node.js 24.x
uses: actions/setup-node@v6
with:
node-version: 24.x
cache: 'npm'
- name: Install dependencies
run: npm i @gaunt-sloth/review@0.1.0 @langchain/openai@1.2.0 -g && gaunt-sloth-review --version
- name: Extract issue number from PR title
id: extract_issue
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_TITLE=$(gh pr view ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --json title --jq '.title')
ISSUE_NUMBER=$(echo "$PR_TITLE" | grep -oE '^[0-9]{2,5} ' | tr -d ' ' || true)
echo "issue_number=$ISSUE_NUMBER" >> $GITHUB_OUTPUT
- name: Perform code review
id: review
continue-on-error: true
run: |
if [ -z "${{ steps.extract_issue.outputs.issue_number }}" ]; then
gaunt-sloth-review ${{ github.event.pull_request.number }}
# Prepend tip about linking issues when no issue number was found
{ echo 'ℹ️ **Tip:** To link this PR to an issue, start your PR title with the issue number followed by a space (e.g., "123 Fix authentication bug")'
echo ""
cat review.md
} > review.tmp && mv review.tmp review.md
else
gaunt-sloth-review ${{ github.event.pull_request.number }} ${{ steps.extract_issue.outputs.issue_number }}
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GSLOTH_IDENTITY_PROFILE: review
OPEN_ROUTER_API_KEY: ${{ secrets.OPEN_ROUTER_API_KEY }}
- name: Hide older AI review comments and post new one
uses: actions/github-script@v8
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs');
const review = fs.readFileSync('review.md', 'utf8');
// Define a unique identifier for AI review comments
const AI_REVIEW_IDENTIFIER = '<!-- AI-PR-REVIEW-BOT -->';
const PREVIOUS_AI_REVIEW_SUMMARY = '<summary>🤖 Previous AI Review (Click to expand)</summary>';
// Get all comments on this PR
const comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
});
// Find and hide older AI review comments
const aiComments = comments.data.filter(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes(AI_REVIEW_IDENTIFIER) &&
!comment.body.includes(PREVIOUS_AI_REVIEW_SUMMARY)
);
// Hide older comments by wrapping them in a collapsible section
for (const comment of aiComments) {
const hiddenBody = `<details>
${PREVIOUS_AI_REVIEW_SUMMARY}
${comment.body}
</details>`;
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
body: hiddenBody
});
}
// Create new comment with the identifier
const newCommentBody = `${AI_REVIEW_IDENTIFIER}
${review}`;
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: newCommentBody
});
- name: Check review step result
if: steps.review.outcome == 'failure'
run: |
echo "Code review step failed with exit code 1"
exit 1