9
9
10
10
'use strict' ;
11
11
12
- if ( ! process . env . CIRCLE_PROJECT_USERNAME ) {
13
- console . error ( 'Missing CIRCLE_PROJECT_USERNAME . Example: facebook' ) ;
12
+ if ( ! process . env . GITHUB_OWNER ) {
13
+ console . error ( 'Missing GITHUB_OWNER . Example: facebook' ) ;
14
14
process . exit ( 1 ) ;
15
15
}
16
- if ( ! process . env . CIRCLE_PROJECT_REPONAME ) {
17
- console . error ( 'Missing CIRCLE_PROJECT_REPONAME . Example: react-native' ) ;
16
+ if ( ! process . env . GITHUB_REPO ) {
17
+ console . error ( 'Missing GITHUB_REPO . Example: react-native' ) ;
18
18
process . exit ( 1 ) ;
19
19
}
20
20
@@ -162,35 +162,51 @@ function getLineMapFromPatch(patchString) {
162
162
return lineMap ;
163
163
}
164
164
165
- function sendReview ( owner , repo , number , commit_id , comments , convertersUsed ) {
166
- if ( comments . length === 0 ) {
167
- // Do not leave an empty review.
168
- return ;
169
- }
170
-
171
- let body = '**Code analysis results:**\n\n' ;
172
- convertersUsed . forEach ( converter => {
173
- body += '* `' + converter + '` found some issues.\n' ;
174
- } ) ;
175
-
176
- const event = 'REQUEST_CHANGES' ;
177
-
178
- const opts = {
179
- owner,
180
- repo,
181
- number,
182
- commit_id,
183
- body,
184
- event,
185
- comments,
186
- } ;
165
+ function sendReview ( owner , repo , number , commit_id , body , comments ) {
166
+ if ( process . env . GITHUB_TOKEN ) {
167
+ if ( comments . length === 0 ) {
168
+ // Do not leave an empty review.
169
+ return ;
170
+ }
187
171
188
- octokit . pullRequests . createReview ( opts , function ( error , res ) {
189
- if ( error ) {
190
- console . error ( error ) ;
172
+ const event = 'REQUEST_CHANGES' ;
173
+
174
+ const opts = {
175
+ owner,
176
+ repo,
177
+ number,
178
+ commit_id,
179
+ body,
180
+ event,
181
+ comments,
182
+ } ;
183
+
184
+ octokit . pullRequests . createReview ( opts , function ( error , res ) {
185
+ if ( error ) {
186
+ console . error ( error ) ;
187
+ return ;
188
+ }
189
+ } ) ;
190
+ } else {
191
+ if ( comments . length === 0 ) {
192
+ console . log ( 'No issues found.' ) ;
191
193
return ;
192
194
}
193
- } ) ;
195
+
196
+ if ( process . env . CIRCLE_CI ) {
197
+ console . error (
198
+ 'Code analysis found issues, but the review cannot be posted to GitHub without an access token.' ,
199
+ ) ;
200
+ process . exit ( 1 ) ;
201
+ }
202
+
203
+ let results = body + '\n' ;
204
+ comments . forEach ( comment => {
205
+ results +=
206
+ comment . path + ':' + comment . position + ': ' + comment . body + '\n' ;
207
+ } ) ;
208
+ console . log ( results ) ;
209
+ }
194
210
}
195
211
196
212
function main ( messages , owner , repo , number ) {
@@ -199,18 +215,17 @@ function main(messages, owner, repo, number) {
199
215
return ;
200
216
}
201
217
202
- if ( ! process . env . GITHUB_TOKEN ) {
203
- console . error (
204
- 'Missing GITHUB_TOKEN. Example: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e' ,
218
+ if ( process . env . GITHUB_TOKEN ) {
219
+ octokit . authenticate ( {
220
+ type : 'oauth' ,
221
+ token : process . env . GITHUB_TOKEN ,
222
+ } ) ;
223
+ } else {
224
+ console . log (
225
+ 'Missing GITHUB_TOKEN. Example: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e. Review feedback with code analysis results will not be provided on GitHub.' ,
205
226
) ;
206
- process . exit ( 1 ) ;
207
227
}
208
228
209
- octokit . authenticate ( {
210
- type : 'oauth' ,
211
- token : process . env . GITHUB_TOKEN ,
212
- } ) ;
213
-
214
229
getShaFromPullRequest ( owner , repo , number , sha => {
215
230
getFilesFromCommit ( owner , repo , sha , files => {
216
231
let comments = [ ] ;
@@ -234,7 +249,13 @@ function main(messages, owner, repo, number) {
234
249
} ) ; // forEach
235
250
} ) ; // filter
236
251
237
- sendReview ( owner , repo , number , sha , comments , convertersUsed ) ;
252
+ let body = '**Code analysis results:**\n\n' ;
253
+ const uniqueconvertersUsed = [ ...new Set ( convertersUsed ) ] ;
254
+ uniqueconvertersUsed . forEach ( converter => {
255
+ body += '* `' + converter + '` found some issues.\n' ;
256
+ } ) ;
257
+
258
+ sendReview ( owner , repo , number , sha , body , comments ) ;
238
259
} ) ; // getFilesFromCommit
239
260
} ) ; // getShaFromPullRequest
240
261
}
@@ -287,16 +308,16 @@ process.stdin.on('end', function() {
287
308
delete messages [ absolutePath ] ;
288
309
}
289
310
290
- const owner = process . env . CIRCLE_PROJECT_USERNAME ;
291
- const repo = process . env . CIRCLE_PROJECT_REPONAME ;
311
+ const owner = process . env . GITHUB_OWNER ;
312
+ const repo = process . env . GITHUB_REPO ;
292
313
293
- if ( ! process . env . CIRCLE_PR_NUMBER ) {
294
- console . error ( 'Missing CIRCLE_PR_NUMBER . Example: 4687' ) ;
314
+ if ( ! process . env . GITHUB_PR_NUMBER ) {
315
+ console . error ( 'Missing GITHUB_PR_NUMBER . Example: 4687' ) ;
295
316
// for master branch, don't throw an error
296
317
process . exit ( 0 ) ;
297
318
}
298
319
299
- const number = process . env . CIRCLE_PR_NUMBER ;
320
+ const number = process . env . GITHUB_PR_NUMBER ;
300
321
301
322
// intentional lint warning to make sure that the bot is working :)
302
323
main ( messages , owner , repo , number ) ;
0 commit comments