|
| 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 | + }); |
0 commit comments