-
Notifications
You must be signed in to change notification settings - Fork 3
101 lines (79 loc) · 5.04 KB
/
ai-pr-summary.yml
File metadata and controls
101 lines (79 loc) · 5.04 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
name: Gemini PR Summary and Explanation
on:
pull_request:
branches:
- dev
types: [opened, synchronize] # PR이 열리거나 업데이트될 때 트리거됨
jobs:
pr-summary:
runs-on: ubuntu-latest # Ubuntu 최신 버전에서 실행
permissions:
contents: read # 리포지토리의 콘텐츠를 읽을 수 있음
pull-requests: write # PR에 댓글을 작성할 수 있음
steps:
- name: Checkout Repository
uses: actions/checkout@v3 # 리포지토리 코드 체크아웃
with:
fetch-depth: 0 # 전체 히스토리를 가져옵니다 (전체 히스토리 필요)
- name: Set up Node.js
uses: actions/setup-node@v3 # Node.js 환경 설정
- name: Install Gemini AI Dependencies
run: npm install @google/generative-ai # Gemini AI 종속성 설치
# PR 이벤트의 변경사항(diff) 추출
- name: Fetch and Generate Git Diff for PR
run: |
git fetch origin "${{ github.event.pull_request.base.ref }}" # 기본 브랜치에서 변경사항 가져오기
git fetch origin "${{ github.event.pull_request.head.ref }}" # PR 브랜치에서 변경사항 가져오기
git diff --unified=0 "origin/${{ github.event.pull_request.base.ref }}" > diff.txt # 변경된 diff 파일 생성
# Gemini API를 호출하여 PR 제목, 설명, diff를 포함한 프롬프트로 요약 및 설명 생성
- name: Call Gemini API to Generate PR Summary
id: gemini_review
uses: actions/github-script@v7 # GitHub API를 사용하여 스크립트 실행
with:
script: |
const fs = require("fs");
const diff_output = fs.readFileSync("diff.txt", 'utf8'); // diff.txt 파일 읽기
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI("${{ secrets.GEMINI_API_KEY }}"); // Gemini API 키로 생성자 인스턴스 생성
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); // Gemini 모델 설정
let prompt = `
다음은 깃허브 PR 에 올라온 수정된 코드들 입니다.
Git diff를 분석하고, 각 변경 사항에 대해 파일명, 수정 내용, 역할을 아래와 같은 형식을 반드시 유지하여 요약해 주세요.
가장 첫 부분에는 이 PR을 최종 요약하여 알려주세요. 최종 요약 부분의 제목은 "### PR 요약 :" 으로 해주세요.
PR 요약의 모든 문장은 끝나면 <br>으로 띄어주세요. PR 요약이 끝난 후에는 <hr>을 하나 넣어주세요.
(각 파일에 대한 요약은 반드시 코드 블럭으로 들어가지 않게 해주세요)
### < 경로를 제외한 파일명 > (이 부분의 < >는 유지되게 해주세요)
- **역할 :**
(이 변경 사항의 역할 예: 기능 추가, 버그 수정, 리펙토링 등)
- **수정 내용 :**
(수정된 내용의 간략한 설명. 모든 문장은 끝나면 <br>으로 띄어주세요. 이 부분에서는 줄을 바꾸고 들여쓰기가 유지되도록 띄어주세요.)
각 파일 내용이 끝나면 <br>으로 한 줄을 띄워주세요.
<git diff>${diff_output}</git diff>`; // diff 내용과 함께 요약 프롬프트 작성
try {
const result = await model.generateContent(prompt); // Gemini API 호출하여 요약 생성
const response = await result.response;
const text = await response.text();
if (!text || text.trim().length === 0) {
console.log("❌ Gemini API 응답이 비어 있습니다.");
throw new Error("Gemini API 응답이 비어 있습니다.");
}
fs.writeFileSync('review_result.txt', text); // 응답을 파일로 저장
console.log("✅ Gemini API 응답을 review_result.txt 파일에 저장했습니다.");
} catch (error) {
console.error("❌ Gemini API 요청 중 오류 발생:", error); // 오류 발생 시 에러 메시지 출력
process.exit(1); // 워크플로 종료
}
- name: Format PR Review Summary for Comment
id: store
run: |
COMMENT_STRING=$(cat review_result.txt) # 생성된 요약을 파일에서 읽어옴
# 줄바꿈 처리
echo "comment<<EOF" >> $GITHUB_OUTPUT
echo "# AI PR 요약" >> $GITHUB_OUTPUT
echo -e "$COMMENT_STRING" >> $GITHUB_OUTPUT # PR 요약을 댓글 형식으로 준비
echo "EOF" >> $GITHUB_OUTPUT
- name: Post PR Summary Comment
uses: mshick/add-pr-comment@v2 # PR에 요약을 깃허브 액션 봇 댓글로 추가
with:
message: ${{ steps.store.outputs.comment }} # 준비된 댓글 메시지 사용
repo-token: ${{ secrets.GITHUB_TOKEN }} # GitHub 토큰 사용