@@ -34,6 +34,8 @@ const converterSummary = {
34
34
'`flow` found some issues. Run `yarn flow check` to analyze your code and address any errors.' ,
35
35
shellcheck :
36
36
'`shellcheck` found some issues. Run `yarn shellcheck` to analyze shell scripts.' ,
37
+ 'google-java-format' :
38
+ '`google-java-format` found some issues. See https://github.com/google/google-java-format' ,
37
39
} ;
38
40
39
41
/**
@@ -57,6 +59,24 @@ const converters = {
57
59
}
58
60
} ,
59
61
62
+ 'google-java-format' : function ( output , input ) {
63
+ if ( ! input ) {
64
+ return ;
65
+ }
66
+
67
+ input . forEach ( function ( change ) {
68
+ push ( output , change . file , {
69
+ message : `\`google-java-format\` suggested changes:
70
+ \`\`\`diff
71
+ ${ change . description }
72
+ \`\`\`
73
+ ` ,
74
+ line : change . line ,
75
+ converter : 'google-java-format' ,
76
+ } ) ;
77
+ } ) ;
78
+ } ,
79
+
60
80
flow : function ( output , input ) {
61
81
if ( ! input || ! input . errors ) {
62
82
return ;
@@ -113,30 +133,6 @@ const converters = {
113
133
} ,
114
134
} ;
115
135
116
- function getShaFromPullRequest ( octokit , owner , repo , number , callback ) {
117
- octokit . pullRequests . get ( { owner, repo, number} , ( error , res ) => {
118
- if ( error ) {
119
- console . error ( error ) ;
120
- return ;
121
- }
122
-
123
- callback ( res . data . head . sha ) ;
124
- } ) ;
125
- }
126
-
127
- function getFilesFromPullRequest ( octokit , owner , repo , number , callback ) {
128
- octokit . pullRequests . listFiles (
129
- { owner, repo, number, per_page : 100 } ,
130
- ( error , res ) => {
131
- if ( error ) {
132
- console . error ( error ) ;
133
- return ;
134
- }
135
- callback ( res . data ) ;
136
- } ,
137
- ) ;
138
- }
139
-
140
136
/**
141
137
* Sadly we can't just give the line number to github, we have to give the
142
138
* line number relative to the patch file which is super annoying. This
@@ -166,7 +162,15 @@ function getLineMapFromPatch(patchString) {
166
162
return lineMap ;
167
163
}
168
164
169
- function sendReview ( octokit , owner , repo , number , commit_id , body , comments ) {
165
+ async function sendReview (
166
+ octokit ,
167
+ owner ,
168
+ repo ,
169
+ pull_number ,
170
+ commit_id ,
171
+ body ,
172
+ comments ,
173
+ ) {
170
174
if ( process . env . GITHUB_TOKEN ) {
171
175
if ( comments . length === 0 ) {
172
176
// Do not leave an empty review.
@@ -181,19 +185,14 @@ function sendReview(octokit, owner, repo, number, commit_id, body, comments) {
181
185
const opts = {
182
186
owner,
183
187
repo,
184
- number ,
188
+ pull_number ,
185
189
commit_id,
186
190
body,
187
191
event,
188
192
comments,
189
193
} ;
190
194
191
- octokit . pullRequests . createReview ( opts , function ( error , res ) {
192
- if ( error ) {
193
- console . error ( error ) ;
194
- return ;
195
- }
196
- } ) ;
195
+ await octokit . pulls . createReview ( opts ) ;
197
196
} else {
198
197
if ( comments . length === 0 ) {
199
198
console . log ( 'No issues found.' ) ;
@@ -216,7 +215,7 @@ function sendReview(octokit, owner, repo, number, commit_id, body, comments) {
216
215
}
217
216
}
218
217
219
- function main ( messages , owner , repo , number ) {
218
+ async function main ( messages , owner , repo , pull_number ) {
220
219
// No message, we don't need to do anything :)
221
220
if ( Object . keys ( messages ) . length === 0 ) {
222
221
return ;
@@ -234,40 +233,54 @@ function main(messages, owner, repo, number) {
234
233
auth : process . env . GITHUB_TOKEN ,
235
234
} ) ;
236
235
237
- getShaFromPullRequest ( octokit , owner , repo , number , sha => {
238
- getFilesFromPullRequest ( octokit , owner , repo , number , files => {
239
- let comments = [ ] ;
240
- let convertersUsed = [ ] ;
241
- files
242
- . filter ( file => messages [ file . filename ] )
243
- . forEach ( file => {
244
- // github api sometimes does not return a patch on large commits
245
- if ( ! file . patch ) {
246
- return ;
247
- }
248
- const lineMap = getLineMapFromPatch ( file . patch ) ;
249
- messages [ file . filename ] . forEach ( message => {
250
- if ( lineMap [ message . line ] ) {
251
- const comment = {
252
- path : file . filename ,
253
- position : lineMap [ message . line ] ,
254
- body : message . message ,
255
- } ;
256
- convertersUsed . push ( message . converter ) ;
257
- comments . push ( comment ) ;
258
- }
259
- } ) ; // forEach
260
- } ) ; // filter
261
-
262
- let body = '**Code analysis results:**\n\n' ;
263
- const uniqueconvertersUsed = [ ...new Set ( convertersUsed ) ] ;
264
- uniqueconvertersUsed . forEach ( converter => {
265
- body += '* ' + converterSummary [ converter ] + '\n' ;
266
- } ) ;
236
+ const opts = {
237
+ owner,
238
+ repo,
239
+ pull_number,
240
+ } ;
267
241
268
- sendReview ( octokit , owner , repo , number , sha , body , comments ) ;
269
- } ) ; // getFilesFromPullRequest
270
- } ) ; // getShaFromPullRequest
242
+ const { data : pull } = await octokit . pulls . get ( opts ) ;
243
+ const { data : files } = await octokit . pulls . listFiles ( opts ) ;
244
+
245
+ const comments = [ ] ;
246
+ const convertersUsed = [ ] ;
247
+
248
+ files
249
+ . filter ( file => messages [ file . filename ] )
250
+ . forEach ( file => {
251
+ // github api sometimes does not return a patch on large commits
252
+ if ( ! file . patch ) {
253
+ return ;
254
+ }
255
+ const lineMap = getLineMapFromPatch ( file . patch ) ;
256
+ messages [ file . filename ] . forEach ( message => {
257
+ if ( lineMap [ message . line ] ) {
258
+ const comment = {
259
+ path : file . filename ,
260
+ position : lineMap [ message . line ] ,
261
+ body : message . message ,
262
+ } ;
263
+ convertersUsed . push ( message . converter ) ;
264
+ comments . push ( comment ) ;
265
+ }
266
+ } ) ; // forEach
267
+ } ) ; // filter
268
+
269
+ let body = '**Code analysis results:**\n\n' ;
270
+ const uniqueconvertersUsed = [ ...new Set ( convertersUsed ) ] ;
271
+ uniqueconvertersUsed . forEach ( converter => {
272
+ body += '* ' + converterSummary [ converter ] + '\n' ;
273
+ } ) ;
274
+
275
+ await sendReview (
276
+ octokit ,
277
+ owner ,
278
+ repo ,
279
+ pull_number ,
280
+ pull . head . sha ,
281
+ body ,
282
+ comments ,
283
+ ) ;
271
284
}
272
285
273
286
let content = '' ;
@@ -331,6 +344,7 @@ process.stdin.on('end', function() {
331
344
332
345
const number = process . env . GITHUB_PR_NUMBER ;
333
346
334
- // intentional lint warning to make sure that the bot is working :)
335
- main ( messages , owner , repo , number ) ;
347
+ ( async ( ) => {
348
+ await main ( messages , owner , repo , number ) ;
349
+ } ) ( ) ;
336
350
} ) ;
0 commit comments