-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
dangerfile.ts
142 lines (110 loc) · 4.73 KB
/
dangerfile.ts
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
import { markdown, danger, warn, fail } from "danger";
const BIG_PR_THRESHOLD = 1000;
const LONG_COMMIT_MESSAGE_THRESHOLD = 125;
function generateCollapsibleList(items: string[], summary: string): string {
return `
<details>
<summary>${summary}</summary>
<ul>
${items.map((item) => `<li>${danger.github.utils.fileLinks([item], false)}</li>`).join("\n")}
</ul>
</details>
`;
}
function getPrefixSymbol(val: number) {
return val > 0 ? "🔴 " : val < 0 ? "🟢 " : "🟡 ";
}
function formatByteStr(bytes: number, addPrefix = false) {
const [kiloBytes, megaBytes, gigaBytes] = [1, 2, 3].map((exp) => 1e3 ** exp);
const outputStr =
bytes < kiloBytes
? bytes + " B"
: bytes < megaBytes
? (bytes / kiloBytes).toFixed(2) + " KB"
: bytes < gigaBytes
? (bytes / megaBytes).toFixed(2) + " MB"
: (bytes / gigaBytes).toFixed(2) + " GB";
return `${addPrefix ? getPrefixSymbol(bytes) : ""}${outputStr}`;
}
(async function () {
const packageChanged = danger.git.modified_files.includes("package.json");
const lockfileChanged = danger.git.modified_files.includes("pnpm-lock.yaml");
const PACKAGE_CHECK_MESSAGE = `Changes found in package.json, but not in pnpm-lock.yaml - <i>'Forgot to run \`pnpm i\`?'</i>`;
const linesAdded = danger.github.pr.additions;
const linesDeleted = danger.github.pr.deletions;
const BIG_PR_MESSAGE = `Big PR (# added: <b>${linesAdded}</b>, # deleted: <b>${linesDeleted}</b>), please keep your PR small to make it easier to review`;
const malformedCommits = danger.git.commits.filter((commit) => {
const subject = commit.message.split("\n")[0];
return (
(!subject.match(/^(feat|fix|build|chore|ci|style|refactor|perf|test|docs)(\(.+?\))?:/i) &&
!subject.includes("Merge pull request")) ||
subject.length > LONG_COMMIT_MESSAGE_THRESHOLD
);
});
const MALFORMED_COMMIT_MESSAGE = `
Some commit messages do not match the expected format (helps us generate changelogs).
<details>
<summary>Violating Commits</summary>
<ul>
${malformedCommits
.map((item) => `<li>${item.message.split("\n")[0]} (${item.sha.slice(0, 7)})</li>`)
.join("\n")}
</ul>
</details>
See ${danger.utils.href(
"https://www.conventionalcommits.org/en/v1.0.0/",
"Conventional Commits"
)} for expected formatting.
`;
const filesChanged = [...danger.git.modified_files, ...danger.git.created_files, ...danger.git.deleted_files];
const fileSizeMapper: Record<"filename" | "base" | "current" | "diff" | "percent", string>[] = [];
const totalChange: Record<"base" | "current" | "diff", number> = { base: 0, current: 0, diff: 0 };
for (const file of filesChanged) {
if (file !== "pnpm-lock.yaml") {
const { before, after } = await danger.git.diffForFile(file);
const unMinifiedSizeDiffInBytes = after.length - before.length;
const percentageChanged = before.length === 0 ? 100 : (unMinifiedSizeDiffInBytes * 100) / before.length;
totalChange.base += before.length;
totalChange.current += after.length;
totalChange.diff += unMinifiedSizeDiffInBytes;
fileSizeMapper.push({
filename: file,
base: formatByteStr(before.length),
current: formatByteStr(after.length),
diff: formatByteStr(unMinifiedSizeDiffInBytes, true),
percent: `${getPrefixSymbol(percentageChanged)}${percentageChanged.toFixed(2)}%`
});
}
}
const totalPercentage = (totalChange.diff * 100) / totalChange.base;
// MESSAGES
if (packageChanged && !lockfileChanged) warn(PACKAGE_CHECK_MESSAGE);
if (linesAdded + linesDeleted > BIG_PR_THRESHOLD && !lockfileChanged) warn(BIG_PR_MESSAGE);
if (malformedCommits.length > 0) fail(MALFORMED_COMMIT_MESSAGE);
markdown(`
## PR Snapshot
Comparing: ${danger.git.base.slice(0, 7)}...${danger.git.head.slice(0, 7)}
<details>
<summary>Insights</summary>
<b>Note:</b> the following excludes changes to \`pnpm-lock.yaml\` due to its volatile nature.
| Filename | Base | Current | +/- | % |
|---------:|:----:|:-------:|:---|:---|
${fileSizeMapper
.map(
(item) =>
`| ${danger.github.utils.fileLinks([item.filename], false)} | ${item.base} | ${item.current} | ${item.diff} | ${
item.percent
} |`
)
.join("\n")}
| Total | ${formatByteStr(totalChange.base)} | ${formatByteStr(totalChange.current)} | ${formatByteStr(
totalChange.diff,
true
)} | ${getPrefixSymbol(totalPercentage)}${totalPercentage.toFixed(2)}% |
</details>
### Summary
${generateCollapsibleList(danger.git.modified_files, "Changed Files")}
${generateCollapsibleList(danger.git.created_files, "Created Files")}
${generateCollapsibleList(danger.git.deleted_files, "Deleted Files")}
`);
})();