-
Notifications
You must be signed in to change notification settings - Fork 0
399 lines (317 loc) · 13.3 KB
/
watch-repos.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
name: Process Repository Changes
'on':
push:
branches:
- main
- master
pull_request:
types:
- closed
workflow_dispatch:
inputs:
full_ingest:
description: Perform full repository ingestion
required: true
type: boolean
default: false
jobs:
process-changes:
if: >-
github.event_name == 'push' || (github.event_name == 'pull_request' &&
github.event.pull_request.merged == true) || github.event_name ==
'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install yq
run: >
sudo wget
https://github.com/mikefarah/yq/releases/download/v4.40.5/yq_linux_amd64
-O /usr/local/bin/yq
sudo chmod +x /usr/local/bin/yq
yq --version
- name: Load Configuration
id: config
env:
OSIRIS_URL: 'https://osiris-server.vercel.app'
run: >
# Install yq if not present
# sudo wget
https://github.com/mikefarah/yq/releases/download/v4.40.5/yq_linux_amd64
-O /usr/local/bin/yq
# sudo chmod +x /usr/local/bin/yq
# Get config from API endpoint
CONFIG=$(curl -s "$OSIRIS_URL/api/config")
if [ -z "$CONFIG" ]; then
echo "::error::Failed to fetch configuration from osiris-server"
exit 1
fi
# Parse the YAML into JSON for the repository
REPO_CONFIG=$(echo "$CONFIG" | yq -o=json ".repositories[\"${{
github.repository }}\"]")
if [ "$REPO_CONFIG" == "null" ]; then
echo "Repository ${{ github.repository }} not configured for watching"
exit 0
fi
# Log for debugging
echo "Repository config:"
echo "$REPO_CONFIG" | jq '.'
# Export config
echo 'CONFIG<<EOF' >> $GITHUB_ENV
echo "$REPO_CONFIG" >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
{
echo "config<<EOF"
echo "$REPO_CONFIG"
echo "EOF"
echo "osiris_url=$OSIRIS_URL"
echo "config_exists=true"
} >> $GITHUB_OUTPUT
- name: Setup API Helper
if: steps.config.outputs.config_exists == 'true'
run: |
cat << \EOF > api_helper.sh
#!/bin/bash
call_api() {
local url="$1"
local data="$2"
local retries=5
local wait=5
local timeout=60
for i in $(seq 1 $retries); do
echo "DEBUG: API call attempt $i of $retries"
# Make the API call
local response=$(curl -X POST "$url" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
--silent \
--max-time $timeout \
--retry 3 \
--retry-delay 2 \
--data-raw "$data")
echo "DEBUG: Testing if response is valid JSON"
if echo "$response" | jq '.' >/dev/null 2>&1; then
echo "DEBUG: Response is valid JSON"
echo "$response"
return 0
else
echo "DEBUG: Response is not valid JSON or empty"
echo "Raw response: $response"
fi
echo "Waiting ${wait}s before retry..."
sleep $wait
wait=$((wait * 2))
done
return 1
}
EOF
chmod +x api_helper.sh
- name: Full Repository Ingestion
if: >-
steps.config.outputs.config_exists == 'true' && github.event_name ==
'workflow_dispatch' && github.event.inputs.full_ingest == 'true'
run: |
source ./api_helper.sh
echo "Starting full repository ingestion..."
echo "DEBUG: Making initial API call..."
# First construct the REQUEST_BODY using jq
REQUEST_BODY=$(jq -n \
--arg repo "${{ github.repository }}" \
--arg branch "${{ github.ref_name }}" \
--arg event_type "${{ github.event_name }}" \
--arg commit_sha "${{ github.sha }}" \
--argjson config "$CONFIG" \
'{
"repo": $repo,
"branch": $branch,
"metadata": {
"repository": $repo,
"branch": $branch,
"event_type": $event_type,
"commit_sha": $commit_sha,
"process_timestamp": (now | strftime("%Y-%m-%dT%H:%M:%SZ")),
"config": $config
},
"maxFileSize": ($config.max_file_size // 100000),
"maxTokens": ($config.max_tokens // 50000),
"forceReplace": true
}'
)
echo "DEBUG: Request body:"
echo "$REQUEST_BODY" | jq '.'
# Make the API call
echo "Making API call to ingest-repo..."
echo "DEBUG: Final JSON to ingest-repo"
echo "$REQUEST_BODY" | xxd
echo "$REQUEST_BODY" | jq '.'
response=$(call_api "${{ steps.config.outputs.osiris_url }}/api/ingest-repo" "$REQUEST_BODY")
api_status=$?
if [ $api_status -ne 0 ]; then
echo "::error::API call failed"
exit 1
fi
# Extract totalBatches directly (no need to revalidate JSON)
total_batches=$(echo "$response" | jq -r '.totalBatches')
if [ "$total_batches" = "null" ] || [ -z "$total_batches" ]; then
echo "::error::Could not find totalBatches in response"
echo "Response was: $response"
exit 1
fi
echo "Processing $total_batches batches..."
successful_batches=0
failed_batches=0
# Process batches
for ((batch=0; batch<total_batches; batch++)); do
echo "Processing batch $((batch + 1)) of $total_batches"
batch_request=$(jq -n \
--arg repo "${{ github.repository }}" \
--arg branch "${{ github.ref_name }}" \
--argjson batchStart "$((batch * 10))" \
--argjson batchSize "10" \
'{
repo: $repo,
branch: $branch,
batchStart: $batchStart,
batchSize: $batchSize
}'
)
if batch_response=$(call_api "${{ steps.config.outputs.osiris_url }}/api/process-batch" "$batch_request"); then
echo "Batch $((batch + 1)) processed successfully"
successful_batches=$((successful_batches + 1))
else
echo "::warning::Failed to process batch $((batch + 1))"
failed_batches=$((failed_batches + 1))
fi
[ $((batch + 1)) -lt "$total_batches" ] && sleep 2
done
echo "Repository ingestion completed:"
echo "- Total batches: $total_batches"
echo "- Successful: $successful_batches"
echo "- Failed: $failed_batches"
if [ "$successful_batches" -eq "$total_batches" ]; then
echo "::notice::Successfully processed all batches"
else
echo "::warning::Completed with $failed_batches failed batches"
[ "$successful_batches" -eq 0 ] && exit 1
fi
- name: Process Incremental Changes
if: >-
steps.config.outputs.config_exists == 'true' && !(github.event_name
== 'workflow_dispatch' && github.event.inputs.full_ingest == 'true')
run: >
source ./api_helper.sh
# Debug: Print full config at start
echo "Full Configuration from env:"
echo "$CONFIG" | jq '.'
# Create extensions file
echo "$CONFIG" | jq -r '.included_extensions[]' | tr -d '\r' >
included_extensions.txt
echo "Available extensions:"
cat included_extensions.txt
# Get commit range
if [ "${{ github.event_name }}" == "push" ]; then
BASE_SHA="${{ github.event.before }}"
HEAD_SHA="${{ github.event.after }}"
elif [ "${{ github.event_name }}" == "pull_request" ]; then
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
else
BASE_SHA=$(git rev-parse HEAD^)
HEAD_SHA=$(git rev-parse HEAD)
fi
echo "Base SHA: $BASE_SHA"
echo "Head SHA: $HEAD_SHA"
# Process changes with improved debug output
echo "Starting to process changed files..."
# Create temporary directory for processing
TEMP_DIR=$(mktemp -d)
trap 'rm -rf "$TEMP_DIR"' EXIT
# Process each changed file
git diff --name-status --no-renames $BASE_SHA $HEAD_SHA | while read
-r status filepath; do
echo "Processing: $filepath (Status: $status)"
[ -z "$filepath" ] && continue
ext=$(echo "${filepath##*.}" | tr -d '[:space:]')
echo "File extension: '$ext'"
if grep -ixFq "$ext" included_extensions.txt; then
echo "Extension '$ext' IS included"
if [ "$status" = "M" ] || [ "$status" = "A" ]; then
content=$(git show "$HEAD_SHA:$filepath" 2>/dev/null | jq -Rs) || continue
echo "$status $filepath $content" >> "$TEMP_DIR/changes.txt"
elif [ "$status" = "D" ]; then
echo "$status $filepath" >> "$TEMP_DIR/changes.txt"
fi
else
echo "Extension '$ext' is NOT included"
fi
done
# Process collected changes
if [ -f "$TEMP_DIR/changes.txt" ]; then
echo "Found changes to process"
# Build changes object
changes_json="{\"added\":["
first=true
while IFS=' ' read -r status filepath content; do
if [ "$status" = "A" ]; then
[ "$first" = true ] && first=false || changes_json+=","
changes_json+="{\"path\":\"$filepath\",\"content\":$content}"
fi
done < "$TEMP_DIR/changes.txt"
changes_json+="],\"modified\":["
first=true
while IFS=' ' read -r status filepath content; do
if [ "$status" = "M" ]; then
[ "$first" = true ] && first=false || changes_json+=","
changes_json+="{\"path\":\"$filepath\",\"content\":$content}"
fi
done < "$TEMP_DIR/changes.txt"
changes_json+="],\"removed\":["
first=true
while IFS=' ' read -r status filepath content; do
if [ "$status" = "D" ]; then
[ "$first" = true ] && first=false || changes_json+=","
changes_json+="{\"path\":\"$filepath\"}"
fi
done < "$TEMP_DIR/changes.txt"
changes_json+="]}"
# Call ingest-changes endpoint
if ! call_api "${{ steps.config.outputs.osiris_url }}/api/ingest-changes" "{
\"repository\": {
\"fullName\": \"${{ github.repository }}\",
\"defaultBranch\": \"${{ github.ref_name }}\"
},
\"changes\": $changes_json,
\"metadata\": {
\"repository\": \"${{ github.repository }}\",
\"branch\": \"${{ github.ref_name }}\",
\"event_type\": \"${{ github.event_name }}\",
\"commit_sha\": \"${{ github.sha }}\",
\"base_sha\": \"$BASE_SHA\",
\"head_sha\": \"$HEAD_SHA\",
\"max_file_size\": $(echo "$CONFIG" | jq .max_file_size),
\"max_tokens\": $(echo "$CONFIG" | jq .max_tokens),
\"process_timestamp\": \"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\"
}
}"; then
echo "::error::Failed to process changes"
exit 1
fi
else
echo "No relevant file changes detected"
fi
- name: Report Status
if: always()
run: |
if [ "${{ steps.config.outputs.config_exists }}" != "true" ]; then
echo "::notice::Repository not configured for watching"
elif [ "${{ job.status }}" == "success" ]; then
if [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ "${{ github.event.inputs.full_ingest }}" == "true" ]; then
echo "::notice::Successfully completed full repository ingestion"
else
echo "::notice::Successfully processed changes"
fi
else
echo "::error::Failed to process changes"
fi