@@ -22,7 +22,10 @@ githubClient.authenticate({
2222 token : process . env . GITHUB_TOKEN
2323} )
2424
25- function pollThenComment ( owner , repoName , prId ) {
25+ exports . pollThenStatus = pollThenStatus
26+
27+
28+ function pollThenStatus ( owner , repoName , prId ) {
2629 const prInfo = prInfoStr ( { owner, repoName, prId } )
2730
2831 // we have to figure out what type of Travis polling we should perform,
@@ -34,10 +37,10 @@ function pollThenComment (owner, repoName, prId) {
3437
3538 const hasAnyPrBuilds = res . builds . some ( ( build ) => build . pull_request )
3639
37- if ( hasAnyPrBuilds ) {
40+ if ( false /* hasAnyPrBuilds*/ ) {
3841 pollByPrThenComment ( owner , repoName , prId )
3942 } else {
40- pollByCommitThenComment ( owner , repoName , prId )
43+ pollByCommitThenStatus ( owner , repoName , prId )
4144 }
4245 } )
4346}
@@ -92,7 +95,7 @@ function pollByPrThenComment (owner, repoName, prId, checkNumber) {
9295 *
9396 * This is the case for readable-stream.
9497 */
95- function pollByCommitThenComment ( owner , repoName , prId ) {
98+ function pollByCommitThenStatus ( owner , repoName , prId ) {
9699 const prInfo = prInfoStr ( { owner, repoName, prId } )
97100
98101 githubClient . pullRequests . getCommits ( {
@@ -111,7 +114,7 @@ function pollByCommitThenComment (owner, repoName, prId) {
111114}
112115
113116function pollTravisBuildBySha ( options , checkNumber ) {
114- const createGhComment = createGhCommentFn ( options )
117+ const createGhStatus = createGhStatusFn ( options )
115118 const prInfo = prInfoStr ( options )
116119 const shaToMatch = options . lastSha
117120
@@ -138,11 +141,14 @@ function pollTravisBuildBySha (options, checkNumber) {
138141 const lastState = lastBuildForCommit . state
139142
140143 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' )
142145 } 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' )
144147 } else if ( ~ [ 'created' , 'started' ] . indexOf ( lastState ) ) {
145148 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+ }
146152 } else {
147153 return console . log ( `* ${ prInfo } Unknown build state: "${ lastState } ", stopping polling` )
148154 }
@@ -157,7 +163,7 @@ function pollTravisBuildBySha (options, checkNumber) {
157163function createGhCommentFn ( options ) {
158164 const prInfo = prInfoStr ( options )
159165
160- return ( message , cb ) => {
166+ return ( message ) => {
161167 githubClient . issues . createComment ( {
162168 user : options . owner ,
163169 repo : options . repoName ,
@@ -172,8 +178,27 @@ function createGhCommentFn (options) {
172178 }
173179}
174180
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 , err . stack )
196+ }
197+ console . log ( `* ${ prInfo } Github PR status updated` )
198+ } )
199+ }
200+ }
201+
175202function prInfoStr ( options ) {
176203 return `${ options . owner } /${ options . repoName } /#${ options . prId } `
177204}
178-
179- exports . pollThenComment = pollThenComment
0 commit comments