@@ -22,10 +22,77 @@ githubClient.authenticate({
22
22
token : process . env . GITHUB_TOKEN
23
23
} )
24
24
25
- exports . pollThenStatus = pollByCommitThenStatus
25
+ exports . pollThenStatus = pollThenStatus
26
+
27
+ function pollThenStatus ( owner , repoName , prId ) {
28
+ const prInfo = prInfoStr ( { owner, repoName, prId } )
29
+
30
+ // we have to figure out what type of Travis polling we should perform,
31
+ // either by PR #id (as for nodejs.org) or commit sha (for readable-stream)
32
+ travisClient . repos ( owner , repoName ) . builds . get ( ( err , res ) => {
33
+ if ( err ) {
34
+ return console . error ( `! ${ prInfo } Error while retrieving initial builds` , err . stack )
35
+ }
36
+
37
+ const hasAnyPrBuilds = res . builds . some ( ( build ) => build . pull_request )
38
+
39
+ if ( hasAnyPrBuilds ) {
40
+ pollByPrThenComment ( owner , repoName , prId )
41
+ } else {
42
+ pollByCommitThenStatus ( owner , repoName , prId )
43
+ }
44
+ } )
45
+ }
26
46
27
47
/**
28
- * Poll and match builds by the last commit SHA of the related PR.
48
+ * When Travis CI picks up our PR's, we can poll and match builds
49
+ * by their related PR #id.
50
+ *
51
+ * That's the case for nodejs.org.
52
+ */
53
+ function pollByPrThenComment ( owner , repoName , prId , checkNumber ) {
54
+ checkNumber = checkNumber || 1
55
+
56
+ const prInfo = prInfoStr ( { owner, repoName, prId } )
57
+ const createGhComment = createGhCommentFn ( { owner, repoName, prId } )
58
+
59
+ if ( checkNumber > 100 ) {
60
+ console . warn ( `* ${ prInfo } Was not able to find matching build for PR, stopping poll now :(` )
61
+ return
62
+ }
63
+
64
+ travisClient . repos ( owner , repoName ) . builds . get ( ( err , res ) => {
65
+ if ( err ) {
66
+ return console . error ( `! ${ prInfo } Error while retrieving builds` , err . stack )
67
+ }
68
+
69
+ const lastBuildForPr = res . builds . find ( ( build ) => build . pull_request_number === prId )
70
+
71
+ if ( lastBuildForPr ) {
72
+ const lastState = lastBuildForPr . state
73
+
74
+ if ( lastState === 'passed' ) {
75
+ return createGhComment ( `[Travis build passed](https://travis-ci.org/${ owner } /${ repoName } /builds/${ lastBuildForPr . id } ) :+1:` )
76
+ } else if ( lastState === 'failed' ) {
77
+ return createGhComment ( `[Travis build failed](https://travis-ci.org/${ owner } /${ repoName } /builds/${ lastBuildForPr . id } ) :-1:` )
78
+ } else if ( ~ [ 'created' , 'started' ] . indexOf ( lastState ) ) {
79
+ console . log ( `* ${ prInfo } "${ lastState } " build found, will do check #${ checkNumber + 1 } in 30 seconds` )
80
+ } else {
81
+ return console . log ( `* ${ prInfo } Unknown build state: "${ lastState } ", stopping polling` )
82
+ }
83
+ } else {
84
+ console . warn ( `! ${ prInfo } Was not able to find matching build, will do check #${ checkNumber + 1 } in 30 seconds` )
85
+ }
86
+
87
+ setTimeout ( pollByPrThenComment , 30 * 1000 , owner , repoName , prId , checkNumber + 1 )
88
+ } )
89
+ }
90
+
91
+ /**
92
+ * When Travis CI *doesn't* pick up our PR's, we have to poll and match builds
93
+ * by the last commit SHA of the related PR.
94
+ *
95
+ * This is the case for readable-stream.
29
96
*/
30
97
function pollByCommitThenStatus ( owner , repoName , prId ) {
31
98
const prInfo = prInfoStr ( { owner, repoName, prId } )
@@ -90,6 +157,24 @@ function pollTravisBuildBySha (options, checkNumber) {
90
157
} )
91
158
}
92
159
160
+ function createGhCommentFn ( options ) {
161
+ const prInfo = prInfoStr ( options )
162
+
163
+ return ( message ) => {
164
+ githubClient . issues . createComment ( {
165
+ user : options . owner ,
166
+ repo : options . repoName ,
167
+ number : options . prId ,
168
+ body : message
169
+ } , ( err , res ) => {
170
+ if ( err ) {
171
+ return console . error ( `! ${ prInfo } Error while creating GitHub comment` , err . stack )
172
+ }
173
+ console . log ( `* ${ prInfo } Github comment created` )
174
+ } )
175
+ }
176
+ }
177
+
93
178
function createGhStatusFn ( options ) {
94
179
const prInfo = prInfoStr ( options )
95
180
0 commit comments