@@ -22,7 +22,10 @@ githubClient.authenticate({
22
22
token : process . env . GITHUB_TOKEN
23
23
} )
24
24
25
- function pollThenComment ( owner , repoName , prId ) {
25
+ exports . pollThenStatus = pollThenStatus
26
+
27
+
28
+ function pollThenStatus ( owner , repoName , prId ) {
26
29
const prInfo = prInfoStr ( { owner, repoName, prId } )
27
30
28
31
// we have to figure out what type of Travis polling we should perform,
@@ -37,7 +40,7 @@ function pollThenComment (owner, repoName, prId) {
37
40
if ( hasAnyPrBuilds ) {
38
41
pollByPrThenComment ( owner , repoName , prId )
39
42
} else {
40
- pollByCommitThenComment ( owner , repoName , prId )
43
+ pollByCommitThenStatus ( owner , repoName , prId )
41
44
}
42
45
} )
43
46
}
@@ -92,7 +95,7 @@ function pollByPrThenComment (owner, repoName, prId, checkNumber) {
92
95
*
93
96
* This is the case for readable-stream.
94
97
*/
95
- function pollByCommitThenComment ( owner , repoName , prId ) {
98
+ function pollByCommitThenStatus ( owner , repoName , prId ) {
96
99
const prInfo = prInfoStr ( { owner, repoName, prId } )
97
100
98
101
githubClient . pullRequests . getCommits ( {
@@ -111,7 +114,7 @@ function pollByCommitThenComment (owner, repoName, prId) {
111
114
}
112
115
113
116
function pollTravisBuildBySha ( options , checkNumber ) {
114
- const createGhComment = createGhCommentFn ( options )
117
+ const createGhStatus = createGhStatusFn ( options )
115
118
const prInfo = prInfoStr ( options )
116
119
const shaToMatch = options . lastSha
117
120
@@ -138,11 +141,14 @@ function pollTravisBuildBySha (options, checkNumber) {
138
141
const lastState = lastBuildForCommit . state
139
142
140
143
if ( lastState === 'passed' ) {
141
- return createGhComment ( `[Travis build passed](https://travis-ci.org/ ${ options . owner } / ${ options . repoName } /builds/ ${ lastBuildForCommit . id } ) :+1:` )
144
+ return createGhStatus ( 'success' , lastBuildForCommit . id , 'all tests passed' )
142
145
} else if ( lastState === 'failed' ) {
143
- return createGhComment ( `[Travis build failed](https://travis-ci.org/ ${ options . owner } / ${ options . repoName } /builds/ ${ lastBuildForCommit . id } ) :-1:` )
146
+ return createGhStatus ( 'failure' , lastBuildForCommit . id , 'build failure' )
144
147
} else if ( ~ [ 'created' , 'started' ] . indexOf ( lastState ) ) {
145
148
console . log ( `* ${ prInfo } "${ lastState } " build found, will do check #${ checkNumber + 1 } in 30 seconds` )
149
+ if ( checkNumber === 1 ) {
150
+ createGhStatus ( 'pending' , lastBuildForCommit . id , 'build in progress' )
151
+ }
146
152
} else {
147
153
return console . log ( `* ${ prInfo } Unknown build state: "${ lastState } ", stopping polling` )
148
154
}
@@ -157,7 +163,7 @@ function pollTravisBuildBySha (options, checkNumber) {
157
163
function createGhCommentFn ( options ) {
158
164
const prInfo = prInfoStr ( options )
159
165
160
- return ( message , cb ) => {
166
+ return ( message ) => {
161
167
githubClient . issues . createComment ( {
162
168
user : options . owner ,
163
169
repo : options . repoName ,
@@ -172,8 +178,27 @@ function createGhCommentFn (options) {
172
178
}
173
179
}
174
180
181
+ function createGhStatusFn ( options ) {
182
+ const prInfo = prInfoStr ( options )
183
+
184
+ return ( state , travisId , message ) => {
185
+ githubClient . statuses . create ( {
186
+ user : options . owner ,
187
+ repo : options . repoName ,
188
+ sha : options . lastSha ,
189
+ target_url : `https://travis-ci.org/${ options . owner } /${ options . repoName } /builds/${ travisId } ` ,
190
+ context : "Travis CI via nodejs-github-bot" ,
191
+ state : state ,
192
+ description : message ,
193
+ } , ( err , res ) => {
194
+ if ( err ) {
195
+ return console . error ( `! ${ prInfo } Error while updating GitHub PR status` , err . stack )
196
+ }
197
+ console . log ( `* ${ prInfo } Github PR status updated` )
198
+ } )
199
+ }
200
+ }
201
+
175
202
function prInfoStr ( options ) {
176
203
return `${ options . owner } /${ options . repoName } /#${ options . prId } `
177
204
}
178
-
179
- exports . pollThenComment = pollThenComment
0 commit comments