-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathauto-reset-community-backlog.yml
More file actions
173 lines (157 loc) · 6.74 KB
/
Copy pathauto-reset-community-backlog.yml
File metadata and controls
173 lines (157 loc) · 6.74 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
name: Auto Reset Community Backlog
on:
workflow_call:
inputs:
minimum_available:
description: 'Reset a backlog when available items are at or below this count'
required: false
default: '5'
type: string
full_reset:
description: 'Reset every backlog item regardless of current availability'
required: false
default: false
type: boolean
dry_run:
description: 'Do not write changes; only report what would be reset'
required: false
default: false
type: boolean
workflow_dispatch:
inputs:
minimum_available:
description: 'Reset a backlog when available items are at or below this count'
required: false
default: '5'
type: string
full_reset:
description: 'Reset every backlog item regardless of current availability'
required: false
type: boolean
dry_run:
description: 'Do not write changes; only report what would be reset'
required: false
type: boolean
permissions:
actions: read
contents: write
pull-requests: write
concurrency:
group: auto-reset-community-backlog
cancel-in-progress: true
jobs:
reset-backlogs:
runs-on: ubuntu-latest
timeout-minutes: 10
if: github.repository == 'lingdojo/kana-dojo'
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.AUTOMATION_PR_TOKEN }}
- name: Reset low-availability backlogs
id: reset
uses: actions/github-script@v7
with:
github-token: ${{ secrets.AUTOMATION_PR_TOKEN || secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const backlogDir = 'community/backlog';
const minimumAvailable = Number(process.env.MINIMUM_AVAILABLE || 5);
const dryRun = String(process.env.DRY_RUN || 'false') === 'true';
const fullReset = String(process.env.FULL_RESET || 'false') === 'true';
const backlogFiles = [
['theme', 'theme-backlog.json'],
['fact', 'facts-backlog.json'],
['proverb', 'proverbs-backlog.json'],
['haiku', 'haiku-backlog.json'],
['trivia', 'trivia-backlog.json'],
['grammar', 'grammar-backlog.json'],
['animeQuote', 'anime-quotes-backlog.json'],
['videoGameQuote', 'video-game-quotes-backlog.json'],
['idiom', 'idioms-backlog.json'],
['regionalDialect', 'regional-dialects-backlog.json'],
['falseFriend', 'false-friends-backlog.json'],
['culturalEtiquette', 'cultural-etiquette-backlog.json'],
['exampleSentence', 'example-sentences-backlog.json'],
['commonMistake', 'common-mistakes-backlog.json'],
['wallpaperUrl', 'wallpaper-urls-backlog.json'],
['communityNote', 'community-notes-backlog.json'],
];
if (!Number.isInteger(minimumAvailable) || minimumAvailable < 0) {
throw new Error(`Invalid minimum_available input: ${context.payload.inputs && context.payload.inputs.minimum_available}`);
}
const changedFiles = [];
let resetCount = 0;
let filesConsidered = 0;
for (const [type, file] of backlogFiles) {
const filePath = path.join(backlogDir, file);
const items = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const available = items.filter(function(item) {
return !item.issued && !item.completed;
}).length;
filesConsidered += 1;
if (!fullReset && available > minimumAvailable) {
console.log(`Skipping ${type}: available=${available}`);
continue;
}
const resetItems = items.map(function(item) {
const next = { ...item, issued: false, completed: false };
delete next.completedBy;
delete next.completedPR;
return next;
});
console.log(`Resetting ${type}: available=${available}, total=${items.length}`);
changedFiles.push(filePath);
resetCount += 1;
if (!dryRun) {
fs.writeFileSync(filePath, JSON.stringify(resetItems, null, 2) + '\n');
}
}
console.log(`Backlog auto-reset summary: changed=${resetCount}, considered=${filesConsidered}, minimum_available=${minimumAvailable}, full_reset=${fullReset}, dry_run=${dryRun}`);
core.setOutput('changed_files', changedFiles.join(' '));
core.setOutput('changed_count', String(resetCount));
core.setOutput('needs_commit', !dryRun && resetCount > 0 ? 'true' : 'false');
core.setOutput('mode', fullReset ? 'full_reset' : 'threshold_reset');
env:
MINIMUM_AVAILABLE: ${{ inputs.minimum_available || github.event.inputs.minimum_available || '5' }}
FULL_RESET: ${{ inputs.full_reset || github.event.inputs.full_reset || 'false' }}
DRY_RUN: ${{ inputs.dry_run || github.event.inputs.dry_run || 'false' }}
- name: Commit backlog resets directly to main
if: steps.reset.outputs.needs_commit == 'true'
run: |
git config user.name "てんとう虫"
git config user.email "reservecrate@gmail.com"
git add ${{ steps.reset.outputs.changed_files }}
if git diff --cached --quiet; then
echo "No backlog reset changes to commit"
exit 0
fi
mode="${{ steps.reset.outputs.mode }}"
if [ "$mode" = "full_reset" ]; then
commit_msg="chore(automation): full reset community backlog"
else
commit_msg="chore(automation): auto reset community backlog"
fi
max_attempts=5
attempt=1
while [ "$attempt" -le "$max_attempts" ]; do
git commit -m "$commit_msg"
if git pull --rebase origin main && git push origin HEAD:main; then
echo "Backlog reset push succeeded on attempt $attempt"
exit 0
fi
echo "Backlog reset push attempt $attempt failed; retrying..."
git rebase --abort || true
git fetch origin main
git reset --hard origin/main
git add ${{ steps.reset.outputs.changed_files }}
if git diff --cached --quiet; then
echo "No backlog reset changes remain after sync"
exit 0
fi
attempt=$((attempt + 1))
done
echo "Failed to push backlog reset updates after $max_attempts attempts"
exit 1