Skip to content

Commit 2f9c182

Browse files
committed
feat: Gemini 기반 자동 코드 리뷰 기능 추가
1 parent b771cf8 commit 2f9c182

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
name: Gemini Code Review
2+
3+
on:
4+
push:
5+
branches: [ feature/gemini ]
6+
paths:
7+
- 'backend/**'
8+
pull_request:
9+
types: [opened, synchronize]
10+
11+
jobs:
12+
code-review:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
pull-requests: write
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v3
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Set up Node
24+
uses: actions/setup-node@v3
25+
26+
- name: Install GoogleGenerativeAI
27+
run: |
28+
npm install @google/generative-ai
29+
30+
# PR 이벤트에서의 변경사항 처리
31+
- name: Get git diff for PR
32+
if: github.event_name == 'pull_request'
33+
run: |
34+
git fetch origin "${{ github.event.pull_request.base.ref }}"
35+
git fetch origin "${{ github.event.pull_request.head.ref }}"
36+
git diff --unified=0 "origin/${{ github.event.pull_request.base.ref }}" > "diff.txt"
37+
echo "EVENT_TYPE=pull_request" >> $GITHUB_ENV
38+
39+
# Push 이벤트에서의 변경사항 처리
40+
- name: Get git diff for Push
41+
if: github.event_name == 'push'
42+
run: |
43+
git diff --unified=0 HEAD^ HEAD > "diff.txt"
44+
echo "EVENT_TYPE=push" >> $GITHUB_ENV
45+
46+
# Gemini를 사용한 코드 분석
47+
- name: Run Gemini-1.5-flash
48+
id: gemini_review
49+
uses: actions/github-script@v7
50+
with:
51+
script: |
52+
const fs = require("fs");
53+
const diff_output = fs.readFileSync("diff.txt",'utf8');
54+
55+
const { GoogleGenerativeAI } = require("@google/generative-ai");
56+
const genAI = new GoogleGenerativeAI("${{ secrets.GEMINI_API_KEY }}");
57+
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash"});
58+
59+
// PR과 Push에 따라 다른 프롬프트 사용
60+
let prompt;
61+
if (process.env.EVENT_TYPE === 'pull_request') {
62+
prompt = `Explain in korean. You are a senior software engineer and need to perform a code review based on the results of a given git diff. Review the changed code from different perspectives and let us know if there are any changes that need to be made. If you see any code that needs to be fixed in the result of the git diff, you need to calculate the exact line number by referring to the "@@ -0,0 +0,0 @@" part. The output format is \[{"path":"{ filepath }", "line": { line }, "text": { review comment }, "side": "RIGHT"}\] format must be respected.\n<git diff>${diff_output}</git diff>`;
63+
} else {
64+
prompt = `Explain in korean. You are a senior software engineer and need to perform a code review based on the results of a given git diff. Provide a detailed review of the code changes, focusing on code quality, readability, performance, and security. Format your response in Markdown with clear headings for each file reviewed.\n<git diff>${diff_output}</git diff>`;
65+
}
66+
67+
const result = await model.generateContent(prompt);
68+
const response = await result.response;
69+
const text = response.text();
70+
71+
fs.writeFileSync('review_result.txt', text);
72+
console.log('Review results saved!');
73+
74+
# PR 이벤트: 라인별 리뷰 코멘트 추가
75+
- name: Format and add PR review comments
76+
if: env.EVENT_TYPE == 'pull_request'
77+
id: store
78+
run: |
79+
COMMENT=$(sed '/^```/d' review_result.txt | jq -c .)
80+
echo "comment=$COMMENT" >> $GITHUB_OUTPUT
81+
82+
- name: Add Pull Request Review Comment
83+
if: env.EVENT_TYPE == 'pull_request'
84+
uses: nbaztec/[email protected]
85+
with:
86+
comments: ${{ steps.store.outputs.comment }}
87+
repo-token: ${{ secrets.GITHUB_TOKEN }}
88+
repo-token-user-login: 'github-actions[bot]'
89+
allow-repeats: false
90+
91+
# Push 이벤트: 액션 로그에 리뷰 결과 출력 및 아티팩트 업로드
92+
- name: Display review results in workflow log
93+
if: env.EVENT_TYPE == 'push'
94+
run: |
95+
echo "===== Gemini Code Review Results ====="
96+
cat review_result.txt
97+
echo "======================================"
98+
99+
- name: Upload review results as artifact
100+
if: env.EVENT_TYPE == 'push'
101+
uses: actions/upload-artifact@v4
102+
with:
103+
name: gemini-code-review
104+
path: review_result.txt

0 commit comments

Comments
 (0)