-
Notifications
You must be signed in to change notification settings - Fork 279
100 lines (86 loc) · 3.3 KB
/
major-version-check.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
name: Major Version Check
on:
pull_request:
types: [opened, edited, synchronize]
issue_comment:
types: [created, edited]
permissions:
contents: read
issues: read
pull-requests: read
jobs:
check-major-bump:
runs-on: ubuntu-latest
steps:
- name: Check for major changesets
id: check_major
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
// Get list of files changed in the PR
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
// Check if any changeset files indicate a major bump
let hasMajorChangeset = false;
for (const file of files) {
if (file.filename.startsWith('.changeset/') && file.status !== 'removed') {
// Fetch the contents of the changeset file
const { data: changesetContent } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: file.filename,
ref: context.payload.pull_request.head.sha,
});
const content = Buffer.from(changesetContent.content, changesetContent.encoding).toString();
if (/major/.test(content)) {
hasMajorChangeset = true;
break;
}
}
}
core.setOutput('has_major_changeset', hasMajorChangeset);
- name: Check if major version bump is allowed
if: steps.check_major.outputs.has_major_changeset == 'true'
id: check_approval
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
const org = context.repo.owner;
// Get all comments on the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
let approvalFound = false;
for (const comment of comments) {
if (comment.body.trim().toLowerCase() === '!allow-major') {
const username = comment.user.login;
// Check if the user is a public member of the organization
try {
const { status } = await github.rest.orgs.checkMembershipForUser({
org,
username,
});
if (status === 204) {
approvalFound = true;
break;
}
} catch (error) {
if (error.status !== 204) {
// User is not a public member
continue;
}
}
}
}
if (!approvalFound) {
core.setFailed('Major version bump requires approval from an organization member by commenting "!allow-major" on the PR.');
} else {
console.log('Major version bump approved by an organization member.');
}