Skip to content

Commit d8ee74c

Browse files
Add automated workflows for JSON validation and ID management
1 parent 1d33e68 commit d8ee74c

File tree

4 files changed

+593
-162
lines changed

4 files changed

+593
-162
lines changed

.github/workflows/auto-fix-ids.yml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
name: Auto-fix Story IDs
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "public/data/stories.json"
7+
push:
8+
branches:
9+
- main
10+
paths:
11+
- "public/data/stories.json"
12+
13+
jobs:
14+
fix-story-ids:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: write
18+
pull-requests: write
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 0
25+
token: ${{ secrets.GITHUB_TOKEN }}
26+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref }}
27+
28+
- name: Setup Node.js
29+
uses: actions/setup-node@v4
30+
with:
31+
node-version: "20"
32+
33+
- name: Validate JSON format
34+
id: validate
35+
run: |
36+
echo "Validating JSON structure..."
37+
if ! jq empty public/data/stories.json 2>/dev/null; then
38+
echo "valid=false" >> $GITHUB_OUTPUT
39+
echo "Invalid JSON format detected"
40+
exit 1
41+
fi
42+
echo "valid=true" >> $GITHUB_OUTPUT
43+
echo "JSON is valid"
44+
45+
- name: Check and fix duplicate IDs
46+
id: fix_ids
47+
run: |
48+
echo "Checking for duplicate or incorrect story IDs..."
49+
50+
# Create a Node.js script to fix IDs
51+
cat > fix-ids.js << 'EOF'
52+
const fs = require('fs');
53+
54+
const data = JSON.parse(fs.readFileSync('public/data/stories.json', 'utf8'));
55+
56+
const originalIds = data.stories.map(s => s.id);
57+
const duplicates = originalIds.filter((id, index) => originalIds.indexOf(id) !== index);
58+
59+
let hasChanges = false;
60+
61+
data.stories.forEach((story, index) => {
62+
const newId = index + 1;
63+
if (story.id !== newId) {
64+
console.log(`Story by ${story.username}: ID ${story.id} -> ${newId}`);
65+
story.id = newId;
66+
hasChanges = true;
67+
}
68+
});
69+
70+
if (duplicates.length > 0) {
71+
console.log('\nDuplicate IDs found:', [...new Set(duplicates)].join(', '));
72+
}
73+
74+
if (hasChanges) {
75+
console.log('\nIDs have been renumbered sequentially from 1 to', data.stories.length);
76+
fs.writeFileSync('public/data/stories.json', JSON.stringify(data, null, 2) + '\n');
77+
process.exit(1);
78+
} else {
79+
console.log('All IDs are already correctly numbered');
80+
process.exit(0);
81+
}
82+
EOF
83+
84+
# Run the script
85+
if node fix-ids.js; then
86+
echo "changes_made=false" >> $GITHUB_OUTPUT
87+
echo "No ID changes needed"
88+
else
89+
echo "changes_made=true" >> $GITHUB_OUTPUT
90+
echo "IDs were fixed and renumbered"
91+
fi
92+
93+
- name: Check if file changed
94+
id: check_changes
95+
if: steps.fix_ids.outputs.changes_made == 'true'
96+
run: |
97+
if git diff --quiet public/data/stories.json; then
98+
echo "file_changed=false" >> $GITHUB_OUTPUT
99+
else
100+
echo "file_changed=true" >> $GITHUB_OUTPUT
101+
fi
102+
103+
- name: Commit fixed IDs (for push to main)
104+
if: steps.check_changes.outputs.file_changed == 'true' && github.event_name == 'push'
105+
run: |
106+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
107+
git config --local user.name "github-actions[bot]"
108+
git add public/data/stories.json
109+
git commit -m "Auto-fix: Renumber story IDs sequentially [skip ci]"
110+
git push
111+
112+
- name: Commit fixed IDs (for pull request)
113+
if: steps.check_changes.outputs.file_changed == 'true' && github.event_name == 'pull_request'
114+
run: |
115+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
116+
git config --local user.name "github-actions[bot]"
117+
git add public/data/stories.json
118+
git commit -m "Auto-fix: Renumber story IDs sequentially"
119+
git push
120+
121+
- name: Comment on PR about ID fixes
122+
if: steps.check_changes.outputs.file_changed == 'true' && github.event_name == 'pull_request'
123+
uses: actions/github-script@v7
124+
with:
125+
script: |
126+
const comment = `## Story IDs Auto-Fixed
127+
128+
The workflow detected duplicate or non-sequential story IDs and has automatically renumbered them.
129+
130+
All stories are now numbered sequentially from 1 to ${context.payload.pull_request.commits}.
131+
132+
This ensures:
133+
- No duplicate IDs
134+
- Sequential numbering (1, 2, 3, etc.)
135+
- Consistent ID structure
136+
137+
The changes have been committed to your branch. Please pull the latest changes before making any additional commits.
138+
139+
\`\`\`bash
140+
git pull
141+
\`\`\`
142+
143+
Your story content and authorship remain unchanged - only the ID numbers were adjusted.`;
144+
145+
github.rest.issues.createComment({
146+
issue_number: context.issue.number,
147+
owner: context.repo.owner,
148+
repo: context.repo.repo,
149+
body: comment
150+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Merge Conflict Helper
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize]
6+
paths:
7+
- "public/data/stories.json"
8+
9+
jobs:
10+
check-conflicts:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
pull-requests: write
14+
contents: read
15+
16+
steps:
17+
- name: Checkout PR
18+
uses: actions/checkout@v4
19+
with:
20+
ref: ${{ github.event.pull_request.head.sha }}
21+
fetch-depth: 0
22+
23+
- name: Check for merge conflicts
24+
id: conflict_check
25+
run: |
26+
git fetch origin ${{ github.event.pull_request.base.ref }}
27+
28+
if git merge-tree $(git merge-base HEAD origin/${{ github.event.pull_request.base.ref }}) HEAD origin/${{ github.event.pull_request.base.ref }} | grep -q "<<<<<<< "; then
29+
echo "has_conflict=true" >> $GITHUB_OUTPUT
30+
echo "Merge conflict detected in stories.json"
31+
else
32+
echo "has_conflict=false" >> $GITHUB_OUTPUT
33+
echo "No merge conflicts detected"
34+
fi
35+
36+
- name: Comment on PR about conflict
37+
if: steps.conflict_check.outputs.has_conflict == 'true'
38+
uses: actions/github-script@v7
39+
with:
40+
script: |
41+
const comment = `## Merge Conflict Detected
42+
43+
It looks like there might be a merge conflict in \`stories.json\`.
44+
45+
Common causes:
46+
- Duplicate story IDs
47+
- Conflicting additions at the same position
48+
49+
### How to resolve:
50+
51+
1. Pull the latest changes from main:
52+
\`\`\`bash
53+
git fetch origin main
54+
git merge origin/main
55+
\`\`\`
56+
57+
2. If you have conflicts in \`stories.json\`:
58+
- Check for duplicate IDs - make sure your story has a unique ID
59+
- Keep both stories, just ensure they have different IDs
60+
- Make sure the JSON structure is valid (commas, brackets, etc.)
61+
62+
3. After fixing:
63+
\`\`\`bash
64+
git add public/data/stories.json
65+
git commit -m "Resolve merge conflict"
66+
git push
67+
\`\`\`
68+
69+
The auto-format workflow will clean up the formatting after merge.`;
70+
71+
github.rest.issues.createComment({
72+
issue_number: context.issue.number,
73+
owner: context.repo.owner,
74+
repo: context.repo.repo,
75+
body: comment
76+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: Validate and Format JSON
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "public/data/stories.json"
7+
push:
8+
branches:
9+
- main
10+
paths:
11+
- "public/data/stories.json"
12+
workflow_run:
13+
workflows: ["Auto-fix Story IDs"]
14+
types:
15+
- completed
16+
17+
jobs:
18+
validate-and-format:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
contents: write
22+
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0
28+
token: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: Setup Node.js
31+
uses: actions/setup-node@v4
32+
with:
33+
node-version: "20"
34+
35+
- name: Validate JSON format
36+
run: |
37+
echo "Validating JSON structure..."
38+
if ! jq empty public/data/stories.json 2>/dev/null; then
39+
echo "Invalid JSON format detected"
40+
exit 1
41+
fi
42+
echo "JSON is valid"
43+
44+
- name: Verify IDs are sequential
45+
run: |
46+
echo "Verifying story IDs are sequential..."
47+
node -e "
48+
const data = require('./public/data/stories.json');
49+
const ids = data.stories.map(s => s.id);
50+
const expectedIds = Array.from({length: ids.length}, (_, i) => i + 1);
51+
const match = JSON.stringify(ids) === JSON.stringify(expectedIds);
52+
if (!match) {
53+
console.log('IDs are not sequential. Expected:', expectedIds);
54+
console.log('Got:', ids);
55+
process.exit(1);
56+
}
57+
console.log('All IDs are correctly numbered from 1 to', ids.length);
58+
"
59+
60+
- name: Format JSON file
61+
run: |
62+
echo "Formatting JSON with proper indentation..."
63+
jq --indent 2 '.' public/data/stories.json > public/data/stories.formatted.json
64+
mv public/data/stories.formatted.json public/data/stories.json
65+
66+
- name: Check if formatting changed the file
67+
id: check_changes
68+
run: |
69+
if git diff --quiet public/data/stories.json; then
70+
echo "changed=false" >> $GITHUB_OUTPUT
71+
echo "No formatting changes needed"
72+
else
73+
echo "changed=true" >> $GITHUB_OUTPUT
74+
echo "File was formatted"
75+
fi
76+
77+
- name: Commit formatted JSON
78+
if: steps.check_changes.outputs.changed == 'true' && github.event_name == 'push'
79+
run: |
80+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
81+
git config --local user.name "github-actions[bot]"
82+
git add public/data/stories.json
83+
git commit -m "Auto-format stories.json [skip ci]"
84+
git push
85+
86+
- name: Comment on PR if formatting needed
87+
if: steps.check_changes.outputs.changed == 'true' && github.event_name == 'pull_request'
88+
uses: actions/github-script@v7
89+
with:
90+
script: |
91+
github.rest.issues.createComment({
92+
issue_number: context.issue.number,
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
body: 'The JSON file has been automatically formatted. Please pull the latest changes before merging.'
96+
})

0 commit comments

Comments
 (0)