-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
195 lines (184 loc) · 6.11 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
let github = require('octonode');
let repos = [
''
];
function handler(event, context, callback) {
mergeStaging().then(() => {
console.log('complete');
callback();
}).catch(e => {
console.error(e)
})
}
function mergeStaging() {
let client = github.client(process.env.GITHUB_TOKEN);
return Promise.all(repos.map(repo => {
return new Promise((resolve, reject) => {
createPullRequest(client, repo, 'Auto Merge Staging', 'Time: ' + Date.now(), 'staging', 'master').then(pr => {
if (!pr) { // if no pr is returned, like for when there are no commits between the branches
console.log('Could not create pull request for repo: ' + repo + '. Possibly because there are no changes to merge');
resolve();
}
getIsPRMergeable(client, repo, pr.number).then(isMergeable => {
if (!isMergeable) {
reject(new Error('PULL REQUEST IS NOT MERGEABLE! Repo: ' + repo + ' PR#: ' + pr.number))
}
merge(client, repo, pr.head.ref, 'master', 'build: automerge staging').then(sha => {
console.log('merge sha', sha);
resolve()
}).catch(e => {
reject(e)
})
}).catch(e => {
reject(e)
})
}).catch(e => {
reject(e)
})
})
}))
}
/**
*
* @param client
* @param repo
* @param head
* @param base
* @returns {Promise<string>} return the commit hash of the merge result
*/
function merge(client, repo, head, base, commitMsg) {
console.log('merge: ', repo, head, base, commitMsg);
let ghrepo = client.repo(repo);
return new Promise(((resolve, reject) => {
ghrepo.merge({
head: head,
base: base,
commit_message: commitMsg
}, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data.sha)
});
}))
}
function getPullRequests(client, repo) {
let ghrepo = client.repo(repo);
return new Promise((resolve, reject) => {
callbackToPromise(ghrepo, 'prs').then(data => resolve(data)).catch(e => reject(e))
})
}
function getPullRequest(client, repo, prNumber) {
let pr = client.pr(repo, prNumber);
return new Promise((resolve, reject) => {
callbackToPromise(pr, 'info').then(data => {
resolve(data)
}).catch(e => reject(e))
})
}
/**
* If the PR already exists, will go and try to find it and return it's number
* @param client
* @param repo
* @param title
* @param body
* @param head
* @param base
* @returns {Promise<object>} returns the created pr
*/
function createPullRequest(client, repo, title, body, head, base) {
console.log('createPullRequest(): ', repo, title, body, head, base);
let ghrepo = client.repo(repo);
return new Promise((resolve, reject) => {
ghrepo.pr({
'title': title,
'body': body,
'head': head,
'base': base
}, (err, data) => {
if (err) {
if (!err.body || !err.body.errors) {
reject(err);
return
}
let fetch = err.body.errors.find(e => e.message && e.message.includes('A pull request already exists for'));
if (fetch) {
getPullRequests(client, repo).then(prs => {
let pr = prs.find(p => p.head.label.includes(head));
if (!pr) {
reject(new Error('Could not find the PR Github Said Existed with head: ' + head))
} else {
console.log('pr already exists');
resolve(pr);
}
})
} else {
let noDiff = err.body.errors.find(e => e.message && e.message.includes('No commits between master and'));
if (typeof noDiff !== 'undefined') {
// no error
resolve(null);
} else {
reject(octoNodeErrorToString(err))
}
}
} else {
resolve(data) // return the pr
}
})
});
}
function getIsPRMergeable(client, repo, prNumber) {
return new Promise((resolve, reject) => {
getPullRequest(client, repo, prNumber).then(data => {
if (data.mergeable === null) {
setTimeout(() => {
console.log('getIsPRMergeable: waiting 1 second');
getIsPRMergeable(client, repo, prNumber).then(d => resolve(d)).catch(e => reject(e))
}, 1000)
} else {
resolve(data.mergeable)
}
}).catch(e => reject(e));
})
}
function callbackToPromise(object, funcName, params = null) {
return new Promise((resolve, reject) => {
if (params) {
object[funcName](params, (err, data, headers) => {
if (err) {
reject(err)
}
resolve(data)
})
} else {
object[funcName]((err, data, headers) => {
if (err) {
reject(err)
}
resolve(data)
})
}
})
}
function octoNodeErrorToString(err) {
let es = '';
if (err.hasOwnProperty('message')) {
es += 'Message: ' + err.message + ' ';
}
if (err.hasOwnProperty('errors')) {
err.errors.forEach((e, i) => {
es += 'Error' + i + ': ' + JSON.stringify(e);
})
}
if (err.hasOwnProperty('documentation_url')) {
es += ' Doc URL: ' + err.documentation_url;
}
return es;
}
exports = Object.assign(exports, {
'handler': handler,
'getPullRequest': getPullRequest,
'createPullRequest': createPullRequest,
'mergeStaging': mergeStaging
});