@@ -7,6 +7,8 @@ import * as git from './git';
7
7
import { Benchmark , BenchmarkResult } from './extract' ;
8
8
import { Config , ToolType } from './config' ;
9
9
import { DEFAULT_INDEX_HTML } from './default_index_html' ;
10
+ import { leavePRComment } from './comment/leavePRComment' ;
11
+ import { leaveCommitComment } from './comment/leaveCommitComment' ;
10
12
11
13
export type BenchmarkSuites = { [ name : string ] : Benchmark [ ] } ;
12
14
export interface DataJson {
@@ -236,24 +238,15 @@ function buildAlertComment(
236
238
return lines . join ( '\n' ) ;
237
239
}
238
240
239
- async function leaveComment ( commitId : string , body : string , token : string ) {
241
+ async function leaveComment ( commitId : string , body : string , commentId : string , token : string ) {
240
242
core . debug ( 'Sending comment:\n' + body ) ;
241
243
242
244
const repoMetadata = getCurrentRepoMetadata ( ) ;
243
- const repoUrl = repoMetadata . html_url ?? '' ;
244
- const client = github . getOctokit ( token ) ;
245
- const res = await client . rest . repos . createCommitComment ( {
246
- owner : repoMetadata . owner . login ,
247
- repo : repoMetadata . name ,
248
- // eslint-disable-next-line @typescript-eslint/naming-convention
249
- commit_sha : commitId ,
250
- body,
251
- } ) ;
252
-
253
- const commitUrl = `${ repoUrl } /commit/${ commitId } ` ;
254
- console . log ( `Comment was sent to ${ commitUrl } . Response:` , res . status , res . data ) ;
245
+ const pr = github . context . payload . pull_request ;
255
246
256
- return res ;
247
+ return await ( pr ?. number
248
+ ? leavePRComment ( repoMetadata . owner . login , repoMetadata . name , pr . number , body , commentId , token )
249
+ : leaveCommitComment ( repoMetadata . owner . login , repoMetadata . name , commitId , body , commentId , token ) ) ;
257
250
}
258
251
259
252
async function handleComment ( benchName : string , curSuite : Benchmark , prevSuite : Benchmark , config : Config ) {
@@ -272,7 +265,7 @@ async function handleComment(benchName: string, curSuite: Benchmark, prevSuite:
272
265
273
266
const body = buildComment ( benchName , curSuite , prevSuite ) ;
274
267
275
- await leaveComment ( curSuite . commit . id , body , githubToken ) ;
268
+ await leaveComment ( curSuite . commit . id , body , ` ${ benchName } Summary` , githubToken ) ;
276
269
}
277
270
278
271
async function handleAlert ( benchName : string , curSuite : Benchmark , prevSuite : Benchmark , config : Config ) {
@@ -292,14 +285,13 @@ async function handleAlert(benchName: string, curSuite: Benchmark, prevSuite: Be
292
285
core . debug ( `Found ${ alerts . length } alerts` ) ;
293
286
const body = buildAlertComment ( alerts , benchName , curSuite , prevSuite , alertThreshold , alertCommentCcUsers ) ;
294
287
let message = body ;
295
- let url = null ;
296
288
297
289
if ( commentOnAlert ) {
298
290
if ( ! githubToken ) {
299
291
throw new Error ( "'comment-on-alert' input is set but 'github-token' input is not set" ) ;
300
292
}
301
- const res = await leaveComment ( curSuite . commit . id , body , githubToken ) ;
302
- url = res . data . html_url ;
293
+ const res = await leaveComment ( curSuite . commit . id , body , ` ${ benchName } Alert` , githubToken ) ;
294
+ const url = res . data . html_url ;
303
295
message = body + `\nComment was generated at ${ url } ` ;
304
296
}
305
297
@@ -364,7 +356,7 @@ function addBenchmarkToDataJson(
364
356
return prevBench ;
365
357
}
366
358
367
- function isRemoteRejectedError ( err : unknown ) {
359
+ function isRemoteRejectedError ( err : unknown ) : err is Error {
368
360
if ( err instanceof Error ) {
369
361
return [ '[remote rejected]' , '[rejected]' ] . some ( ( l ) => err . message . includes ( l ) ) ;
370
362
}
@@ -443,7 +435,7 @@ async function writeBenchmarkToGitHubPagesWithRetry(
443
435
console . log (
444
436
`Automatically pushed the generated commit to ${ ghPagesBranch } branch since 'auto-push' is set to true` ,
445
437
) ;
446
- } catch ( err : any ) {
438
+ } catch ( err : unknown ) {
447
439
if ( ! isRemoteRejectedError ( err ) ) {
448
440
throw err ;
449
441
}
0 commit comments