generated from BlossomLabs/web3-turbo-template
-
Notifications
You must be signed in to change notification settings - Fork 4
126 lines (113 loc) · 5.06 KB
/
ai-code-review.yml
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
name: "Code Review by Gemini AI"
on:
issue_comment:
types:
- created
- updated
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
issues: read
steps:
- uses: actions/checkout@v3
- name: Only PR comments
if: ${{ !github.event.issue.pull_request || !contains(github.event.comment.body,'!ai') }}
run: exit 1
- name: Put a reaction to the comment
run: gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_ID: ${{ github.event.comment.node_id }}
- name: Fetch filtered PR diff (only .ts, .tsx, .sol files)
id: pr-diff
uses: actions/github-script@v6
with:
script: |
const pr_number = context.payload.issue.number;
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr_number
});
const { data: diff } = await github.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
headers: {
accept: 'application/vnd.github.v3.diff'
},
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr_number
});
// Initialize variables to store filtered diff and a flag for capturing lines
let filteredDiff = '';
let capture = false;
// Iterate over each line in the diff
diff.split('\n').forEach(line => {
// Check if the line is a diff line for the relevant file types
if (line.startsWith('diff --git')) {
capture = line.endsWith('.ts') || line.endsWith('.tsx') || line.endsWith('.sol');
}
// If capturing, append the current line to the filtered diff
if (capture) {
filteredDiff += line + '\n';
}
});
// Get the head commit SHA
const headSha = pr.data.head.sha;
// Set outputs for the filtered diff and head SHA
core.setOutput("diff", filteredDiff);
core.setOutput("headSha", headSha);
- name: Log filtered diff to file
id: log-diff
continue-on-error: true
run: |
echo "${{ steps.pr-diff.outputs.diff }}"
- uses: rubensflinco/[email protected]
name: "Code Review by Gemini AI"
id: review
continue-on-error: true
with:
gemini_api_key: ${{ secrets.GEMINI_API_KEY }}
github_token: ${{ secrets.GITHUB_TOKEN }}
github_repository: ${{ github.repository }}
github_pull_request_number: ${{ github.event.issue.number }}
git_commit_hash: ${{ steps.pr-diff.outputs.headSha }}
model: "gemini-1.5-pro-latest"
pull_request_diff: |-
${{ steps.pr-diff.outputs.diff }}
pull_request_chunk_size: "5000"
extra_prompt: |-
Make a pull request review in regards of the added and removed code.
Start with a summary of the changes and then list detail the review and suggestions.
Focus on code quality, security, performance, best practices and potential mistakes.
Never ask to provide more code and only review the code you received even if its partial.
When a line start with a minus (`-`) it means that the line was removed, when it starts with plus (`+`) it means that the line was added.
log_level: "DEBUG"
- name: Get current job ID
id: get-job-id
run: |
job_uri=$(gh run --repo ${{ github.repository }} view ${{ github.run_id }} --json jobs --jq '.jobs[] | select(.name == "${{ github.job }}") | .url')
echo "job_uri=$job_uri" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Add comment with log
if: steps.review.outcome == 'failure'
uses: actions/github-script@v6
with:
script: |
const runUrl = `${{ steps.get-job-id.outputs.job_uri }}#step:8:1`;
const commentBody = `Gemini Code Review encountered an error. [View the run here](${runUrl}).\n\nSee **Code Review by Gemini AI** step as it may still contains some review before failing.`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
- name: Add thumbs down reaction if something went wrong
if: failure()
run: gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_DOWN}){reaction{content}subject{id}}}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_ID: ${{ github.event.comment.node_id }}