-
Notifications
You must be signed in to change notification settings - Fork 0
168 lines (129 loc) · 5.21 KB
/
gemini_review.yml
File metadata and controls
168 lines (129 loc) · 5.21 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
name: Gemini Automated Code Review
on:
pull_request:
types: [opened, synchronize, reopened]
branches: [main]
push:
branches:
- main
workflow_dispatch:
inputs:
pr_number:
description: "리뷰할 PR 번호 (수동 실행 시 필수)"
required: true
jobs:
code-review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
env:
GH_TOKEN: ${{ github.token }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install GitHub CLI
run: sudo apt-get install -y gh jq
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install GoogleGenerativeAI SDK
run: npm install @google/generative-ai@latest
- name: Get git diff for PR
if: github.event_name == 'pull_request'
run: |
echo "EVENT_TYPE=pull_request" >> $GITHUB_ENV
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
git fetch origin "${{ github.event.pull_request.base.ref }}"
git fetch origin "${{ github.event.pull_request.head.ref }}"
git diff --unified=0 "origin/${{ github.event.pull_request.base.ref }}" "origin/${{ github.event.pull_request.head.ref }}" > diff.txt
- name: Get git diff for manual PR review
if: github.event_name == 'workflow_dispatch'
run: |
PR_NUMBER="${{ github.event.inputs.pr_number }}"
echo "EVENT_TYPE=pull_request" >> $GITHUB_ENV
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
BASE=$(gh pr view $PR_NUMBER --json baseRefName -q .baseRefName)
HEAD=$(gh pr view $PR_NUMBER --json headRefName -q .headRefName)
git fetch origin $BASE
git fetch origin $HEAD
git diff --unified=0 origin/$BASE origin/$HEAD > diff.txt
- name: Detect PR for push event
if: github.event_name == 'push'
run: |
PR_NUMBER=$(gh pr list --state open --json number,headRefName \
--jq ".[] | select(.headRefName==\"${GITHUB_REF_NAME}\") | .number")
if [ -z "$PR_NUMBER" ]; then
echo "Push된 커밋과 연결된 PR을 찾을 수 없습니다."
exit 1
fi
echo "EVENT_TYPE=pull_request" >> $GITHUB_ENV
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
BASE=$(gh pr view $PR_NUMBER --json baseRefName -q .baseRefName)
HEAD=$(gh pr view $PR_NUMBER --json headRefName -q .headRefName)
git fetch origin $BASE
git fetch origin $HEAD
git diff --unified=0 origin/$BASE origin/$HEAD > diff.txt
- name: Run Gemini Review (File-based Chunking)
id: gemini_review
uses: actions/github-script@v7
with:
script: |
const fs = require("fs");
const { GoogleGenerativeAI } = require("@google/generative-ai");
const rawDiff = fs.readFileSync("diff.txt", "utf8");
const fileDiffs = rawDiff
.split("diff --git")
.slice(1)
.map(block => "diff --git" + block);
const tsDiffs = fileDiffs.filter(block =>
/a\/src\/.*\.(ts|tsx)/.test(block)
);
console.log("Filtered ts/tsx files:", tsDiffs.length);
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
let allComments = [];
for (let i = 0; i < tsDiffs.length; i++) {
const diffBlock = tsDiffs[i];
const prompt = `
당신은 시니어 프론트엔드 개발자입니다.
아래는 특정 파일의 diff입니다.
**src/ 내부의 .ts / .tsx 파일만 리뷰합니다.**
JSON 배열 형식만 출력하세요:
[
{
"path": "파일경로",
"line": 라인번호,
"text": "리뷰내용",
"side": "RIGHT"
}
]
JSON 외 텍스트 금지.
<file diff ${i + 1}/${tsDiffs.length}>
${diffBlock}
</file diff>
`;
console.log("Reviewing file " + (i + 1));
try {
const result = await model.generateContent(prompt);
const text = result.response.text();
const parsed = JSON.parse(text);
allComments.push(...parsed);
} catch (err) {
console.log("⚠️ Gemini error for file", i + 1);
console.log(err);
}
}
fs.writeFileSync("review_result.txt", JSON.stringify(allComments, null, 2));
console.log("Review saved!");
- name: Add PR Review Comments
uses: nbaztec/add-pr-review-comment@v1.0.7
with:
comments: ${{ steps.gemini_review.outputs.comment }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
repo-token-user-login: "github-actions[bot]"
allow-repeats: false