-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (102 loc) · 2.92 KB
/
index.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
const axios = require("axios");
const diffParse = require("parse-git-diff");
const { exec } = require("child_process");
/**
* @param {import('probot').Probot} app
*/
module.exports = (app) => {
app.log.info("Yay, the app was loaded!");
app.on(
["pull_request.opened", "pull_request.synchronize"],
async (context) => {
const diffUrl = context.payload.pull_request.diff_url;
const res = await axios.get(diffUrl);
const parsed = diffParse(res.data);
if (
parsed.files.length != 1 ||
parsed.files[0].path != process.env.CHANGE_FILE
) {
return context.octokit.issues.createComment(
context.issue({
body: `Please only edit ${process.env.CHANGE_FILE}. You can make a commit to undo the invalid changes.`,
})
);
}
const diff = parsed.files[0];
// check for invalid changes
for (const chunk of diff.chunks) {
for (const change of chunk.changes) {
if (change.type !== "UnchangedLine" && change.type !== "AddedLine") {
return context.octokit.issues.createComment(
context.issue({
body: `Please only add new lines. Raise an issue if you'd like to remove a line.`,
})
);
}
if (change.type === "AddedLine") {
const line = change.content;
//prevent imports
if (
/\s*import\s+.*\s*.*/.test(line) ||
/\s*from\s+.*\s+import\s+.*/.test(line) ||
/\s*done\(\)\s*.*/.test(line)
) {
return context.octokit.issues.createComment(
context.issue({
body: `Might wanna take a look at the rules.`,
})
);
}
}
}
}
const prRepo = context.payload.pull_request.head.repo.html_url;
const branch = context.payload.pull_request.head.ref;
//check for execution time
const pixelDiff = await new Promise((resolve, reject) => {
exec(`./test.sh "${prRepo}" "${branch}"`, (_, stdout) => {
resolve(Number.parseInt(stdout));
});
});
if (pixelDiff > 50000) {
return context.octokit.issues.createComment(
context.issue({
body: `Please take care not edit more than 10000 pixels per commit.`,
})
);
}
if (pixelDiff == -1) {
return context.octokit.issues.createComment(
context.issue({
body: `draw.js has errors. Please fix.`,
})
);
}
if (pixelDiff == -2) {
return context.octokit.issues.createComment(
context.issue({
body: `Please remove any long running/infinite loops.`,
})
);
}
try {
const mergeRes = await context.octokit.pulls.merge(
context.repo({
pull_number: context.payload.pull_request.number ,
merge_method: 'squash',
})
);
exec("./updatesite.sh")
// exec("(cp /tmp/screenshot1.png /home/cliford/place/canvas.png)");
return mergeRes;
} catch (err) {
console.log(err.message)
return context.octokit.issues.createComment(
context.issue({
body: `Please resolve all merge conflicts.`,
})
);
}
}
);
};