Skip to content

Commit 9e236c7

Browse files
authored
combine samples
1 parent 59a5d72 commit 9e236c7

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed

.github/workflows/combine-samples.yaml

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ on:
1313
workflow_dispatch:
1414

1515
jobs:
16-
process_samples:
16+
process_samples:
1717
runs-on: ubuntu-latest
1818
if: github.event.pull_request.merged == true || github.event_name == 'push' || github.event_name == 'workflow_dispatch'
1919

2020
permissions:
2121
contents: write
2222

2323
steps:
24-
- name: Checkout repository
25-
uses: actions/checkout@v4
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
2626
with:
2727
fetch-depth: 0
2828
token: ${{ secrets.GITHUB_TOKEN }}
@@ -42,6 +42,23 @@ jobs:
4242
import sys
4343
from pathlib import Path
4444
45+
def flatten_samples(data):
46+
"""Flatten nested arrays to get individual sample objects"""
47+
if isinstance(data, list):
48+
result = []
49+
for item in data:
50+
if isinstance(item, dict):
51+
# It's a sample object, add it
52+
result.append(item)
53+
elif isinstance(item, list):
54+
# It's a nested array, flatten it recursively
55+
result.extend(flatten_samples(item))
56+
return result
57+
elif isinstance(data, dict):
58+
# Single sample object
59+
return [data]
60+
return []
61+
4562
# Initialize collections
4663
valid_samples = []
4764
errors = []
@@ -60,7 +77,7 @@ jobs:
6077
try:
6178
absolute_path = sample_file.resolve()
6279
relative_path = str(absolute_path.relative_to(base_path))
63-
except Exception as e:
80+
except Exception as e:
6481
print(f"✗ Error resolving path for {sample_file}: {str(e)}")
6582
continue
6683
@@ -80,8 +97,17 @@ jobs:
8097
# Try to parse as JSON
8198
try:
8299
sample_data = json.loads(content)
83-
valid_samples.append(sample_data)
84-
print(f"✓ Successfully processed: {relative_path}")
100+
# Flatten the data to ensure we get individual samples
101+
flattened = flatten_samples(sample_data)
102+
if flattened:
103+
valid_samples.extend(flattened)
104+
print(f"✓ Successfully processed: {relative_path} ({len(flattened)} sample(s))")
105+
else:
106+
errors.append({
107+
"file": relative_path,
108+
"error": "No valid samples found after parsing"
109+
})
110+
print(f"✗ No valid samples in: {relative_path}")
85111
except json.JSONDecodeError as e:
86112
errors.append({
87113
"file": relative_path,
@@ -109,7 +135,7 @@ jobs:
109135
metadata_dir = base_path / '.metadata'
110136
metadata_dir.mkdir(exist_ok=True)
111137
112-
# Write combined samples.json as a proper JSON array
138+
# Write combined samples.json as a proper flat JSON array
113139
samples_output = metadata_dir / 'samples.json'
114140
try:
115141
with open(samples_output, 'w', encoding='utf-8') as f:
@@ -121,7 +147,7 @@ jobs:
121147
122148
print(f"\n📊 Summary:")
123149
print(f" Total files found: {len(sample_files) if 'sample_files' in locals() else 0}")
124-
print(f" ✓ Valid samples: {len(valid_samples)}")
150+
print(f" ✓ Valid samples: {len(valid_samples)}")
125151
print(f" ✗ Errors: {len(errors)}")
126152
127153
# Handle errors.json
@@ -132,9 +158,9 @@ jobs:
132158
try:
133159
with open(errors_file, 'w', encoding='utf-8') as f:
134160
json.dump({
135-
"timestamp": "${{ github.event.head_commit.timestamp || github.event.pull_request. merged_at }}",
161+
"timestamp": "${{ github.event.head_commit.timestamp || github.event.pull_request.merged_at }}",
136162
"commit": "${{ github.sha }}",
137-
"errors": errors
163+
"errors": errors
138164
}, f, indent=2, ensure_ascii=False)
139165
print(f" ⚠️ Errors written to: {errors_file}")
140166
except Exception as e:
@@ -144,10 +170,10 @@ jobs:
144170
if errors_file.exists():
145171
try:
146172
errors_file.unlink()
147-
print(f" ✓ No errors found. Removed existing errors.json")
148-
except Exception as e:
173+
print(f" ✓ No errors found. Removed existing errors.json")
174+
except Exception as e:
149175
print(f" ⚠️ Could not remove errors.json: {str(e)}")
150-
else:
176+
else:
151177
print(f" ✓ No errors found.")
152178
153179
# Set output for git commit step
@@ -176,26 +202,40 @@ jobs:
176202
177203
- name: Commit and push changes
178204
if: steps.check_changes.outputs.changes == 'true'
179-
run: |
205+
run: |
180206
git config --local user.email "github-actions[bot]@users.noreply.github.com"
181207
git config --local user.name "github-actions[bot]"
182208
git add .metadata/
183209
184210
if [ "${{ steps.combine.outputs.has_errors }}" == "true" ]; then
185-
git commit -m "chore: update samples metadata (with errors) [skip ci]"
211+
git commit -m "chore: update samples metadata (with errors) [skip ci]"
186212
else
187213
git commit -m "chore: update samples metadata [skip ci]"
188214
fi
189215
190216
git push
191217
192-
- name: Summary
218+
- name: Check for sample errors
219+
if: always()
220+
run: |
221+
if [ -f .metadata/errors.json ]; then
222+
ERROR_COUNT=$(jq '.errors | length' . metadata/errors.json)
223+
224+
if [ "$ERROR_COUNT" -gt 0 ]; then
225+
echo "::warning:: Found $ERROR_COUNT sample validation error(s). Check the workflow summary for details."
226+
227+
# Output each error as a warning annotation with file reference
228+
jq -r '.errors[] | ":: warning file=\(.file)::\(.error)"' .metadata/errors.json
229+
fi
230+
fi
231+
232+
- name: Summary
193233
if: always()
194234
run: |
195235
echo "## Sample JSON Combination Results" >> $GITHUB_STEP_SUMMARY
196236
echo "" >> $GITHUB_STEP_SUMMARY
197237
198-
if [ -f .metadata/samples.json ]; then
238+
if [ -f . metadata/samples.json ]; then
199239
SAMPLE_COUNT=$(jq '. | length' .metadata/samples.json)
200240
echo "✅ **Valid Samples:** $SAMPLE_COUNT" >> $GITHUB_STEP_SUMMARY
201241
fi
@@ -208,15 +248,15 @@ jobs:
208248
echo "<details><summary>View errors</summary>" >> $GITHUB_STEP_SUMMARY
209249
echo "" >> $GITHUB_STEP_SUMMARY
210250
echo "\`\`\`json" >> $GITHUB_STEP_SUMMARY
211-
cat .metadata/errors.json >> $GITHUB_STEP_SUMMARY
251+
cat . metadata/errors.json >> $GITHUB_STEP_SUMMARY
212252
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
213253
echo "</details>" >> $GITHUB_STEP_SUMMARY
214254
else
215255
echo "" >> $GITHUB_STEP_SUMMARY
216256
echo "✅ **No Errors**" >> $GITHUB_STEP_SUMMARY
217257
fi
218258
219-
if [ "${{ steps.check_changes.outputs.changes }}" != "true" ]; then
259+
if [ "${{ steps. check_changes.outputs.changes }}" != "true" ]; then
220260
echo "" >> $GITHUB_STEP_SUMMARY
221261
echo "ℹ️ No changes detected in metadata files." >> $GITHUB_STEP_SUMMARY
222262
fi

0 commit comments

Comments
 (0)