-
Notifications
You must be signed in to change notification settings - Fork 8
/
dangerfile.js
81 lines (67 loc) · 3.88 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
import { danger, fail } from 'danger';
const { pr } = danger.github;
const validChangesetCategories = ['Added', 'Changed', 'Removed', 'Fixed'];
const isDependabotPR = pr.user.login === 'dependabot[bot]';
// Check for correct Changeset formatting
danger.git.created_files
.filter((filepath) => filepath.includes('.changeset/') && !filepath.includes('.changeset/pre.json'))
.forEach((filepath) => {
const changesetDiff = danger.git.diffForFile(filepath);
changesetDiff.then((result) => {
const diffString = result.diff;
const changesetCategoryRegex = /(?<=\[).+?(?=\])/g;
const changesetCategories = diffString.match(changesetCategoryRegex);
const numberOfCategories = changesetCategories ? changesetCategories.length : 0;
if (isDependabotPR) {
// Check if at least one of the valid changeset categories is present
if (numberOfCategories === 0) {
fail(`:memo: Your changeset doesn't include a category. Please add one of: \`${validChangesetCategories.join(', ')}\`. Filepath: \`${filepath}`);
} else if (!validChangesetCategories.some((cat) => changesetCategories.includes(cat))) {
fail(`:memo: Your changeset includes an invalid category. Please use one of: \`${validChangesetCategories.join(', ')}\`. Filepath: \`${filepath}`);
}
// Check that categories are followed are in the following format `[Category] - {Description}`
const changesetLineFormatRegex = /\[\w+\] - [\S].+/g;
const validChangesetEntries = diffString.match(changesetLineFormatRegex);
const numberOfValidChangesetEntries = validChangesetEntries ? validChangesetEntries.length : 0;
if (numberOfCategories !== numberOfValidChangesetEntries) {
fail(`:memo: Your changeset entries should be in the format: \`[Category] - {Description}\`. One or more of your entries does not follow this format. Filepath: \`${filepath}`);
}
}
}, (err) => {
console.error(err);
});
});
// Allow unchecked checkboxes only in "Not-applicable Checklist items"
if (!isDependabotPR) {
const { body } = pr;
const uncheckedCheckboxRegex = /- \[ \]/g;
// Split the body into sections
const sections = body.split(/## /);
const notApplicableSection = sections.find((section) => section.startsWith('Not-applicable Checklist items'));
// Check for unchecked checkboxes outside the "Not-applicable Checklist items" section
const uncheckedOutsideNotApplicableSection = sections.some((section) => {
if (section !== notApplicableSection) {
return uncheckedCheckboxRegex.test(section);
}
return false;
});
if (uncheckedOutsideNotApplicableSection) {
fail('You have unchecked checklist items outside the "Not-applicable Checklist items" section.\n\nPlease ensure all unchecked checkboxes are moved to the appropriate section.');
}
// Match sections for Reviewer 1 and Reviewer 2
const reviewer1Section = body.match(/### Reviewer 1.*?(?=###|$)/s);
const reviewer2Section = body.match(/### Reviewer 2.*?(?=###|$)/s);
// Helper function to check a reviewer's section
const checkReviewerSection = (section, reviewerName) => {
if (section) {
const uncheckedReviewerCheckboxes = section.match(uncheckedCheckboxRegex);
if (uncheckedReviewerCheckboxes) {
fail(`You have unchecked checklist items in ${reviewerName}'s section.\n\nPlease ensure all items are addressed before approval.`);
}
}
};
// Check Reviewer 1
checkReviewerSection(reviewer1Section ? reviewer1Section[0] : null, 'Reviewer 1');
// Check Reviewer 2
checkReviewerSection(reviewer2Section ? reviewer2Section[0] : null, 'Reviewer 2');
}