-
-
Notifications
You must be signed in to change notification settings - Fork 326
185 lines (160 loc) · 5.96 KB
/
Copy pathpr-scope-governance.yml
File metadata and controls
185 lines (160 loc) · 5.96 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
name: pr-scope-governance
on:
pull_request:
types: [opened, reopened, edited, synchronize, ready_for_review]
permissions:
contents: read
issues: write
pull-requests: write
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
apply-scope-metadata:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}
- uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const marker = '<!-- pr-scope-governance -->';
const config = JSON.parse(
fs.readFileSync('.github/pr-scope-map.json', 'utf8')
);
const pr = context.payload.pull_request;
const changedFiles = await github.paginate(
github.rest.pulls.listFiles,
{
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
per_page: 100,
}
);
const filenames = changedFiles.map((file) => file.filename);
const matches = config.areas.filter((area) => {
const prefixes = area.prefixes ?? [];
const files = area.files ?? [];
return filenames.some((filename) => {
return (
prefixes.some((prefix) => filename.startsWith(prefix)) ||
files.includes(filename)
);
});
});
const matchedLabels = [...new Set(matches.map((area) => area.label))];
const managedLabels = config.areas.map((area) => area.label);
for (const area of config.areas) {
try {
await github.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: area.label,
});
} catch (error) {
if (error.status !== 404) {
throw error;
}
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: area.label,
color: area.color,
description: area.description,
});
}
}
const issueLabels = await github.paginate(
github.rest.issues.listLabelsOnIssue,
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
}
);
const currentManaged = issueLabels
.map((label) => label.name)
.filter((label) => managedLabels.includes(label));
const labelsToAdd = matchedLabels.filter(
(label) => !currentManaged.includes(label)
);
const labelsToRemove = currentManaged.filter(
(label) => !matchedLabels.includes(label)
);
if (labelsToAdd.length > 0) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: labelsToAdd,
});
}
for (const label of labelsToRemove) {
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
name: label,
});
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
}
const packageMatches = matches.filter(
(area) => area.kind === 'package'
);
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
}
);
const existingComment = comments.find(
(comment) =>
comment.user?.type === 'Bot' &&
comment.body?.includes(marker)
);
if (packageMatches.length > 1) {
const scopes = packageMatches
.map((area) => `\`${area.scope}\``)
.join(', ');
const body = [
marker,
`This PR touches multiple package scopes: ${scopes}.`,
'',
'Please confirm the changes are closely related. `Squash merge` is highly preferred. If you recommend a non-squash merge, add a brief note explaining why the commit boundaries matter and why this PR should bypass focused changes per package.',
].join('\n');
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body,
});
}
} else if (existingComment) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
});
}
core.info(`Changed files: ${filenames.join(', ')}`);
core.info(`Matched labels: ${matchedLabels.join(', ') || 'none'}`);