Skip to content

Commit

Permalink
Add information about the Github CI action for the automatic code rev…
Browse files Browse the repository at this point in the history
…iew.
  • Loading branch information
Thanasis Politis committed Mar 25, 2023
1 parent 2eab395 commit c525762
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 27 deletions.
54 changes: 27 additions & 27 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Checkout code
uses: actions/checkout@v2

- name: Install Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install Python
uses: actions/setup-python@v2
with:
python-version: "3.9"

- name: Install dependencies
run: pip install requests
- 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
- 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)
# 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)
# 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")
# 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')
# 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
# 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
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,61 @@ Miracle Mill ChatGPT Assignment

## Service Weaver `hello` example
> https://github.com/ServiceWeaver/weaver/tree/main/examples/hello
```
$ weaver generate .
$ go run .
```

## ChatGPT Code Review via Github Actions

Setup your ChatGPT API key in the project's secrets page and access it within the script like this: `CHATGPT_API_KEY: ${{ secrets.CHATGPT_API_KEY }}`

```
name: 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
```

0 comments on commit c525762

Please sign in to comment.