-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathdangerfile.ts
140 lines (117 loc) · 4.03 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
/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */
// @ts-nocheck
function handleMultipleFileChanges(gitChanges: GitDSL) {
fail(
"This PR requires a manual review because you are changing more files than just `src/assets/data.json`."
);
}
function hasOnlyCartChange(gitChanges: GitDSL) {
return (
gitChanges.modified_files.length === 1 &&
gitChanges.modified_files[0] === "src/assets/data.json" &&
gitChanges.created_files.length === 0 &&
gitChanges.deleted_files.length === 0
);
}
function hasOperation(diffs: JSONPatchOperation[], operation: string) {
for (const diff of diffs) {
if (diff.op === operation) {
return true;
}
}
return false;
}
function array2Set(data: Cart[]): Set<any> {
const outputSet = new Set();
data.forEach((cart: Cart) => outputSet.add(JSON.stringify(cart)));
return outputSet;
}
function evaluateChanges(changes: JSONPatch) {
const isDiffEmpty = changes.diff.length === 0;
if (isDiffEmpty) {
fail("This PR appears to be empty.");
}
const beforeCarts: Set<string> = array2Set(changes.before);
const afterCarts: Set<string> = array2Set(changes.after);
const addsMultipleCarts = afterCarts.size - beforeCarts.size > 1;
if (addsMultipleCarts) {
fail(`You can not add more than one cart.`);
}
const removedCarts = changes.before.filter(
(cart: Cart) => !afterCarts.has(JSON.stringify(cart))
);
if (removedCarts.length > 0) {
const removedUserNames = removedCarts.map((el: string) => {
console.log("el: ", el);
return JSON.parse(el).name;
});
fail(
"It seems like you are accidentally deleting or editing some contributions of others. Please make sure you have pulled the latest changes from the master branch and resolved any merge conflicts. https://help.github.com/en/articles/syncing-a-fork"
);
}
const gitHubUsername = danger.github?.pr?.user?.login;
const cartUsername = changes.after.filter(
(cart: Cart) => cart.name.toLowerCase() === gitHubUsername?.toLowerCase()
);
if (cartUsername.length > 1) {
fail(
stripIndents`You cannot create more than one cart per GitHub username.`
);
}
const newCart = changes.after.find(
(cart: Cart) => !beforeCarts.has(JSON.stringify(cart))
);
if (newCart.text.length > 22) {
fail(stripIndents`Your message is too long`);
}
if (
danger.github &&
newCart.name.toLowerCase() !== gitHubUsername?.toLowerCase()
) {
fail(stripIndents`You cannot create cart for other GitHub users.`);
}
const addOperations = changes.diff.filter(x => x.op === "add");
if (addOperations.length > 1) {
fail(
"It seems like you are adding more than one cart. This will require a manual review to make sure this is not a mistake."
);
}
if (
hasOperation(changes.diff, "remove") ||
hasOperation(changes.diff, "replace")
) {
fail(
"It seems like you are accidentally deleting some contributions of others. Please make sure you have pulled the latest changes from the master branch and resolved any merge conflicts. https://help.github.com/en/articles/syncing-a-fork"
);
}
return true;
}
async function run() {
// if (danger.github.thisPR) {
try {
if ((await danger.git.linesOfCode()) === 0) {
fail("This PR is empty. Read README.md.");
} else if (!hasOnlyCartChange(danger.git)) {
await handleMultipleFileChanges(danger.git);
} else {
const jsonPatch = await danger.git.JSONPatchForFile(
"src/assets/data.json"
);
if (!jsonPatch) {
fail("This PR appears to be empty.");
}
const passed = await evaluateChanges(jsonPatch);
if (passed) {
message(
"🚂 Thank you for submitting your cart. Should be online soon."
);
}
}
} catch (error) {
fail(JSON.stringify(error));
}
// }
}
run().catch(console.error);
export { handleMultipleFileChanges, hasOnlyCartChange };