Skip to content

Commit e49fa08

Browse files
committed
feat: improve GitHub workflows
1 parent 354fae7 commit e49fa08

File tree

4 files changed

+76
-67
lines changed

4 files changed

+76
-67
lines changed

.github/workflows/PR.yml

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,31 @@ jobs:
1515
const { owner, repo, number: issue_number } = context.issue;
1616
const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number });
1717
const title = pr.data.title;
18-
const labRegex = /\[LAB(\d+)\]/;
19-
const titleRegex = /^\[LAB\d+\] [\da-zA-Z]+$/;
2018
21-
if (!titleRegex.test(title)) {
22-
core.setFailed('PR title does not match the required format. Please use the format [LAB#] student#.');
19+
const titleRegex = /^\[LAB(\d+)\] [a-zA-Z]?\d+$/;
20+
const match = title.match(titleRegex);
21+
22+
let labNumberStr = undefined;
23+
if (match) {
24+
labNumberStr = match[1];
25+
} else {
26+
core.setFailed('PR title does not match the required format. Please use the format: [LAB#] <studentId>.');
2327
}
2428
25-
if (pr.data.head.ref !== pr.data.base.ref) {
26-
core.setFailed('The source branch and target branch must be the same.');
29+
const labelToAdd = `lab${labNumberStr}`;
30+
if (labNumberStr) {
31+
await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] });
2732
}
2833
2934
if (pr.data.base.ref === 'main') {
3035
core.setFailed('The target branch cannot be main.');
3136
}
3237
33-
const match = title.match(labRegex);
34-
if (match) {
35-
const labelToAdd = 'lab' + match[1];
36-
await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] });
37-
} else {
38-
core.setFailed('No match found in PR title. Please add a label in the format [LAB#] to the PR title.');
38+
if (labNumberStr < 3 && pr.data.head.ref !== pr.data.base.ref) {
39+
core.setFailed('The source branch and target branch must be the same.');
40+
}
41+
if (labNumberStr >= 3 && pr.data.head.ref !== labelToAdd) {
42+
core.setFailed(`The source branch must be '${labelToAdd}'`);
3943
}
4044
checklist-check:
4145
runs-on: ubuntu-latest
@@ -49,12 +53,12 @@ jobs:
4953
const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number });
5054
const body = pr.data.body;
5155
52-
const checkboxes = body.match(/\- \[[x ]\]/g);
56+
const checkboxes = body.match(/^ ?(-|\*) \[[x ]\]/gmi);
5357
if (!checkboxes || checkboxes.length !== 5) {
5458
core.setFailed('The PR description must contain exactly 5 checkboxes.');
5559
}
5660
57-
const unchecked = body.match(/\- \[ \]/g);
61+
const unchecked = body.match(/^ ?(-|\*) \[ \]/gm);
5862
if (unchecked && unchecked.length > 0) {
59-
core.setFailed(`There are ${unchecked.length} unchecked items in the PR description.`);
63+
core.setFailed(`There are ${unchecked.length} unchecked item(s) in the PR description.`);
6064
}

.github/workflows/lab-autograding.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Autograding
2+
3+
on:
4+
pull_request_target:
5+
types: [labeled, synchronize, opened, reopened, ready_for_review]
6+
7+
jobs:
8+
build:
9+
runs-on: ${{ matrix.os }}
10+
strategy:
11+
matrix:
12+
os: [ubuntu-22.04]
13+
fail-fast: false
14+
steps:
15+
- uses: actions/checkout@v4
16+
with:
17+
ref: "${{ github.event.pull_request.merge_commit_sha }}"
18+
fetch-depth: 1
19+
- uses: actions/setup-node@v4
20+
with:
21+
node-version: latest
22+
- name: Extract lab number and Check no changes other than specific files
23+
uses: actions/github-script@v5
24+
id: lab
25+
with:
26+
result-encoding: string
27+
github-token: ${{ secrets.GITHUB_TOKEN }}
28+
script: |
29+
const { owner, repo, number: issue_number } = context.issue;
30+
const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number });
31+
const labels = pr.data.labels;
32+
const lab = labels.find((label) => label.name.startsWith('lab'));
33+
if (!lab) {
34+
core.setFailed('No lab label found on the PR.');
35+
return { number: 0 };
36+
}
37+
const labNumberMatch = lab.name.match(/lab(\d+)/);
38+
if (!labNumberMatch) {
39+
core.setFailed('Invalid lab label found on the PR.');
40+
return { number: 0 };
41+
}
42+
const labNumber = labNumberMatch[1];
43+
console.log(`Lab number: ${labNumber}`)
44+
45+
const files = await github.rest.pulls.listFiles({ owner, repo, pull_number: issue_number });
46+
const changedFiles = files.data.map((file) => file.filename);
47+
const allowedFiles = [
48+
`lab${labNumber}/main_test.js`,
49+
];
50+
// if (!changedFiles.every((file) => allowedFiles.includes(file))) {
51+
// core.setFailed('The PR contains changes to files other than the allowed files.');
52+
// }
53+
return labNumber;
54+
- name: Grading
55+
run: |
56+
cd lab${{ steps.lab.outputs.result }}
57+
./validate.sh

.github/workflows/lab1.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

.github/workflows/lab2.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)