From 2eab395a72d91870d72b3f307d12f1381f9c9540 Mon Sep 17 00:00:00 2001 From: Thanasis Politis Date: Sat, 25 Mar 2023 22:03:38 +0200 Subject: [PATCH] Create .github/workflows/main.yml --- .github/workflows/main.yml | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..801ba17 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,46 @@ +name: ChatGPT Code Review + +on: + pull_request: + types: [opened, edited, reopened, synchronize] + +jobs: + code_review: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Install Python + uses: actions/setup-python@v2 + with: + python-version: '3.9' + + - name: Install dependencies + run: pip install requests + + - name: Run code review + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CHATGPT_API_KEY: ${{ secrets.CHATGPT_API_KEY }} + run: | + set -e + + # Get the list of files changed in the pull request + files=$(git diff --name-only HEAD~1) + + # Loop through each file and get its contents + for file in $files; do + contents=$(git show HEAD~1:$file) + + # Send the contents to ChatGPT for review + response=$(curl -s -X POST -H "Authorization: Bearer $CHATGPT_API_KEY" -H "Content-Type: application/json" -d "{\"text\": \"$contents\"}" "https://api.openai.com/v1/engines/davinci-codex/completions?prompt=Please review the following code:&max_tokens=100&n=1") + + # Parse the response to get the review + review=$(echo $response | jq -r '.choices[].text') + + # Post the review as a comment on the pull request + curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" -H "Content-Type: application/json" -d "{\"body\": \"$review\"}" "https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" + done