Repository navigation
Expand file tree
/
Copy pathpr-validation-bot.yml
More file actions
312 lines (276 loc) · 11.4 KB
/
Copy pathpr-validation-bot.yml
File metadata and controls
312 lines (276 loc) · 11.4 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
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
name: PR Validation & Feedback Bot
# Student-focused workflow: validates PRs, welcomes first-timers, provides educational feedback
# This workflow runs on ALL branches - students work on feature branches
on:
pull_request:
types: [opened, edited, synchronize, reopened]
issue_comment:
types: [created]
concurrency:
group: pr-validation-${{ github.event.pull_request.number || github.event.issue.number || github.run_id }}
cancel-in-progress: true
permissions:
contents: read
pull-requests: write
issues: write
statuses: write
jobs:
welcome-first-timer:
name: Welcome First-Time Contributor
runs-on: ubuntu-latest
timeout-minutes: 10
if: github.event_name == 'pull_request' && github.event.action == 'opened'
steps:
- name: Check if first-time contributor
uses: actions/github-script@v8
with:
script: |
try {
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'all',
creator: context.payload.pull_request.user.login
});
const isFirstPR = prs.length === 1;
if (isFirstPR) {
const welcomeBody = [
'## Welcome to Your First Pull Request',
'',
'Hi @' + context.payload.pull_request.user.login + '! Congratulations on opening your first PR in the Learning Room!',
'',
'### What Happens Next',
'',
'1. **Automated checks** - This bot validates your PR within about 30 seconds',
'2. **Review feedback** - You will see a validation report below',
'3. **Peer review** - A facilitator or peer will review your changes',
'4. **Iterate** - Make updates if needed based on feedback',
'5. **Merge** - Once approved, your changes are merged',
'',
'### While You Wait',
'',
'- Check the automated validation report below',
'- Review the [PR Guidelines](https://github.com/Community-Access/git-going-with-github/blob/main/docs/06-working-with-pull-requests.md)',
'- Look at other open PRs to learn from examples',
'- Ask questions in comments - no question is too basic',
'',
'**Remember:** Every experienced open source contributor started exactly where you are now.',
'Your perspective and effort matter!',
'',
'---',
'*This is an automated message from the Learning Room Bot.*'
].join('\n');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: welcomeBody
});
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
labels: ['first-time-contributor', 'needs-review']
});
}
} catch (error) {
console.error('Error checking first-time contributor status:', error);
}
validate-pr:
name: Validate PR & Provide Feedback
runs-on: ubuntu-latest
timeout-minutes: 15
if: github.event_name == 'pull_request'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install Node dependencies
run: |
if [ -f package-lock.json ]; then
npm ci
elif [ -f package.json ]; then
npm install
else
echo "No package.json found; skipping npm install"
fi
- name: Install Python dependencies
run: |
pip install requests markupsafe
- name: Run PR validation
id: validate
continue-on-error: true
run: node .github/scripts/validate-pr.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_TITLE: ${{ github.event.pull_request.title }}
PR_BODY: ${{ github.event.pull_request.body }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
HEAD_REF: ${{ github.head_ref }}
- name: Detect challenge context
id: challenge
uses: actions/github-script@v8
with:
script: |
// Detect which challenge this PR relates to from linked issues or branch name
const prBody = process.env.PR_BODY || '';
const branch = '${{ github.head_ref }}' || '';
// Check for "Closes #N" or "Fixes #N" patterns linking to challenge issues
const closePattern = /(?:closes|fixes|resolves)\s+#(\d+)/gi;
const matches = [...prBody.matchAll(closePattern)];
const linkedIssues = matches.map(m => parseInt(m[1]));
// Detect challenge from branch name (e.g., challenge-06/username)
const branchMatch = branch.match(/challenge-(\d+)/i);
const branchChallenge = branchMatch ? parseInt(branchMatch[1]) : null;
// Map challenges to curriculum chapters for help links
const challengeToChapter = {
1: '02', 2: '05', 3: '05', 4: '04', 5: '04',
6: '06', 7: '07', 8: '08', 9: '06', 10: '14',
11: '06', 12: '15', 13: '16', 14: '17', 15: '19', 16: '20'
};
const detected = branchChallenge || (linkedIssues.length > 0 ? null : null);
const chapter = detected ? challengeToChapter[detected] : null;
core.setOutput('number', detected || '');
core.setOutput('chapter', chapter || '');
core.setOutput('linked_issues', linkedIssues.join(','));
env:
PR_BODY: ${{ github.event.pull_request.body }}
- name: Post validation results
if: always()
uses: actions/github-script@v8
with:
script: |
const fs = require('fs');
const { buildValidationReportBody } = require('./.github/scripts/validation-report.js');
let results;
try {
if (!fs.existsSync('validation-results.json')) {
console.log('Warning: validation-results.json not found');
results = {
passed: false,
required: [{
name: 'Validation Output',
passed: false,
message: 'Validation results not found. Check workflow logs for details.'
}],
suggestions: [],
accessibility: [],
resources: []
};
} else {
results = JSON.parse(fs.readFileSync('validation-results.json', 'utf8'));
}
} catch (error) {
console.error('Error reading validation results:', error);
results = {
passed: false,
required: [{
name: 'Validation System Error',
passed: false,
message: `Error: ${error.message}`
}],
suggestions: [],
accessibility: [],
resources: []
};
}
const body = buildValidationReportBody(results);
try {
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('PR Validation Report')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: body
});
}
} catch (error) {
console.error('Could not post validation comment:', error.message);
}
comment-responder:
name: Respond to Help Requests
runs-on: ubuntu-latest
timeout-minutes: 10
if: >-
github.event_name == 'issue_comment' &&
github.event.comment.user.type != 'Bot' &&
(
contains(github.event.comment.body, '@bot help') ||
contains(github.event.comment.body, '@gandalf-bot') ||
contains(github.event.comment.body, 'need help') ||
contains(github.event.comment.body, 'Need help') ||
contains(github.event.comment.body, 'merge conflict') ||
contains(github.event.comment.body, 'Merge conflict') ||
contains(github.event.comment.body, 'request review') ||
contains(github.event.comment.body, 'Request review') ||
contains(github.event.comment.body, 'next challenge') ||
contains(github.event.comment.body, 'Next challenge') ||
contains(github.event.comment.body, 'assign me') ||
contains(github.event.comment.body, 'Assign me') ||
contains(github.event.comment.body, 'claim') ||
contains(github.event.comment.body, 'Claim')
)
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install dependencies
run: |
if [ -f package-lock.json ]; then
npm ci
elif [ -f package.json ]; then
npm install
else
echo "No package.json found; skipping npm install"
fi
- name: Respond to comment
uses: actions/github-script@v8
with:
script: |
const { getAutoResponse } = require('./.github/scripts/comment-responder.js');
const author = context.payload.comment.user.login;
const body = context.payload.comment.body;
const response = getAutoResponse(body, author);
if (response) {
try {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: response
});
} catch (error) {
console.error('Error posting response:', error);
}
}