코드 리뷰 Github Action 테스트를 위한 일부 코드 리팩토링 #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Code Review with GPT | |
| on: | |
| pull_request: | |
| types: [opened, synchronize] | |
| workflow_dispatch: | |
| jobs: | |
| code_review: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.x' | |
| - name: Install dependencies | |
| run: | | |
| pip install openai requests | |
| - name: Generate diff for code review | |
| id: diff | |
| run: | | |
| git fetch origin ${{ github.event.pull_request.base.ref }} --depth=1 | |
| git diff origin/${{ github.event.pull_request.base.ref }} ${{ github.sha }} > pr_diff.txt | |
| shell: bash | |
| - name: Perform Code Review with GPT | |
| id: gpt_review | |
| run: | | |
| python - <<EOF | |
| import openai | |
| # OpenAI API Key 및 모델명 환경 변수로 설정 | |
| openai.api_key = "${{ secrets.OPENAI_API_KEY }}" | |
| # 시스템 프롬프트 및 모델명 가져오기 | |
| system_prompt = """${{ secrets.SYSTEM_PROMPT }}""" | |
| model_name = "${{ secrets.OPENAI_MODEL }}" | |
| with open('pr_diff.txt', 'r') as file: | |
| diff_content = file.read() | |
| # GPT 모델 호출 | |
| response = openai.chat.completions.create( | |
| model=model_name, | |
| messages=[ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": f"Here is the code diff:\n{diff_content}"} | |
| ] | |
| ) | |
| # GPT 리뷰 결과 추출 | |
| review_feedback = response.choices[0].message.content | |
| # 리뷰 결과를 gpt_review.txt 파일에 저장 | |
| with open('gpt_review.txt', 'w') as output_file: | |
| output_file.write(review_feedback) | |
| EOF | |
| - name: Post review comment on PR | |
| run: | | |
| REVIEW_COMMENT=$(cat gpt_review.txt) | |
| echo "Review Comment: $REVIEW_COMMENT" | |
| REVIEW_COMMENT_ESCAPED=$(echo "${REVIEW_COMMENT}" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))') | |
| curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
| -d "{\"body\": ${REVIEW_COMMENT_ESCAPED}}" \ | |
| "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" |