Repository navigation
344 lines (288 loc) Β· 13.6 KB
/
Copy pathcoderabbit-ai-review.yml
File metadata and controls
344 lines (288 loc) Β· 13.6 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
name: CodeRabbit Review
permissions:
contents: read
issues: read
pull-requests: write
actions: write
on:
pull_request:
types: [opened, edited, synchronize]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to add banner to'
required: true
type: string
action:
description: 'Action to perform'
required: false
type: choice
options:
- banner
- review
default: 'banner'
concurrency:
group: >-
${{ github.repository }}-${{ github.event.pull_request.number || github.event.inputs.pr_number || github.head_ref || github.sha }}-${{ github.workflow }}-coderabbit-review
cancel-in-progress: true
jobs:
banner:
name: Add CodeRabbit Review Banner
runs-on: self-hosted
if: >-
(github.event_name == 'pull_request' && github.event.action == 'opened') ||
(github.event_name == 'workflow_dispatch' && (github.event.inputs.action == 'banner' || github.event.inputs.action == ''))
env:
COMMENT_PREFIX: '<img src="https://avatars.githubusercontent.com/in/347564?s=41" alt="CodeRabbit" width="20" height="20"> CodeRabbit'
BANNER_MARKER: '<!-- coderabbit-review-banner -->'
steps:
- name: Get PR number
id: pr_number
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
let prNumber;
if (context.eventName === 'workflow_dispatch') {
prNumber = context.payload.inputs.pr_number;
} else {
prNumber = context.payload.pull_request?.number;
}
if (!prNumber) {
core.setFailed('PR number is required');
return;
}
prNumber = parseInt(prNumber, 10);
core.setOutput('pr_number', prNumber.toString());
core.info(`Processing PR #${prNumber}`);
- name: Check for existing banner
id: check_banner
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = parseInt('${{ steps.pr_number.outputs.pr_number }}', 10);
if (!prNumber || isNaN(prNumber)) {
core.setOutput('exists', 'false');
return;
}
const marker = process.env.BANNER_MARKER || '<!-- coderabbit-review-banner -->';
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100
});
const bannerExists = comments.some(comment =>
comment.body?.includes(marker)
);
core.setOutput('exists', bannerExists ? 'true' : 'false');
core.info(`Banner exists: ${bannerExists}`);
- name: Add review banner
if: ${{ steps.check_banner.outputs.exists == 'false' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = parseInt('${{ steps.pr_number.outputs.pr_number }}', 10);
if (!prNumber || isNaN(prNumber)) {
core.info('PR number unavailable; skipping banner comment.');
return;
}
const marker = process.env.BANNER_MARKER || '<!-- coderabbit-review-banner -->';
const prefix = process.env.COMMENT_PREFIX || '';
const body = `${prefix}${marker}
### π€ CodeRabbit AI Review Available
To request a code review from CodeRabbit AI, add \`[coderabbit-ai-review]\` to your PR title.
CodeRabbit will analyze your code and provide feedback on:
- Logic and correctness
- Security issues
- Performance optimizations
- Code quality and best practices
- Error handling
- Maintainability
**Note:** Reviews are only performed when \`[coderabbit-ai-review]\` is present in the PR title.
`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body
});
core.info(`Banner added to PR #${prNumber}`);
review:
name: CodeRabbit AI Review
runs-on: self-hosted
if: >-
github.event_name == 'pull_request' &&
contains(github.event.pull_request.title, '[coderabbit-ai-review]')
env:
COMMENT_PREFIX: '<img src="https://avatars.githubusercontent.com/in/347564?s=41" alt="CodeRabbit" width="20" height="20"> CodeRabbit'
CODERABBIT_SETUP_REMINDER_MARKER: '<!-- coderabbit-review-setup-reminder -->'
steps:
- name: Detect CodeRabbit token
id: coderabbit_token
env:
CODERABBIT_TOKEN: ${{ secrets.CODERABBIT_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
missing_tokens=()
if [ -z "${CODERABBIT_TOKEN}" ]; then
missing_tokens+=("CODERABBIT_TOKEN")
fi
if [ -z "${OPENAI_API_KEY}" ]; then
missing_tokens+=("OPENAI_API_KEY")
fi
if [ ${#missing_tokens[@]} -gt 0 ]; then
echo "Missing required secrets: ${missing_tokens[*]}"
echo "available=false" >> "$GITHUB_OUTPUT"
else
echo "available=true" >> "$GITHUB_OUTPUT"
fi
- name: Notify missing CodeRabbit token
if: steps.coderabbit_token.outputs.available == 'false'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = ${{ github.event.pull_request.number }};
if (!prNumber || isNaN(prNumber)) {
core.info('PR number unavailable; skipping reminder comment.');
return;
}
const marker = process.env.CODERABBIT_SETUP_REMINDER_MARKER || '<!-- coderabbit-review-setup-reminder -->';
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100
});
if (comments.some(comment => comment.body?.includes(marker))) {
return;
}
const body = `${process.env.COMMENT_PREFIX || ''}${marker}
### CodeRabbit Review Setup Required
The CodeRabbit review workflow is disabled because required secrets are not configured: \`CODERABBIT_TOKEN\` and/or \`OPENAI_API_KEY\`.
Please follow the setup guide: ${process.env.CODERABBIT_REVIEW_DOC_URL || 'https://wiki.gluzdov.com/doc/coderabbit-review-workflow-setup-6CqNB5aHtY'}
`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body
});
- name: Checkout repository
if: steps.coderabbit_token.outputs.available == 'true'
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check if review already exists for commit SHA
if: steps.coderabbit_token.outputs.available == 'true'
id: check_review
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = ${{ github.event.pull_request.number }};
const commitSha = '${{ github.event.pull_request.head.sha }}';
if (!prNumber || !commitSha) {
core.info('PR number or commit SHA unavailable; proceeding with review.');
core.setOutput('review_exists', 'false');
return;
}
// Get all review comments for this PR
const reviewComments = await github.paginate(github.rest.pulls.listReviewComments, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
per_page: 100
});
// Check if any review comment contains a marker for this commit SHA
const marker = `<!-- coderabbit-review-commit:${commitSha} -->`;
const hasReviewForCommit = reviewComments.some(comment =>
comment.body?.includes(marker) ||
(comment.user?.login === 'github-actions[bot]' &&
comment.body?.includes('CodeRabbit') &&
comment.created_at &&
new Date(comment.created_at) > new Date(Date.now() - 3600000))
);
// Also check regular PR comments for the marker
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100
});
const hasCommentMarker = comments.some(comment =>
comment.body?.includes(marker) ||
(comment.user?.login === 'github-actions[bot]' &&
comment.body?.includes('CodeRabbit') &&
comment.body?.includes(commitSha.substring(0, 7)))
);
if (hasReviewForCommit || hasCommentMarker) {
core.info(`Review already exists for commit ${commitSha.substring(0, 7)}. Skipping.`);
core.setOutput('review_exists', 'true');
} else {
core.info(`No review found for commit ${commitSha.substring(0, 7)}. Proceeding with review.`);
core.setOutput('review_exists', 'false');
}
- name: Run CodeRabbit AI Reviewer
if: steps.coderabbit_token.outputs.available == 'true' && steps.check_review.outputs.review_exists == 'false'
uses: coderabbitai/ai-pr-reviewer@latest # NOSONAR
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
with:
debug: ${{ vars.PR_REVIEW_DEBUG || 'false' }}
review_simple_changes: ${{ vars.PR_REVIEW_SIMPLE_CHANGES || 'false' }}
review_comment_lgtm: ${{ vars.PR_REVIEW_COMMENT_LGTM || 'false' }}
summarize: ''
system_message: |
You are `@coderabbitai` (aka `github-actions[bot]`), a language model trained by OpenAI. Your purpose is to act as a highly experienced software engineer and provide a thorough review of the code hunks and suggest code snippets to improve key areas such as:
- Logic
- Security
- Performance
- Data races
- Consistency
- Error handling
- Maintainability
- Modularity
- Complexity
- Optimization
- Best practices: DRY, SOLID, KISS
Do not comment on minor code style issues, missing comments/documentation. Identify and resolve significant concerns to improve overall code quality while deliberately disregarding minor issues.
CRITICAL: Only comment on actual problems, bugs, security issues, or code quality issues. Do NOT comment on things that are correct or well-implemented. Skip praise, confirmations, or positive feedback. Focus exclusively on issues that need to be fixed. If there are no issues, do not create any comments.
CRITICAL: DO NOT create summary comments, high-level summaries, file summaries, or any general comments. ONLY create inline review comments on specific lines of code with actual problems. Never create issue comments or PR comments - only inline code review comments.
MANDATORY FORMAT FOR EACH INLINE COMMENT:
Every inline review comment MUST follow this exact structure:
**[CATEGORY_EMOJI] [CATEGORY]: [Brief issue title]**
[Detailed description of the problem, including context and potential impact]
**Current Code:**
```[language]
[Show the exact problematic code snippet here]
```
**Suggestion:**
```[language]
[Show the improved code here]
```
**Why this matters:** [Explain the impact, risk, or benefit of this change]
CATEGORY_EMOJI and CATEGORY should be one of:
- π BUG - Logic errors, incorrect behavior, missing edge cases
- π SECURITY - Security vulnerabilities, unsafe practices, data exposure
- β‘ PERFORMANCE - Performance bottlenecks, inefficient algorithms, resource waste
- ποΈ ARCHITECTURE - Design issues, coupling problems, architectural concerns
- π§Ή CODE_QUALITY - Maintainability issues, code smells, technical debt
- π BEST_PRACTICE - Violations of best practices, conventions, patterns
Example format:
**π BUG: Missing null check could cause runtime error**
The code filters data without checking if the array is null or undefined, which could cause a runtime error if data is null.
**Current Code:**
```typescript
const result = data.filter(x => x.value > 0);
```
**Suggestion:**
```typescript
const result = data?.filter(x => x?.value > 0) ?? [];
```
**Why this matters:** Without null checks, the application will crash if data is null or undefined, leading to poor user experience and potential data loss.
IMPORTANT: Entire response must be in the language with ISO code: en-US