generated from darrentyson/GitHub-Classroom-Introductory-Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
511 lines (453 loc) · 18.1 KB
/
classroom.yml
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
name: GitHub Classroom Workflow
on:
push:
branches:
- '*'
- '!status'
- '!feedback'
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
issue_comment:
types: [created]
jobs:
# job to run autograding
build:
name: Autograding
runs-on: ubuntu-latest
timeout-minutes: 5
if: ${{ !(github.event_name == 'issue_comment') || contains(github.event.repository.name, github.actor) }}
steps:
- uses: actions/checkout@v2
# pause to wait for classroom bot to setup feedback PR
- if: ${{ github.actor == 'github-classroom[bot]' }}
run: sleep 30
# Default branch is usually 'main', but in case it isn't get default branch name
- name: Get default branch name
uses: actions/github-script@v5
id: default-branch-name
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
// get default branch
const repo = await github.rest.repos.get({
owner: context.repo.owner,
repo: context.repo.repo,
});
console.log(`Get repo response - status: ${repo.status}`);
return repo.data.default_branch;
result-encoding: string
- run: echo "Default branch name - ${{ steps.default-branch-name.outputs.result }}"
# find PR if it exists
- name: Find PR number
uses: markpatterson27/find-pull-request-action@pre-pr-release
id: check-pr
with:
github-token: ${{secrets.GITHUB_TOKEN}}
title: Feedback
base: feedback
branch: ${{ steps.default-branch-name.outputs.result }}
state: all
- run: echo ${{ steps.check-pr.outputs.number }}
# re-open PR if closed
- name: Reopen PR
if: ${{ steps.check-pr.outputs.state == 'closed' }}
uses: actions/github-script@v5
env:
PR_NUMBER: ${{ steps.check-pr.outputs.number }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
try {
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: process.env.PR_NUMBER,
state: 'open'
});
} catch(err) {
console.log("Error reopening PR. (Merged PRs can't be reopened).");
}
# delete and recreate result dir
- name: Reset results dir
run: |
rm -rf .github/results
mkdir -p .github/results
# test activity 1
- uses: actions/github-script@v4
name: "Check Activity 1"
id: activity1
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
// get repo name
const repoName = context.repo.repo.toLowerCase()
// get repo members
const res = await github.repos.listCollaborators({
owner: context.repo.owner,
repo: context.repo.repo,
});
let collaborators = res.data
console.log(collaborators)
// check if one of collab list matches repository name suffix. make case insensitive.
// pattern is `assignmentname-username` or `assignmentname-username-i`. #TODO: optionally remove number suffix and use endsWith to match
if (collaborators.some(collaborator=>repoName.includes(collaborator.login.toLowerCase()))) {
console.log("found collaborator match to repo suffix")
// write result to file
const fs = require('fs');
fs.writeFile('.github/results/activity1.txt', 'pass', function (err) {
if (err) return console.log(err);
});
return 'success'
}
else {
console.log("no match to repo suffix")
return 'fail'
}
result-encoding: string
# test activity 2
- uses: actions/github-script@v4
name: "Check Activity 2"
id: activity2
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
// get commits, filtered by actor
const res = await github.repos.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
author: context.actor,
});
let commitList = res.data
console.log(commitList)
// is commit list non-zero
if (Array.isArray(commitList) && commitList.length) {
console.log("commits found")
// write result to file
const fs = require('fs');
fs.writeFile('.github/results/activity2.txt', 'pass', function (err) {
if (err) return console.log(err);
});
return 'success'
}
else {
console.log(`no commits for ${context.actor} found`)
return 'fail'
}
result-encoding: string
# test activity 3
- uses: actions/github-script@v4
name: "Check Activity 3"
id: activity3
if: ${{ steps.check-pr.outputs.number }}
env:
ISSUE: ${{ steps.check-pr.outputs.number }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
// get comments on Feedback PR
const res = await github.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: process.env.ISSUE,
});
let commentList = res.data
console.log(commentList)
// is comment list non-zero AND does actor equal one of comment authors
if (Array.isArray(commentList) && commentList.length && commentList.some(comment => comment.user.login == context.actor)) {
console.log("comments found")
// write result to file
const fs = require('fs');
fs.writeFile('.github/results/activity3.txt', 'pass', function (err) {
if (err) return console.log(err);
});
return 'success'
}
else {
console.log(`no comments for ${context.author} found`)
return 'fail'
}
result-encoding: string
- run: ls .github/results
# run grading
# add id to step so outputs can be referenced
- uses: education/autograding@v1
name: "** Grading and Feedback **"
id: autograde
continue-on-error: true
# # fail job if autograde returns failed
# # outcome can be 'success', 'failure', 'cancelled', or 'skipped'
# # trigger fail either on !success or on failure depending on preference
# - name: check autograde pass fail
# if: ${{ steps.autograde.outcome != 'success' }}
# run: exit 1
outputs:
grading-score: ${{ steps.autograde.outputs.Points }}
activity1-result: ${{ steps.activity1.outputs.result }}
activity2-result: ${{ steps.activity2.outputs.result }}
activity3-result: ${{ steps.activity3.outputs.result }}
default-branch-name: ${{ steps.default-branch-name.outputs.result }}
feedback-pr: ${{ steps.check-pr.outputs.number }}
# job to build activity status icons
build-activity-icons:
name: Build Activity Icons
runs-on: ubuntu-latest
if: ${{ always() && (!(github.event_name == 'issue_comment') || contains(github.event.repository.name, github.actor)) }}
needs: build
steps:
# need to checkout whole repo
- uses: actions/checkout@v2
with:
fetch-depth: 0
# get quiz score
- name: Calculate quiz score
id: quiz-score
run: |
if [[ $(git log remotes/origin/feedback..main quiz.md) ]]; then
echo "quiz.md file changed"
/bin/bash .github/grading-scripts/quiz.sh || echo "::set-output name=quiz_score::0"
else
echo "quiz.md file not changed"
fi
# switch to status branch
- run: git checkout status || git checkout -b status
# make dir for activity status icons
- name: make icons dir
run: mkdir -p .github/activity-icons
# make/copy activity 1 icon
- name: activity 1 icon
run: |
echo ${{ needs.build.outputs.activity1-result }}
if ${{ needs.build.outputs.activity1-result == 'success' }}; then
cp .github/templates/activity-completed.svg .github/activity-icons/activity1.svg
else
cp .github/templates/activity-incomplete.svg .github/activity-icons/activity1.svg
fi
# make/copy activity 2 icon
- name: activity 2 icon
run: |
echo ${{ needs.build.outputs.activity2-result }}
if ${{ needs.build.outputs.activity2-result == 'success' }}; then
cp .github/templates/activity-completed.svg .github/activity-icons/activity2.svg
else
cp .github/templates/activity-incomplete.svg .github/activity-icons/activity2.svg
fi
# make/copy activity 3 icon
- name: activity 3 icon
run: |
echo ${{ needs.build.outputs.activity3-result }}
if ${{ needs.build.outputs.activity3-result == 'success' }}; then
cp .github/templates/activity-completed.svg .github/activity-icons/activity3.svg
else
cp .github/templates/activity-incomplete.svg .github/activity-icons/activity3.svg
fi
# make/copy quiz icon
- name: quiz icon
run: |
if [[ ! -z "${{ steps.quiz-score.outputs.quiz_score }}" ]]; then
score=${{ steps.quiz-score.outputs.quiz_score }}
echo $score
if [[ $score == 5 ]]; then
cp .github/templates/quiz5.svg .github/activity-icons/quiz.svg
else
cp .github/templates/quiz.svg .github/activity-icons/quiz.svg
sed -i "s/><\/text>/>${score}<\/text>/" .github/activity-icons/quiz.svg
sed -i "s/>[0-9]<\/text>/>${score}<\/text>/" .github/activity-icons/quiz.svg
fi
fi
# create points bar
- name: points bar
uses: markpatterson27/points-bar@v1
with:
points: ${{ needs.build.outputs.grading-score }}
path: '.github/activity-icons/points-bar.svg'
# commit and push activity icons if statuses have changed
- name: Commit changes
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add '.github/activity-icons'
git commit -m "Add/Update activity icons" || exit 0
git push origin status
# create Feedback PR
create-feedback-pr:
name: Create Feedback PR
runs-on: ubuntu-latest
# run even if autograding fails.
if: ${{ always() && needs.build.outputs.feedback-pr == '' }}
needs: [build]
steps:
# checkout files so can access template
- uses: actions/checkout@v2
with:
fetch-depth: 0
# read template
- uses: markpatterson27/markdown-to-output@v1
id: mto
with:
filepath: .github/templates/pr_body.md
# switch to feedback branch
- name: Create feedback branch
run: |
git checkout feedback || git checkout -b feedback
git push origin feedback
# check if base and head are same. create empty commit if they are
- name: Create empty commit
env:
DEFAULT_BRANCH: ${{ needs.build.outputs.default-branch-name }}
run: |
if [[ $(git rev-parse feedback) == $(git rev-parse $DEFAULT_BRANCH) ]]; then
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git checkout $DEFAULT_BRANCH
git commit -m "Setup Feedback PR" --allow-empty
git push origin $DEFAULT_BRANCH
fi
# create feedback pr
- name: Create Feedback PR
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
title: "Feedback"
body: ${{ steps.mto.outputs.body }}
base: feedback
head: ${{ needs.build.outputs.default-branch-name }}
run: |
gh pr create --base "$base" --head "$head" --title "$title" --body "$body"
# job to post feedback message in Feedback PR
# Classroom will create the PR when assignment accepted. PR should be issue 1.
post-feedback:
name: Post Feedback Comment
runs-on: ubuntu-latest
# run even if autograding fails. only run on main branch and if pr exists.
if: ${{ always() && needs.build.outputs.feedback-pr != '' && github.ref == 'refs/heads/main' && github.actor != 'github-classroom[bot]' }}
needs: [build]
steps:
# checkout files so can access template
- uses: actions/checkout@v2
with:
fetch-depth: 0
# get quiz score
- name: Calculate quiz score
id: quiz-score
run: |
if [[ $(git log remotes/origin/feedback..main quiz.md) ]]; then
echo "quiz.md file changed"
/bin/bash .github/grading-scripts/quiz.sh || echo "::set-output name=quiz_score::0"
else
echo "quiz.md file not changed"
fi
# set env var
- name: Set additional environment variables
run: |
echo "REPO_NAME=$(echo '${{ github.repository }}' | awk -F '/' '{print $2}')" >> $GITHUB_ENV
# read template
- uses: markpatterson27/markdown-to-output@v1
id: mto
with:
filepath: .github/templates/pr_feedback.md
# set feedback var
# activity 1 feedback
- name: activity 1 feedback
run: |
if ${{ needs.build.outputs.activity1-result == 'success' }}; then
message="${{ steps.mto.outputs.activity1-success }}"
status="${{ steps.mto.outputs.status-success }}"
else
message="${{ steps.mto.outputs.activity1-fail }}"
status="${{ steps.mto.outputs.status-fail }}"
fi
echo "fb-activity1=$(/bin/bash .github/scripts/escape.sh "$message")" >> $GITHUB_ENV
echo "status-activity1=$(/bin/bash .github/scripts/escape.sh "$status")" >> $GITHUB_ENV
# activity 2 feedback
- name: activity 2 feedback
run: |
if ${{ needs.build.outputs.activity2-result == 'success' }}; then
message="${{ steps.mto.outputs.activity2-success }}"
status="${{ steps.mto.outputs.status-success }}"
else
message="${{ steps.mto.outputs.activity2-fail }}"
status="${{ steps.mto.outputs.status-fail }}"
fi
echo "fb-activity2=$(/bin/bash .github/scripts/escape.sh "$message")" >> $GITHUB_ENV
echo "status-activity2=$(/bin/bash .github/scripts/escape.sh "$status")" >> $GITHUB_ENV
# activity 3 feedback
- name: activity 3 feedback
run: |
if ${{ needs.build.outputs.activity3-result == 'success' }}; then
message="${{ steps.mto.outputs.activity3-success }}"
status="${{ steps.mto.outputs.status-success }}"
else
message="${{ steps.mto.outputs.activity3-fail }}"
status="${{ steps.mto.outputs.status-fail }}"
fi
echo "fb-activity3=$(/bin/bash .github/scripts/escape.sh "$message")" >> $GITHUB_ENV
echo "status-activity3=$(/bin/bash .github/scripts/escape.sh "$status")" >> $GITHUB_ENV
# quiz feedback
- name: quiz feedback
run: |
if [[ $(git log remotes/origin/feedback..main quiz.md) ]]; then
score=${{ steps.quiz-score.outputs.quiz_score }}
incorrect=(${{ steps.quiz-score.outputs.incorrect_answers }})
message=""
if [[ $score == 5 ]]; then
message="${{ steps.mto.outputs.quiz-success }}"
status="${{ steps.mto.outputs.status-success }}"
else
for i in ${incorrect[@]}; do
message+="Question $i incorrect. Try again. "$'\n'
done
case $score in
0) status=":zero:" ;;
1) status=":one:" ;;
2) status=":two:" ;;
3) status=":three:" ;;
4) status=":four:" ;;
esac
fi
else
message="Quiz not attempted"
status=":zero:"
fi
echo "fb-quiz=$(/bin/bash .github/scripts/escape.sh "$message")" >> $GITHUB_ENV
echo "status-quiz=$(/bin/bash .github/scripts/escape.sh "$status")" >> $GITHUB_ENV
# replace tokens
# read template file and replace tokens. token replacement based on env name.
- name: prepare comment and substitute tokens
id: prep
uses: actions/github-script@v3
env:
points: ${{ needs.build.outputs.grading-score }}
template: ${{ steps.mto.outputs.body }}
with:
script: |
const fs = require('fs')
let commentBody = process.env.template
for (envName in process.env) {
commentBody = commentBody.replace("${"+envName+"}", process.env[envName]
.replace(/%0D/g, '\r')
.replace(/%0A/g, '\n')
.replace(/%25/g, '\%'))
}
return commentBody
result-encoding: string
# hide old feedback comments
- name: hide old feedback comments
uses: kanga333/comment-hider@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
issue_number: ${{ needs.build.outputs.feedback-pr }}
# post comment on feedback PR. issues and PRs have same numbers
- name: post comment
uses: actions/github-script@v3
env:
MESSAGE: ${{ steps.prep.outputs.result }}
ISSUE: ${{ needs.build.outputs.feedback-pr }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const { MESSAGE, ISSUE } = process.env
await github.issues.createComment({
issue_number: process.env.ISSUE,
owner: context.repo.owner,
repo: context.repo.repo,
body: `${MESSAGE}`
})