-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
192 lines (156 loc) · 6.21 KB
/
Copy pathindex.js
File metadata and controls
192 lines (156 loc) · 6.21 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
186
187
188
189
190
191
192
const CONFIG = {
allowedEvents: ['pull_request', 'pull_request_target'],
defaultLabel: 'safe to test',
};
async function run(modules = {}) {
const { core, github } = await resolveModules(modules);
try {
const context = resolveContext(core, github);
if (!context) return;
if (!isForkPullRequest(context)) {
core.info('Pull request is not from a fork. Assuming the code is safe.');
return;
}
const { labelName, isLabelPresent } = failIfLabelIsNotPresent(core, context);
if (!isLabelPresent) return;
core.info(`Pull request has the "${labelName}" label, changes are approved.`);
await removeLabelWhenRequired(core, github, context, labelName);
} catch (error) {
core.setFailed(getFailureMessage(error));
}
}
async function resolveModules(modules) {
const core = modules.core || await import('@actions/core');
const github = modules.github || await import('@actions/github');
return { core, github };
}
function resolveContext(core, github) {
const context = github.context || {};
if (!CONFIG.allowedEvents.includes(context.eventName)) {
core.info(`Event "${context.eventName}", skipping. This action only supports: ${CONFIG.allowedEvents.join(', ')}.`);
return null;
}
const payload = context.payload;
if (!isObject(payload) || !isObject(payload.pull_request)) {
throw new Error('Event payload does not include a pull_request object.');
}
return context;
}
function isForkPullRequest(context) {
const { headRepoFullName, baseRepoFullName } = getRepositoryNames(context);
return headRepoFullName !== baseRepoFullName;
}
function failIfLabelIsNotPresent(core, context) {
const labelName = normalizeLabel(core.getInput('label'));
const isLabelPresent = checkLabel(context, labelName);
if (!isLabelPresent) {
core.setFailed(
`Pull request does not have the "${labelName}" label. ` +
`Code owners must add the "${labelName}" label to the pull request before the workflow can run.`
);
return { labelName: undefined, isLabelPresent: false };
}
return { labelName, isLabelPresent: true };
}
async function removeLabelWhenRequired(core, github, context, labelName) {
const requiresReapproval = toBoolean(core.getInput('require-reapproval'));
if (requiresReapproval) {
const token = core.getInput('repo-token');
try {
await removeLabel({
context,
github,
token,
labelName,
});
core.info(`Removed the "${labelName}" label from pull request. Every change must be re-approved. Next workflow run requires the "${labelName}" label again.`);
} catch (error) {
if (isLabelAlreadyGoneError(error)) {
core.info('Label was removed during action execution, continuing.');
} else {
throw error;
}
}
}
}
function normalizeLabel(inputLabel) {
if (typeof inputLabel !== 'string') {
return CONFIG.defaultLabel;
}
const trimmed = inputLabel.trim();
return trimmed.length > 0 ? trimmed : CONFIG.defaultLabel;
}
function toBoolean(inputValue, defaultValue = true) {
if (typeof inputValue !== 'string') return defaultValue;
return inputValue.trim().toLowerCase() === 'true';
}
function isLabelAlreadyGoneError(error) {
return error?.message === 'Label does not exist' || error?.status === 404;
}
function isMissingIntegrationPermissionError(error) {
return error?.status === 403
&& typeof error?.message === 'string'
&& error.message.includes('Resource not accessible by integration');
}
function getFailureMessage(error) {
if (isMissingIntegrationPermissionError(error)) {
return 'Failed to remove label because the workflow token lacks required permissions. Ensure your workflow grants `contents: read` and `pull-requests: write`.';
}
return error instanceof Error ? error.message : String(error);
}
function getRepositoryNames(context) {
const payload = context.payload;
const pullRequest = payload.pull_request;
const headRepoFullName = pullRequest?.head?.repo?.full_name;
const baseRepoFullName = payload?.repository?.full_name ?? pullRequest?.base?.repo?.full_name;
if (!headRepoFullName || !baseRepoFullName) {
throw new Error('Unable to determine head/base repository names from the event payload.');
}
return { headRepoFullName, baseRepoFullName };
}
function checkLabel(context, labelName) {
const pullRequest = context.payload.pull_request;
if (!Array.isArray(pullRequest.labels)) {
return false;
}
return pullRequest.labels.some((label) => isObject(label) && label.name === labelName);
}
async function removeLabel({ context, github, token, labelName }) {
const { payload } = context;
const pullRequest = payload.pull_request;
const octokit = github.getOctokit(token);
const { owner, repo } = getOwnerAndRepo(context, payload);
const issueNumber = pullRequest?.number;
if (!issueNumber) {
throw new Error('Unable to determine pull request number from the event payload.');
}
await octokit.rest.issues.removeLabel({
owner,
repo,
issue_number: issueNumber,
name: labelName,
});
}
function getOwnerAndRepo(context, payload) {
if (context?.repo?.owner && context?.repo?.repo) {
return { owner: context.repo.owner, repo: context.repo.repo };
}
const fullName = payload?.repository?.full_name;
if (typeof fullName !== 'string' || !fullName.includes('/')) {
throw new Error('Unable to determine base repository owner/name from the event payload.');
}
const [owner, repo] = fullName.split('/', 2);
if (!owner || !repo) {
throw new Error('Unable to determine base repository owner/name from the event payload.');
}
return { owner, repo };
}
function isObject(value) {
return value !== null && typeof value === 'object';
}
// Export is only used for testing
module.exports = run;
/* istanbul ignore next -- direct CLI invocation is not exercised in Jest */
if (require.main === module) {
run();
}