@@ -8,7 +8,7 @@ import { Benchmark, BenchmarkResult } from './extract';
88import { Config , ToolType } from './config' ;
99import { DEFAULT_INDEX_HTML } from './default_index_html' ;
1010import { leavePRComment } from './comment/leavePRComment' ;
11- import { leaveCommitComment } from './comment/leaveCommitComment' ;
11+ import { leaveCommitComment , updateCommitCommentIfExists } from './comment/leaveCommitComment' ;
1212import { addBenchmarkEntry } from './addBenchmarkEntry' ;
1313
1414export type BenchmarkSuites = { [ name : string ] : Benchmark [ ] } ;
@@ -348,6 +348,28 @@ function buildAlertComment(
348348 return lines . join ( '\n' ) ;
349349}
350350
351+ function buildNoAlertsComment ( benchName : string , curSuite : Benchmark , prevSuite : Benchmark , cc : string [ ] ) : string {
352+ // Do not show benchmark name if it is the default value 'Benchmark'.
353+ const benchmarkText = benchName === 'Benchmark' ? '' : ` for **'${ benchName } '**` ;
354+ const lines = [
355+ '# Performance Report' ,
356+ '' ,
357+ `No performance alerts${ benchmarkText } .` ,
358+ '' ,
359+ `Previous commit: ${ prevSuite . commit . id } ` ,
360+ `Current commit: ${ curSuite . commit . id } ` ,
361+ ] ;
362+
363+ // Footer
364+ lines . push ( '' , commentFooter ( ) ) ;
365+
366+ if ( cc . length > 0 ) {
367+ lines . push ( '' , `CC: ${ cc . join ( ' ' ) } ` ) ;
368+ }
369+
370+ return lines . join ( '\n' ) ;
371+ }
372+
351373async function leaveComment ( commitId : string , body : string , commentId : string , token : string ) {
352374 core . debug ( 'Sending comment:\n' + body ) ;
353375
@@ -378,6 +400,37 @@ async function handleComment(benchName: string, curSuite: Benchmark, prevSuite:
378400 await leaveComment ( curSuite . commit . id , body , `${ benchName } Summary` , githubToken ) ;
379401}
380402
403+ async function replaceAlertCommentWithNoAlerts ( body : string , commentId : string , token : string , prevCommitId : string ) {
404+ core . debug ( 'Replacing an existing alert comment with a no-alerts message:\n' + body ) ;
405+
406+ const repoMetadata = getCurrentRepoMetadata ( ) ;
407+ const pr = github . context . payload . pull_request ;
408+
409+ if ( pr ?. number ) {
410+ // PR review comments are updated in place, so only update an existing alert comment
411+ return await leavePRComment (
412+ repoMetadata . owner . login ,
413+ repoMetadata . name ,
414+ pr . number ,
415+ body ,
416+ commentId ,
417+ token ,
418+ true ,
419+ ) ;
420+ }
421+
422+ // Commit comments are attached to the commit where the alert was detected. When the alert is
423+ // resolved, update the alert comment left on the previous commit.
424+ return await updateCommitCommentIfExists (
425+ repoMetadata . owner . login ,
426+ repoMetadata . name ,
427+ prevCommitId ,
428+ body ,
429+ commentId ,
430+ token ,
431+ ) ;
432+ }
433+
381434async function handleAlert ( benchName : string , curSuite : Benchmark , prevSuite : Benchmark , config : Config ) {
382435 const { alertThreshold, githubToken, commentOnAlert, failOnAlert, alertCommentCcUsers, failThreshold } = config ;
383436
@@ -389,7 +442,19 @@ async function handleAlert(benchName: string, curSuite: Benchmark, prevSuite: Be
389442 const [ losses , gains ] = findAlerts ( curSuite , prevSuite , alertThreshold ) ;
390443 const alerts = [ ...losses , ...gains ] ;
391444 if ( alerts . length === 0 ) {
392- core . debug ( 'No performance alert found happily' ) ;
445+ if ( commentOnAlert ) {
446+ if ( ! githubToken ) {
447+ throw new Error ( "'comment-on-alert' input is set but 'github-token' input is not set" ) ;
448+ }
449+ // When a previous alert comment exists (e.g. an alert was left on an earlier commit of
450+ // this PR), replace it with a message saying that no alerts are detected anymore instead
451+ // of leaving the stale alert comment. Do not create a new comment when none exists.
452+ core . debug ( 'No performance alert was found. Replacing an existing alert comment with a no-alerts message' ) ;
453+ const body = buildNoAlertsComment ( benchName , curSuite , prevSuite , alertCommentCcUsers ) ;
454+ await replaceAlertCommentWithNoAlerts ( body , `${ benchName } Alert` , githubToken , prevSuite . commit . id ) ;
455+ } else {
456+ core . debug ( 'No performance alert found happily' ) ;
457+ }
393458 return ;
394459 }
395460
@@ -409,8 +474,10 @@ async function handleAlert(benchName: string, curSuite: Benchmark, prevSuite: Be
409474 throw new Error ( "'comment-on-alert' input is set but 'github-token' input is not set" ) ;
410475 }
411476 const res = await leaveComment ( curSuite . commit . id , body , `${ benchName } Alert` , githubToken ) ;
412- const url = res . data . html_url ;
413- message = body + `\nComment was generated at ${ url } ` ;
477+ if ( res ) {
478+ const url = res . data . html_url ;
479+ message = body + `\nComment was generated at ${ url } ` ;
480+ }
414481 }
415482
416483 if ( failOnAlert ) {
0 commit comments