-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgemini_finder.sh
More file actions
executable file
·238 lines (206 loc) · 7.44 KB
/
gemini_finder.sh
File metadata and controls
executable file
·238 lines (206 loc) · 7.44 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
# Find Gemini's Summary of Changes and update PR description
# Usage: ./gemini_finder.sh [PR_NUMBER]
# Check if gh CLI is installed
if ! command -v gh &> /dev/null; then
echo "Error: GitHub CLI (gh) is not installed"
echo "Please install GitHub CLI: https://cli.github.com/"
exit 1
fi
# If GEMINI_API_KEY is not set, try to get it from pass
if [ -z "$GEMINI_API_KEY" ]; then
api_key_from_pass=$(pass show gemini/key 2>/dev/null)
if [ -n "$api_key_from_pass" ]; then
GEMINI_API_KEY="$api_key_from_pass"
fi
fi
# Check if GEMINI_API_KEY exists
if [ -z "$GEMINI_API_KEY" ]; then
echo "Error: GEMINI_API_KEY is not set"
echo "Please set GEMINI_API_KEY in your ~/.zshrc or password manager"
exit 1
fi
# Check if logged into GitHub
if ! gh auth status &> /dev/null; then
echo "Please login to GitHub first:"
gh auth login
fi
# Check if PR number is provided
if [ -z "$1" ]; then
echo "Please provide a PR number"
exit 1
fi
PR_NUMBER=$1
echo "Processing PR #$PR_NUMBER"
# Get PR reviews and comments
echo "Getting PR reviews and comments..."
REVIEWS=$(gh pr view $PR_NUMBER --json reviews,comments)
REVIEW_COUNT=$(echo "$REVIEWS" | jq '.reviews | length')
echo "Found $REVIEW_COUNT reviews"
# Find Gemini generated content
echo "Looking for Gemini's Summary of Changes..."
GEMINI_CONTENT=""
# First look in reviews
for ((i=0; i<$REVIEW_COUNT; i++)); do
AUTHOR=$(echo "$REVIEWS" | jq -r ".reviews[$i].author.login")
if [ "$AUTHOR" = "gemini-code-assist" ]; then
echo "Found gemini-code-assist review..."
REVIEW_BODY=$(echo "$REVIEWS" | jq -r ".reviews[$i].body")
if [[ $REVIEW_BODY == *"Summary of Changes"* ]]; then
GEMINI_CONTENT="$REVIEW_BODY"
echo "Found content in gemini-code-assist review!"
break
fi
fi
done
# If not found in reviews, check comments
if [ -z "$GEMINI_CONTENT" ]; then
echo "Content not found in reviews, checking comments..."
COMMENT_COUNT=$(echo "$REVIEWS" | jq '.comments | length')
echo "Found $COMMENT_COUNT comments"
for ((i=0; i<$COMMENT_COUNT; i++)); do
AUTHOR=$(echo "$REVIEWS" | jq -r ".comments[$i].author.login")
if [ "$AUTHOR" = "gemini-code-assist" ]; then
echo "Found gemini-code-assist comment..."
COMMENT_BODY=$(echo "$REVIEWS" | jq -r ".comments[$i].body")
if [[ $COMMENT_BODY == *"Summary of Changes"* ]]; then
GEMINI_CONTENT="$COMMENT_BODY"
echo "Found content in gemini-code-assist comment!"
break
fi
fi
done
fi
if [ -z "$GEMINI_CONTENT" ]; then
echo "No Gemini generated content found (in either reviews or comments)"
exit 1
fi
echo "Gemini content status: Found"
# Create temporary files
TEMP_FILE=$(mktemp)
RESULT_FILE="${TEMP_FILE}.final"
# Save original content to temp file, preserving line breaks
echo "$GEMINI_CONTENT" > "$TEMP_FILE"
# Extract main content
echo "Extracting main content..."
if grep -q "## Summary of Changes" "$TEMP_FILE"; then
# Use awk to extract content
awk '
BEGIN { printing = 0; changelog_found = 0; details_found = 0; list_mode = 0 }
/^## Summary of Changes$/ { printing = 1 }
printing == 1 {
if ($0 ~ /^### Changelog$/) {
changelog_found = 1
print "## Changelog"
next
}
if (changelog_found) {
# Check if details tag exists
if ($0 ~ /^<details>/) {
details_found = 1
print ""
print
next
}
# If list item found and no details tag
if (!details_found && ($0 ~ /^\* / || $0 ~ /^ \* /)) {
list_mode = 1
print
next
}
# If in list mode and empty line or new section found, exit
if (list_mode && ($0 ~ /^$/ || $0 ~ /^##/)) {
exit
}
# If in details mode and closing tag found
if (details_found && $0 ~ /^<\/details>/) {
print
exit
}
# Output other content
if (details_found || list_mode) {
print
}
}
if (!changelog_found) {
print
}
}
' "$TEMP_FILE" > "$RESULT_FILE"
else
# If Summary of Changes not found, use first half of content
sed -n '1,/^<details>/p' "$TEMP_FILE" | sed '$d' > "$RESULT_FILE"
fi
# Clean up content
sed -i '' '/^$/N;/^\n$/D' "$RESULT_FILE" # Remove consecutive empty lines
# Use Gemini API for translation
echo "Using Gemini API to translate content..."
CONTENT_TO_TRANSLATE=$(cat "$RESULT_FILE")
echo "Original content length: $(echo "$CONTENT_TO_TRANSLATE" | wc -c) characters"
# Escape content to avoid JSON format issues
ESCAPED_CONTENT=$(echo "$CONTENT_TO_TRANSLATE" | jq -Rs .)
PROMPT=$(cat << EOF | jq -Rs .
You are a professional translation assistant. Please translate the following English content into Traditional Chinese. Note:
1. All English content must be translated to Traditional Chinese
2. Keep all Markdown format markers unchanged
3. Keep all code blocks, links, and technical terms format unchanged
4. Use professional terminology common in Taiwan
Here's the content to translate:
$CONTENT_TO_TRANSLATE
EOF
)
# Build API URL (ensure API key is properly encoded)
API_URL="https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
ENCODED_KEY=$(echo "$GEMINI_API_KEY" | jq -sRr @uri)
# Prepare request content
REQUEST_BODY=$(jq -n \
--arg prompt "$PROMPT" \
'{
"contents": [{
"parts":[{
"text": $prompt
}]
}]
}')
# Send API request
RESPONSE=$(curl -s -X POST "${API_URL}?key=${ENCODED_KEY}" \
-H "Content-Type: application/json" \
-d "$REQUEST_BODY")
echo "Full API response:" > /tmp/gemini_response.log
echo "$RESPONSE" >> /tmp/gemini_response.log
echo "Request content:" >> /tmp/gemini_response.log
echo "$REQUEST_BODY" >> /tmp/gemini_response.log
# Check if response is empty
if [ -z "$RESPONSE" ]; then
echo "API response is empty"
echo "$CONTENT_TO_TRANSLATE" > "$RESULT_FILE"
else
# Try to parse response
if echo "$RESPONSE" | jq -e '.candidates[0].content.parts[0].text' > /dev/null 2>&1; then
TRANSLATED_CONTENT=$(echo "$RESPONSE" | jq -r '.candidates[0].content.parts[0].text')
echo "Translation successful"
# Remove leading and trailing code markers
CLEANED_CONTENT=$(echo "$TRANSLATED_CONTENT" | sed -E '1s/^```(markdown)?[[:space:]]*//' | sed -E '$s/```[[:space:]]*$//')
echo "$CLEANED_CONTENT" > "$RESULT_FILE"
else
echo "Unable to parse API response, using original content"
echo "API response content:"
echo "$RESPONSE"
echo "$CONTENT_TO_TRANSLATE" > "$RESULT_FILE"
fi
fi
# Preview final content
echo "Final content preview:"
head -n 5 "$RESULT_FILE"
echo "..."
# Update PR description
echo "Updating PR description..."
if [ -s "$RESULT_FILE" ]; then
gh pr edit $PR_NUMBER --body "$(cat "$RESULT_FILE")"
echo "PR description updated"
else
echo "Error: Result file is empty, keeping original PR description"
exit 1
fi
# Clean up temporary files
rm -f "$TEMP_FILE" "$RESULT_FILE"