-
Notifications
You must be signed in to change notification settings - Fork 0
144 lines (112 loc) · 4.35 KB
/
gemini_review.yml
File metadata and controls
144 lines (112 loc) · 4.35 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
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 }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install GitHub CLI
run: sudo apt-get install -y gh
- 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 }}" > 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-2.5-flash
id: gemini_review
uses: actions/github-script@v7
with:
script: |
const fs = require("fs");
const diff = fs.readFileSync("diff.txt", "utf8");
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI("${{ secrets.GEMINI_API_KEY }}");
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
const prompt = `
당신은 시니어 프론트엔드 개발자입니다.
이 diff를 기반으로 PR 코드 리뷰를 수행하세요.
**src/ 내부의 .ts, .tsx 파일만 리뷰합니다.**
다음 JSON 배열만 출력하세요:
[
{
"path": "파일경로",
"line": 라인번호,
"text": "리뷰코멘트",
"side": "RIGHT"
}
]
JSON 외 텍스트 출력 금지.
<git diff>
${diff}
</git diff>
`;
const result = await model.generateContent(prompt);
const text = result.response.text();
fs.writeFileSync("review_result.txt", text);
- name: Parse review result
id: store
run: |
COMMENT=$(cat review_result.txt | jq -c .)
echo "comment=$COMMENT" >> $GITHUB_OUTPUT
- name: Add review comments to PR
uses: nbaztec/add-pr-review-comment@v1.0.7
with:
comments: ${{ steps.store.outputs.comment }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
repo-token-user-login: "github-actions[bot]"
allow-repeats: false