-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreview
203 lines (171 loc) · 4.46 KB
/
review
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env bash
# Usage:
# review <options>...
#
# Send staged files to ChatGTP for a code review before commit. Optionally include untracked files
# You will need an OpenAI API key that you can generate here: https://platform.openai.com/account/api-keys
# Then set the environment variable OPENAI_KEY with the key's value and this will just work as-is
#
# Depends on:
# curl
# Exit immediately if a pipeline returns non-zero.
set -o errexit
# Allow the above trap be inherited by all functions in the script.
set -o errtrace
# Return value of a pipeline is the value of the last (rightmost) command to
# exit with a non-zero status, or zero if all commands in the pipeline exit
# successfully.
set -o pipefail
# Set $IFS to only newline and tab.
IFS=$'\n\t'
# This program's basename.
_ME="$(basename "${0}")"
# Deal with stuff
escape_special_chars() {
local string="$1"
string="${string//\\/\\\\}" # Escape backslashes
string="${string//\"/\\\"}" # Escape double quotes
string="${string//\//\\/}" # Escape forward slashes
string="${string//$'\n'/\\n}" # Escape newlines
string="${string//$'\r'/\\r}" # Escape carriage returns
string="${string//$'\t'/\\t}" # Escape tabs
echo "$string"
}
# Debug
__DEBUG_COUNTER=0
_debug() {
if ((${_USE_DEBUG:-0}))
then
__DEBUG_COUNTER=$((__DEBUG_COUNTER+1))
{
# Prefix debug message with "bug (U+1F41B)"
printf "🐛 %s " "${__DEBUG_COUNTER}"
"${@}"
printf "―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\\n"
} 1>&2
fi
}
# Error Messages
_exit_1() {
{
printf "%s " "$(tput setaf 1)!$(tput sgr0)"
"${@}"
} 1>&2
exit 1
}
_warn() {
{
printf "%s " "$(tput setaf 1)!$(tput sgr0)"
"${@}"
} 1>&2
}
# Help
_print_help() {
cat <<HEREDOC
Code reviewer
ChatGPT based code reviewer
Usage:
${_ME} [--options] [<arguments>]
${_ME} -h | --help
Options:
-h --help Display this help information.
HEREDOC
}
# Parse Options
_PRINT_HELP=0
_USE_DEBUG=0
_UNTRACKED=0
__get_option_value() {
local __arg="${1:-}"
local __val="${2:-}"
if [[ -n "${__val:-}" ]] && [[ ! "${__val:-}" =~ ^- ]]
then
printf "%s\\n" "${__val}"
else
_exit_1 printf "%s requires a valid argument.\\n" "${__arg}"
fi
}
while ((${#}))
do
__arg="${1:-}"
__val="${2:-}"
case "${__arg}" in
-h|--help)
_PRINT_HELP=1
;;
--debug)
_USE_DEBUG=1
;;
-u|--untracked)
_UNTRACKED=1
;;
--endopts)
# Terminate option parsing.
break
;;
-*)
_exit_1 printf "Unexpected option: %s\\n" "${__arg}"
;;
esac
shift
done
# Program Functions
_simple() {
repo_root=$(git rev-parse --show-toplevel)
printf "Performing code review for $repo_root...\\n"
untracked_files=""
if ((_UNTRACKED))
then
printf "Including untracked files.\\n"
# Get untracked files
untracked_files=$(git ls-files --others --exclude-standard --full-name)
fi
# Get staged files
staged_files=$(git diff --name-only --cached)
all_files="$staged_files"$'\n'"$untracked_files"
# Iterate over them
for file in $all_files; do
# file_contents=$(cat "$repo_root/$file" | sed -z 's/\n/\\n/g' | sed -z 's/"/\"/g' )
file_contents=$(cat "$repo_root/$file")
escaped_contents=$(escape_special_chars "$file_contents")
prompt="Review the code below and provide feedback on how to improve it.\\n\\n$escaped_contents\\n\\n"
response=$(curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "'"$prompt"'"}],
"temperature": 0.7
}')
# Show results
generated_text=$(echo "$response" | jq -r '.choices[0].message.content')
echo "Review for $file:"
echo "$generated_text"
done
}
# Main
_main() {
if ! command -v curl &> /dev/null
then
_exit_1 printf "curl not found.\\n"
fi
if ! command -v git &> /dev/null
then
_exit_1 printf "git not found.\\n"
fi
if ! command -v jq &> /dev/null
then
_exit_1 printf "jq not found.\\n"
fi
if [[ -z "$OPENAI_KEY" ]]; then
_exit_1 printf "OPENAI_KEY is not set.\\n"
fi
if ((_PRINT_HELP))
then
_print_help
else
_simple "$@"
fi
}
# Call `_main` after everything has been defined.
_main "$@"