@@ -16,32 +16,27 @@ github.authenticate({
1616} )
1717
1818module . exports = {
19-
20- github,
21-
2219/**
2320 * issue 是否包含某 label
2421 *
2522 *@param {Object } payload data
2623 *@param {string } body 评论内容
24+ *@return {boolean }
2725 */
2826async issueHasLabel ( payload , label ) {
2927const owner = payload . repository . owner . login
3028const repo = payload . repository . name
3129const number = payload . issue . number
3230
3331try {
34- const res = github . issues . getIssueLabels ( {
32+ const res = await github . issues . getIssueLabels ( {
3533 owner,
3634 repo,
3735 number
3836} )
39-
40- if ( res . data . map ( v => v . name ) . indexOf ( label ) === - 1 ) {
41- Promise . reject ( new Error ( 'issue no label' ) )
42- }
37+ return res . data . map ( v => v . name ) . indexOf ( label ) > - 1
4338} catch ( e ) {
44- Promise . reject ( e )
39+ return false
4540}
4641} ,
4742
@@ -50,6 +45,7 @@ module.exports = {
5045 *
5146 *@param {Object } payload data
5247 *@param {string } body 评论内容
48+ *@return {boolean }
5349 */
5450async pullRequestHasLabel ( payload , label ) {
5551const owner = payload . repository . owner . login
@@ -62,11 +58,9 @@ module.exports = {
6258 repo,
6359 number
6460} )
65- if ( res . data . map ( v => v . name ) . indexOf ( label ) === - 1 ) {
66- Promise . reject ( new Error ( 'pull request no label' ) )
67- }
61+ return res . data . map ( v => v . name ) . indexOf ( label ) > - 1
6862} catch ( e ) {
69- Promise . reject ( e )
63+ return false
7064}
7165} ,
7266
@@ -75,22 +69,23 @@ module.exports = {
7569 *
7670 *@param {Object } payload data
7771 *@param {string } body 评论内容
72+ *@return {boolean } 是否成功
7873 */
7974async commentIssue ( payload , body ) {
8075const owner = payload . repository . owner . login
8176const repo = payload . repository . name
8277const number = payload . issue . number
8378
8479try {
85- const res = await github . issues . createComment ( {
80+ await github . issues . createComment ( {
8681 owner,
8782 repo,
8883 number,
8984 body
9085} )
91- return res
86+ return true
9287} catch ( e ) {
93- Promise . reject ( e )
88+ return false
9489}
9590} ,
9691
@@ -99,45 +94,47 @@ module.exports = {
9994 *
10095 *@param {Object } payload data
10196 *@param {string } body 评论内容
97+ *@return {boolean } 是否成功
10298 */
10399async commentPullRequest ( payload , body ) {
104100const owner = payload . repository . owner . login
105101const repo = payload . repository . name
106102const number = payload . pull_request . number
107103
108104try {
109- const res = await github . issues . createComment ( {
105+ await github . issues . createComment ( {
110106 owner,
111107 repo,
112108 number,
113109 body
114110} )
115- return res
111+ return true
116112} catch ( e ) {
117- Promise . reject ( e )
113+ return false
118114}
119115} ,
120116
121117/**
122118 * 关闭 issue
123119 *
124120 *@param {Object } payload data
121+ *@return {boolean } 是否成功
125122 */
126123async closeIssue ( payload ) {
127124const owner = payload . repository . owner . login
128125const repo = payload . repository . name
129126const number = payload . issue . number
130127
131128try {
132- const res = await github . issues . edit ( {
129+ await github . issues . edit ( {
133130 owner,
134131 repo,
135132 number,
136133state :'closed'
137134} )
138- return res
135+ return true
139136} catch ( e ) {
140- Promise . reject ( e )
137+ return false
141138}
142139} ,
143140
@@ -146,22 +143,23 @@ module.exports = {
146143 *
147144 *@param {Object } payload data
148145 *@param {string | Array } assign 用户id
146+ *@return {boolean } 是否成功
149147 */
150148async addAssigneesToIssue ( payload , assign ) {
151149const owner = payload . repository . owner . login
152150const repo = payload . repository . name
153151const number = payload . issue . number
154152
155153try {
156- const res = await github . issues . edit ( {
154+ await github . issues . edit ( {
157155 owner,
158156 repo,
159157 number,
160- assignees :Array . isArray ( assign ) ? assign : [ assign ]
158+ assignees :toArray ( assign )
161159} )
162- return res
160+ return true
163161} catch ( e ) {
164- Promise . reject ( e )
162+ return false
165163}
166164} ,
167165
@@ -170,22 +168,23 @@ module.exports = {
170168 *
171169 *@param {Object } payload data
172170 *@param {string | Array } labels 标签
171+ *@return {boolean } 是否成功
173172 */
174173async addLabelsToIssue ( payload , labels ) {
175174const owner = payload . repository . owner . login
176175const repo = payload . repository . name
177176const number = payload . issue . number
178177
179178try {
180- const res = await github . issues . addLabels ( {
179+ await github . issues . addLabels ( {
181180 owner,
182181 repo,
183182 number,
184- labels :Array . isArray ( labels ) ? labels : [ labels ]
183+ labels :toArray ( labels )
185184} )
186- return res
185+ return true
187186} catch ( e ) {
188- Promise . reject ( e )
187+ return false
189188}
190189} ,
191190
@@ -194,22 +193,23 @@ module.exports = {
194193 *
195194 *@param {Object } payload data
196195 *@param {string | Array } labels 标签
196+ *@return {boolean } 是否成功
197197 */
198198async addLabelsToPullRequest ( payload , labels ) {
199199const owner = payload . repository . owner . login
200200const repo = payload . repository . name
201201const number = payload . pull_request . number
202202
203203try {
204- const res = await github . issues . addLabels ( {
204+ await github . issues . addLabels ( {
205205 owner,
206206 repo,
207207 number,
208- labels :Array . isArray ( labels ) ? labels : [ labels ]
208+ labels :toArray ( labels )
209209} )
210- return res
210+ return true
211211} catch ( e ) {
212- Promise . reject ( e )
212+ return false
213213}
214214} ,
215215
@@ -218,22 +218,23 @@ module.exports = {
218218 *
219219 *@param {Object } payload data
220220 *@param {string } name 标签名
221+ *@return {boolean } 是否成功
221222 */
222223async removeLabelsToPullRequest ( payload , name ) {
223224const owner = payload . repository . owner . login
224225const repo = payload . repository . name
225226const number = payload . pull_request . number
226227
227228try {
228- const res = await github . issues . removeLabel ( {
229+ await github . issues . removeLabel ( {
229230 owner,
230231 repo,
231232 number,
232233 name
233234} )
234- return res
235+ return true
235236} catch ( e ) {
236- Promise . reject ( e )
237+ return false
237238}
238239} ,
239240
@@ -242,21 +243,22 @@ module.exports = {
242243 *
243244 *@param {Object } payload data
244245 *@param {string } name 标签名
246+ *@return {boolean } 是否成功
245247 */
246248async removeLabelsToIssue ( payload , name ) {
247249const owner = payload . repository . owner . login
248250const repo = payload . repository . name
249251const number = payload . issues . number
250252try {
251- const res = await github . issues . removeLabel ( {
253+ await github . issues . removeLabel ( {
252254 owner,
253255 repo,
254256 number,
255257 name
256258} )
257- return res
259+ return true
258260} catch ( e ) {
259- Promise . reject ( e )
261+ return false
260262}
261263} ,
262264
@@ -270,12 +272,13 @@ module.exports = {
270272 *@param {string } options.body 内容
271273 *@param {boolean } options.draft 是否为草稿
272274 *@param {boolean } options.prerelease 是否预发布
275+ *@return {boolean } 是否成功
273276 */
274277async createRelease ( payload , { tag_name, target_commitish, name, body, draft, prerelease} ) {
275278const owner = payload . repository . owner . login
276279const repo = payload . repository . name
277280try {
278- const res = await github . repos . createRelease ( {
281+ await github . repos . createRelease ( {
279282 owner,
280283 repo,
281284 tag_name,
@@ -285,9 +288,9 @@ module.exports = {
285288 draft,
286289 prerelease
287290} )
288- return res
291+ return true
289292} catch ( e ) {
290- Promise . reject ( e )
293+ return false
291294}
292295} ,
293296
@@ -297,7 +300,7 @@ module.exports = {
297300 *@param {Object } payload data
298301 *@param {string } options.tag_name tag名
299302 *
300- *@return {Promise }
303+ *@return {Object | null }
301304 */
302305async getReleaseByTag ( payload , { tag_name} ) {
303306const owner = payload . repository . owner . login
@@ -308,9 +311,9 @@ module.exports = {
308311 repo,
309312tag :tag_name
310313} )
311- return res
314+ return res . data
312315} catch ( e ) {
313- return false
316+ return null
314317}
315318} ,
316319
@@ -321,47 +324,54 @@ module.exports = {
321324 *@param {Array | string } options.reviewers reviewer
322325 *@param {Array | string } options.team_reviewers team_reviewers
323326 *
324- *@return {Promise }
327+ *@return {boolean } 是否成功
325328 */
326329async createReviewRequest ( payload , { reviewers, team_reviewers} ) {
327330const owner = payload . repository . owner . login
328331const repo = payload . repository . name
329332const number = payload . pull_request . number
330333try {
331- const res = await github . pullRequests . createReviewRequest ( {
334+ await github . pullRequests . createReviewRequest ( {
332335 owner,
333336 repo,
334337 number,
335338reviewers :toArray ( reviewers ) ,
336339team_reviewers :toArray ( team_reviewers )
337340} )
338- return res
341+ return true
339342} catch ( e ) {
340- Promise . reject ( e )
343+ return false
341344}
342345} ,
343346
344347/**
345348 * 获得 repo 所有的tag
346349 *
347350 *@param {any } payload data
348- *@returns
351+ *@return { Array }
349352 */
350353async getTags ( payload ) {
351354const owner = payload . repository . owner . login
352355const repo = payload . repository . name
353-
354356try {
355357const res = await github . repos . getTags ( {
356358 owner,
357359 repo
358360} )
359361return res . data
360362} catch ( e ) {
361- Promise . reject ( e )
363+ return [ ]
362364}
363365} ,
364366
367+ /**
368+ * 对比2个提交
369+ *
370+ *@param {Object } payload data
371+ *@param {string } options.base 基点
372+ *@param {string } options.head diff
373+ *@return {Array | null }
374+ */
365375async compareCommits ( payload , { base, head} ) {
366376const owner = payload . repository . owner . login
367377const repo = payload . repository . name
@@ -374,7 +384,7 @@ module.exports = {
374384} )
375385return res . data
376386} catch ( e ) {
377- Promise . reject ( e )
387+ return null
378388}
379389}
380390}