-
Notifications
You must be signed in to change notification settings - Fork 63
81 lines (73 loc) · 3.76 KB
/
PR.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
name: PR automation
on:
pull_request_target:
types: [opened, reopened, edited, ready_for_review]
jobs:
labeler:
runs-on: ubuntu-latest
steps:
- name: Label PR
uses: actions/github-script@v5
with:
github-token: ${{ secrets.PAT }}
script: |
const { owner, repo, number: issue_number } = context.issue;
const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number });
const title = pr.data.title;
const labRegex = /\[LAB(\d+)\]/;
const titleRegex = /^\[LAB\d+\] [a-zA-Z]?\d+$/;
if (!titleRegex.test(title)) {
core.setFailed('PR title does not match the required format. Please use the format [LAB#] student#.');
}
if (pr.data.head.ref !== pr.data.base.ref) {
core.setFailed('The source branch and target branch must be the same.');
}
if (pr.data.base.ref === 'main') {
core.setFailed('The target branch cannot be main.');
}
const match = title.match(labRegex);
if (match) {
const labelToAdd = 'lab' + match[1];
await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] });
} else {
core.setFailed('No match found in PR title. Please add a label in the format [LAB#] to the PR title.');
}
checklist-check:
runs-on: ubuntu-latest
steps:
- name: Check PR description
uses: actions/github-script@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo, number: issue_number } = context.issue;
const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number });
const body = pr.data.body;
const checkboxes = body.match(/^ ?(-|\*) \[[x ]\]/gi);
if (!checkboxes || checkboxes.length !== 5) {
core.setFailed('The PR description must contain exactly 5 checkboxes.');
}
const unchecked = body.match(/^ ?(-|\*) \[ \]/g);
if (unchecked && unchecked.length > 0) {
core.setFailed(`There are ${unchecked.length} unchecked items in the PR description.`);
}
changed-files-check:
runs-on: ubuntu-latest
steps:
- name: Check no changes other than specific files
uses: actions/github-script@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo, number: issue_number } = context.issue;
const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number });
const files = await github.rest.pulls.listFiles({ owner, repo, pull_number: issue_number });
const changedFiles = files.data.map((file) => file.filename);
echo changedFiles;
const allowedFiles = [
'lab1/main_test.js',
'lab2/main_test.js',
];
if (!changedFiles.every((file) => allowedFiles.includes(file))) {
core.setFailed('The PR contains changes to files other than the allowed files.');
}