-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdangerfile.js
More file actions
237 lines (201 loc) · 7.73 KB
/
dangerfile.js
File metadata and controls
237 lines (201 loc) · 7.73 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const { getFlavorConfig, extractPRFlavor } = require('./dangerfile-utils.js');
const headRepoName = danger.github.pr.head.repo.git_url;
const baseRepoName = danger.github.pr.base.repo.git_url;
const isFork = headRepoName != baseRepoName;
if (isFork) {
console.log(
"::warning::Running from a forked repo. Danger won't be able to post comments and workflow status on the main repo, printing directly."
);
// Override DangerJS default functions to print to console & create annotations instead.
const log = function (type, message, file, line) {
message = message.replace(/%/g, "%25");
message = message.replace(/\n/g, "%0A");
message = message.replace(/\r/g, "%0D");
console.log(`::${type} file=${file},line=${line}::${message}`);
};
const dangerFail = fail;
fail = function (message, file, line) {
log("error", message, file, line);
dangerFail(message, file, line);
};
warn = function (message, file, line) {
log("warning", message, file, line);
};
message = function (message, file, line) {
log("notice", message, file, line);
};
markdown = function (message, file, line) {
log("notice", message, file, line);
};
}
// e.g. "feat" if PR title is "Feat : add more useful stuff"
// or "ci" if PR branch is "ci/update-danger"
const prFlavor = extractPRFlavor(
danger.github?.pr?.title,
danger.github?.pr?.head?.ref
);
console.log(`::debug:: PR Flavor: '${prFlavor}'`);
async function checkDocs() {
const flavorConfig = getFlavorConfig(prFlavor);
if (flavorConfig.isFeature) {
message(
'Do not forget to update <a href="https://github.com/getsentry/sentry-docs">Sentry-docs</a> with your feature once the pull request gets approved.'
);
}
}
async function checkChangelog() {
const changelogFile = "CHANGELOG.md";
const flavorConfig = getFlavorConfig(prFlavor);
// Check if skipped - either by flavor config, explicit skip, or skip label
if (
flavorConfig.changelog === undefined ||
(danger.github.pr.body + "").includes("#skip-changelog") ||
(danger.github.pr.labels || []).some(label => label.name === 'skip-changelog')
) {
return;
}
// Check if current PR has an entry in changelog
const changelogContents = await danger.github.utils.fileContents(
changelogFile
);
const changelogMatch = RegExp(`^(.*?)\n[^\n]+(\\(${danger.github.pr.html_url}\\)|#${danger.github.pr.number}\\b)`, 's').exec(
changelogContents
);
// check if a changelog entry exists
if (!changelogMatch) {
return reportMissingChangelog(changelogFile);
}
// Check if the entry is added to an Unreleased section (or rather, check that it's not added to a released one)
const textBeforeEntry = changelogMatch[1]
const section = RegExp('^(## +v?[0-9.]+)([\-\n _]|$)', 'm').exec(textBeforeEntry)
if (section) {
const lineNr = 1 + textBeforeEntry.split(/\r\n|\r|\n/).length
fail(
`The changelog entry seems to be part of an already released section \`${section[1]}\`.
Consider moving the entry to the \`## Unreleased\` section, please.`,
changelogFile,
lineNr
);
}
}
/// Report missing changelog entry
function reportMissingChangelog(changelogFile) {
fail("Please consider adding a changelog entry for the next release.", changelogFile);
const prTitleFormatted = danger.github.pr.title
.split(": ")
.slice(-1)[0]
.trim()
.replace(/\.+$/, "");
// Determine the appropriate section based on PR flavor
const flavorConfig = getFlavorConfig(prFlavor);
const sectionName = flavorConfig.changelog || "Features";
markdown(
`
### Instructions and example for changelog
Please add an entry to \`${changelogFile}\` to the "Unreleased" section. Make sure the entry includes this PR's number.
Example:
\`\`\`markdown
## Unreleased
### ${sectionName}
- ${prTitleFormatted} ([#${danger.github.pr.number}](${danger.github.pr.html_url}))
\`\`\`
If none of the above apply, you can opt out of this check by adding \`#skip-changelog\` to the PR description or adding a \`skip-changelog\` label.`.trim(),
changelogFile
);
}
async function checkActionsArePinned() {
const workflowFiles = danger.git.created_files
.concat(danger.git.modified_files)
.filter((path) => path.startsWith(".github/workflows/"));
if (workflowFiles.length == 0) {
return;
}
console.log(
`::debug:: Some workflow files have been changed - checking whether actions are pinned: ${workflowFiles}`
);
const usesRegex = /^\+? *uses:/;
const usesActionRegex =
/^\+? *uses: *(?<user>[^\/]+)\/(?<action>[^@]+)@(?<ref>[^\s]+)/;
const usesLocalRegex = /^\+? *uses: *\.\//; // e.g. 'uses: ./.github/actions/something'
const shaRegex = /^[a-f0-9]{40}$/;
const whitelistedUsers = ["getsentry", "actions", "github"];
for (const path of workflowFiles) {
const diff = await danger.git.structuredDiffForFile(path);
for (const chunk of diff.chunks) {
for (const change of chunk.changes) {
if (change.add) {
const line = change.content;
const match = line.match(usesActionRegex);
// Example of `match.groups`:
// [Object: null prototype] {
// user: 'getsentry',
// action: 'action-prepare-release',
// ref: 'v1'
// }
if (match && match.groups) {
if (!match.groups.ref.match(shaRegex)) {
if (!whitelistedUsers.includes(match.groups.user)) {
fail(
"Please pin the action by specifying a commit SHA instead of a tag/branch.",
path,
change.ln
);
}
}
} else if (line.match(usesRegex) && !line.match(usesLocalRegex)) {
warn(
"Couldn't parse 'uses:' declaration while checking for action pinning.",
path,
change.ln
);
}
}
}
}
}
}
async function checkFromExternalChecks() {
// Get the external dangerfile path from environment variable (passed via workflow input)
// Priority: EXTRA_DANGERFILE (absolute path) -> EXTRA_DANGERFILE_INPUT (relative path)
const extraDangerFilePath = process.env.EXTRA_DANGERFILE || process.env.EXTRA_DANGERFILE_INPUT;
console.log(`::debug:: Checking from external checks: ${extraDangerFilePath}`);
if (extraDangerFilePath) {
try {
const workspaceDir = '/github/workspace';
const path = require('path');
const fs = require('fs');
const customPath = path.join(workspaceDir, extraDangerFilePath);
// Ensure the resolved path is within workspace
const resolvedPath = fs.realpathSync(customPath);
if (!resolvedPath.startsWith(workspaceDir)) {
fail(`Invalid dangerfile path: ${extraDangerFilePath}. Must be within workspace.`);
throw new Error('Security violation: dangerfile path outside workspace');
}
const extraModule = require(customPath);
if (typeof extraModule !== 'function') {
warn(`EXTRA_DANGERFILE must export a function at ${customPath}`);
return;
}
await extraModule({
fail: fail,
warn: warn,
message: message,
markdown: markdown,
danger: danger,
});
} catch (err) {
if (err.message && err.message.includes('Cannot use import statement outside a module')) {
warn(`External dangerfile uses ES6 imports. Please convert to CommonJS syntax (require/module.exports) or use .mjs extension with proper module configuration.\nFile: ${extraDangerFilePath}`);
} else {
warn(`Could not load custom Dangerfile: ${extraDangerFilePath}\n${err}`);
}
}
}
}
async function checkAll() {
await checkDocs();
await checkChangelog();
await checkActionsArePinned();
await checkFromExternalChecks();
}
schedule(checkAll);