-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (135 loc) · 5.1 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
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
const axios = require('axios');
const checker = require('./lib/markdownChecker');
const {
Wring
} = require('wring-js');
const wring = new Wring();
const strings = wring.load('./lib/strings.yml', __dirname);
module.exports = app => {
app.log('Yay, the app was loaded!');
// comment when new issue is opened
app.on('issues.opened', async context => {
const issueComment = context.issue({
body: strings.get('newIssue')
});
await context.github.issues.addLabels(context.issue({
labels: ["Awaiting Review"]
}));
return context.github.issues.createComment(issueComment);
});
app.on('status', async context => {
//console.log(JSON.stringify(context));
});
app.on(['pull_request.opened', 'pull_request.reopened'], async context => {
const pullRequest = context.payload.pull_request;
const creator = pullRequest.user.login;
const number = pullRequest.number;
// get the changed file
const filesResponse = await context.github.pullRequests.listFiles(context.issue({
number: number
}));
if (filesResponse.data.length == 1) {
let file = filesResponse.data[0];
// Pull request is added for profile submission, add the label
if (file.filename.match(/^src\/profiles\//g)) {
// Add submission label to the Pull request
await context.github.issues.addLabels(context.issue({
labels: ["submission"],
number: number
}));
// Add a comment on the pull request
const submissionComment = context.issue({
body: strings.with('newSubmission').format({
username: creator
})
});
await context.github.issues.createComment(submissionComment);
// retrieve the file and check for proper syntax
const contents = await axios.get(file.raw_url);
console.log(contents.data);
let check_results = checker(contents.data, creator );
context.github.issues.createComment(context.issue({
number: number,
body: check_results.message
}));
// Okay this is kinda wiered API response here
// the request will result in 404 if the PR isn't merged
// and 204 if the PR is merged
if (check_results.merge) {
// Add approved label to the Pull request
await context.github.issues.addLabels(context.issue({
labels: ["approved"],
number: number
}));
try {
await context.github.pullRequests.checkIfMerged(context.issue({
number: number
}));
console.log("Pull request already merged")
} catch (error) {
console.log("Pull request not merged");
// Let's merge it! Yaay
try {
// Status 200 if merged, else unable to merge
await context.github.pullRequests.merge(context.issue({
number: number
}));
let message = strings.with('successfullyMerged').format({
username: creator
});
return await context.github.issues.createComment(context.issue({
number: number,
body: message
}));
} catch (error) {
// Unable to merge the PR, maybe a merge conflict.
// https://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged
console.log(error);
console.log(JSON.stringify(error));
// Add approved label to the Pull request
await context.github.issues.addLabels(context.issue({
labels: ["review required"],
number: number
}));
let message = strings.get('unableToMergePR');
context.github.issues.createComment(context.issue({
number: number,
body: message
}));
}
}
}
}
}
});
app.on('pull_request.merged', async context => {
const pullRequest = context.payload.pull_request;
const creator = pullRequest.user.login;
console.log(JSON.stringify(context));
console.log("Successfully merged the PullRequest!")
console.log("Sending Congratulations!")
let message = strings.with('successfullyMerged').format({
username: creator
});
context.github.issues.createComment(context.issue({
number: number,
body: message
}));
});
// app.on('check_run.completed', async context => {
// console.log(JSON.stringify(context));
// const checkSuite = context.payload.check_run;
// const checksPRData = await context.github.checks.getSuite(context.issue({
// check_suite_id: checkSuite.id
// }));
// console.log(JSON.stringify(checksPRData));
// // check if the checks result is success
// if (checkSuite.conclusion !== "success") {
// console.error("CheckSuite Failed!");
// return;
// };
// // Checks are all Green, Submit the deploy preview URL
// console.log("CheckSuite Success. Creating deploy URL comment...");
// //get the PR Number
// });
}