-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaction.yml
140 lines (131 loc) · 4.61 KB
/
action.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
name: Code Review Github Action
author: Frank Chen (@fxchen)
description: 'Improve your pull requests and code base with AI-assisted code reviews'
inputs:
github-token:
description: 'The token used to authenticate with the GitHub API'
required: false
default: ${{ github.token }}
api-to-use:
description: 'The API to use for code review (openai, claude)'
required: false
default: 'openai'
openai-key:
description: 'The OpenAI API key'
required: true
anthropic-key:
description: 'The Anthropic (Claude) API key'
required: false
model:
description: 'The OpenAI or Anthropic language model to use for code review'
required: false
default: 'gpt-3.5-turbo-16k'
persona:
description: 'The persona to use in the prompt (developer, kent_beck, marc_benioff, yoda)'
required: false
default: 'kent_beck'
style:
description: 'The style of output to use (concise, zen)'
required: false
default: 'concise'
include-full-files:
description: 'Whether to include full files in addition to the diff (true or false)'
required: false
default: 'false'
post-if-error:
description: 'Whether to post a comment if there was an error'
required: false
default: 'false'
exclude-files:
description: 'A list of flags to exclude from the action (comma separated). E.g. "package.json,pyproject.toml"'
required: false
default: ''
outputs:
results:
description: 'Code review results (experimental)'
runs:
using: 'composite'
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install Dependencies
run: pip install -r ${{ github.action_path }}/requirements.txt
shell: bash
- name: Get common ancestor for branch point
id: first_commit
run: |
# Find the common ancestor of the base branch and the head branch
common_ancestor=$(git merge-base ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})
echo "sha=$common_ancestor" >> $GITHUB_OUTPUT
shell: bash
- name: Prepare excluded files
id: prepare_exclude
run: |
exclude_flags=${{ inputs.exclude-files }}
IFS=', ' read -r -a exclude_array <<< "$exclude_flags"
exclude_str=""
for index in "${!exclude_array[@]}"
do
exclude_str+="':!${exclude_array[index]}' "
done
echo "exclude_files=$exclude_str" >> $GITHUB_OUTPUT
shell: bash
- name: Run git diff
id: git_diff
run: |
source $GITHUB_OUTPUT
git diff --merge-base ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} -- . ${{ steps.prepare_exclude.outputs.exclude_files }} > diff.txt
shell: bash
- name: Get git diff
id: print_diff
run: |
echo "::set-output name=diff::$(cat diff.txt)"
shell: bash
- name: Print diff
run: echo "${{ steps.print_diff.outputs.diff }}"
shell: bash
- name: Run OpenAI code review
id: openai
run: |
cat diff.txt | python ${{ github.action_path }}/action_code_review.py > review.txt
echo 'reviewresult<<EOF' >> $GITHUB_OUTPUT
echo "$(cat review.txt)" >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.github-token }}
API_TO_USE: ${{ inputs.api-to-use }}
OPENAI_API_KEY: ${{ inputs.openai-key }}
ANTHROPIC_API_KEY: ${{ inputs.anthropic-key }}
MODEL: ${{ inputs.model }}
PERSONA: ${{ inputs.persona }}
STYLE: ${{ inputs.style }}
INCLUDE_FILES: ${{ inputs.include-full-files }}
COMMIT_TITLE: ${{ github.event.pull_request.title }}
COMMIT_BODY: ${{ github.event.pull_request.body }}
- name: Show output in case of failure
id: err-output
if: failure()
run: |
echo 'errorresult<<EOF' >> $GITHUB_OUTPUT
echo "ERROR: $(cat review.txt)" >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
echo "Review result and error: $(cat review.txt)"
shell: bash
- name: Create comment
if: success() || (inputs.post-if-error && inputs.post-if-error != 'false')
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
# Review:
${{ steps.openai.outputs.reviewresult && steps.openai.outputs.reviewresult || steps.err-output.outputs.errorresult }}
branding:
icon: 'user-check'
color: 'green'