-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdangerfile.js
196 lines (167 loc) · 5.85 KB
/
dangerfile.js
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
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 = (function () {
if (danger.github && danger.github.pr) {
if (danger.github.pr.title) {
const parts = danger.github.pr.title.split(":");
if (parts.length > 1) {
return parts[0].toLowerCase().trim();
}
}
if (danger.github.pr.head && danger.github.pr.head.ref) {
const parts = danger.github.pr.head.ref.split("/");
if (parts.length > 1) {
return parts[0].toLowerCase();
}
}
}
return "";
})();
console.log(`::debug:: PR Flavor: '${prFlavor}'`);
async function checkDocs() {
if (prFlavor.startsWith("feat")) {
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";
// Check if skipped
if (
["ci", "test", "deps", "chore(deps)", "build(deps)"].includes(prFlavor) ||
(danger.github.pr.body + "").includes("#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(/\.+$/, "");
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
- ${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.`.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 checkAll() {
await checkDocs();
await checkChangelog();
await checkActionsArePinned();
}
schedule(checkAll);