|
| 1 | +name: Code Review with GPT |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + code_review: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v3 |
| 15 | + |
| 16 | + - name: Set up Python |
| 17 | + uses: actions/setup-python@v4 |
| 18 | + with: |
| 19 | + python-version: '3.x' |
| 20 | + |
| 21 | + - name: Install dependencies |
| 22 | + run: | |
| 23 | + pip install openai requests |
| 24 | +
|
| 25 | + - name: Generate diff for code review |
| 26 | + id: diff |
| 27 | + run: | |
| 28 | + git fetch origin ${{ github.event.pull_request.base.ref }} --depth=1 |
| 29 | + git diff origin/${{ github.event.pull_request.base.ref }} ${{ github.sha }} > pr_diff.txt |
| 30 | + shell: bash |
| 31 | + |
| 32 | + - name: Perform Code Review with GPT |
| 33 | + id: gpt_review |
| 34 | + run: | |
| 35 | + python - <<EOF |
| 36 | + import openai |
| 37 | +
|
| 38 | + # OpenAI API Key 및 모델명 환경 변수로 설정 |
| 39 | + openai.api_key = "${{ secrets.OPENAI_API_KEY }}" |
| 40 | +
|
| 41 | + # 시스템 프롬프트 및 모델명 가져오기 |
| 42 | + system_prompt = """${{ secrets.SYSTEM_PROMPT }}""" |
| 43 | + model_name = "${{ secrets.OPENAI_MODEL }}" |
| 44 | +
|
| 45 | + with open('pr_diff.txt', 'r') as file: |
| 46 | + diff_content = file.read() |
| 47 | +
|
| 48 | + # GPT 모델 호출 |
| 49 | + response = openai.ChatCompletion.create( |
| 50 | + model=model_name, |
| 51 | + messages=[ |
| 52 | + {"role": "system", "content": system_prompt}, |
| 53 | + {"role": "user", "content": f"Here is the code diff:\n{diff_content}"} |
| 54 | + ] |
| 55 | + ) |
| 56 | +
|
| 57 | + # GPT 리뷰 결과 출력 |
| 58 | + review_feedback = response['choices'][0]['message']['content'] |
| 59 | + print(review_feedback) |
| 60 | + EOF |
| 61 | +
|
| 62 | + - name: Post review comment on PR |
| 63 | + run: | |
| 64 | + REVIEW_COMMENT=$(python -c 'import json; print(open("gpt_review.txt").read())') |
| 65 | + curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 66 | + -d "{\"body\": \"${REVIEW_COMMENT}\"}" \ |
| 67 | + "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" |
0 commit comments