From 80a741a8cd3fcfb04c8cad2097d72e49063190d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Louv-Jansen?= Date: Sat, 19 Mar 2022 08:35:25 +0100 Subject: [PATCH] Add `publishStatusCommentOnAbort` --- src/lib/github/v3/createStatusComment.test.ts | 11 +++++--- src/lib/github/v3/createStatusComment.ts | 27 +++++++------------ src/options/ConfigOptions.ts | 3 ++- src/options/cliArgs.ts | 1 + src/options/options.test.ts | 3 ++- src/options/options.ts | 3 ++- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/lib/github/v3/createStatusComment.test.ts b/src/lib/github/v3/createStatusComment.test.ts index 0267a9cb..da79bc14 100644 --- a/src/lib/github/v3/createStatusComment.test.ts +++ b/src/lib/github/v3/createStatusComment.test.ts @@ -401,8 +401,11 @@ describe('getCommentBody', () => { } as BackportResponse, }); - it('posts a comment when `publishStatusCommentOnFailure = true`', () => { - const params = getParams({ publishStatusCommentOnFailure: true }); + it('posts a comment when `publishStatusCommentOnAbort = true`', () => { + const params = getParams({ + publishStatusCommentOnAbort: true, + publishStatusCommentOnFailure: true, + }); expect(getCommentBody(params)).toMatchInlineSnapshot(` "## ⚪ Backport skipped The pull request was not backported as there were no branches to backport to. If this is a mistake, please apply the desired version labels or run the backport tool manually. @@ -420,8 +423,8 @@ describe('getCommentBody', () => { `); }); - it('does not post a comment when `publishStatusCommentOnFailure = false`', () => { - const params = getParams({ publishStatusCommentOnFailure: false }); + it('does not post a comment when `publishStatusCommentOnAbort = false`', () => { + const params = getParams({ publishStatusCommentOnAbort: false }); expect(getCommentBody(params)).toBe(undefined); }); }); diff --git a/src/lib/github/v3/createStatusComment.ts b/src/lib/github/v3/createStatusComment.ts index f4f6b8fe..84f988cc 100644 --- a/src/lib/github/v3/createStatusComment.ts +++ b/src/lib/github/v3/createStatusComment.ts @@ -14,21 +14,7 @@ export async function createStatusComment({ options: ValidConfigOptions; backportResponse: BackportResponse; }): Promise { - const { - githubApiBaseUrlV3, - repoName, - repoOwner, - accessToken, - publishStatusCommentOnFailure, - publishStatusCommentOnSuccess, - } = options; - - if ( - (!publishStatusCommentOnFailure && !publishStatusCommentOnSuccess) || - options.dryRun - ) { - return; - } + const { githubApiBaseUrlV3, repoName, repoOwner, accessToken } = options; try { const octokit = new Octokit({ @@ -81,11 +67,11 @@ export function getCommentBody({ repoName, repoOwner, autoMerge, - publishStatusCommentOnSuccess, + publishStatusCommentOnAbort, publishStatusCommentOnFailure, + publishStatusCommentOnSuccess, } = options; - // TODO; add new cli args to specify whether to post comments for successful and failures // eg. in addition to `--noStatusComment` add `--noFailureStatusComment` and `--noSuccessStatusComment` where the former will overwrite the two latter const didAllBackportsSucceed = @@ -93,10 +79,15 @@ export function getCommentBody({ backportResponse.results.every((r) => r.status === 'success'); if ( + // don't publish on dry-run + options.dryRun || + // don't publish comment regardless if it succeeded or failed + (!publishStatusCommentOnFailure && !publishStatusCommentOnSuccess) || // dont publish comment if all backports suceeded (didAllBackportsSucceed && !publishStatusCommentOnSuccess) || // dont publish comment if some failed - (!didAllBackportsSucceed && !publishStatusCommentOnFailure) + (!didAllBackportsSucceed && !publishStatusCommentOnFailure) || + (backportResponse.status === 'aborted' && !publishStatusCommentOnAbort) ) { return; } diff --git a/src/options/ConfigOptions.ts b/src/options/ConfigOptions.ts index 50640da3..10656049 100644 --- a/src/options/ConfigOptions.ts +++ b/src/options/ConfigOptions.ts @@ -51,8 +51,9 @@ type Options = Partial<{ prFilter: string; projectConfigFile: string; // only available via cli and module options (not project or global config) prTitle: string; - publishStatusCommentOnSuccess: boolean; + publishStatusCommentOnAbort: boolean; publishStatusCommentOnFailure: boolean; + publishStatusCommentOnSuccess: boolean; pullNumber: number; repoForkOwner: string; repoName: string; diff --git a/src/options/cliArgs.ts b/src/options/cliArgs.ts index e9f2b86c..381724e7 100644 --- a/src/options/cliArgs.ts +++ b/src/options/cliArgs.ts @@ -460,6 +460,7 @@ export function getOptionsFromCliArgs(processArgs: readonly string[]) { noVerify: verify ?? noVerify, publishStatusCommentOnSuccess: noStatusComment === true ? false : undefined, publishStatusCommentOnFailure: noStatusComment === true ? false : undefined, + publishStatusCommentOnAbort: noStatusComment === true ? false : undefined, interactive: nonInteractive === true ? false : undefined, }); } diff --git a/src/options/options.test.ts b/src/options/options.test.ts index 74bd6a35..5288ec66 100644 --- a/src/options/options.test.ts +++ b/src/options/options.test.ts @@ -225,8 +225,9 @@ describe('getOptions', () => { multipleBranches: true, multipleCommits: false, noVerify: true, + publishStatusCommentOnAbort: false, publishStatusCommentOnSuccess: true, - publishStatusCommentOnFailure: true, + publishStatusCommentOnFailure: false, repoForkOwner: 'john.diller', repoName: 'kibana', repoOwner: 'elastic', diff --git a/src/options/options.ts b/src/options/options.ts index d451897f..e708223e 100644 --- a/src/options/options.ts +++ b/src/options/options.ts @@ -43,8 +43,9 @@ export const defaultConfigOptions = { multipleBranches: true, multipleCommits: false, noVerify: true, + publishStatusCommentOnAbort: false, publishStatusCommentOnSuccess: true, - publishStatusCommentOnFailure: true, + publishStatusCommentOnFailure: false, resetAuthor: false, reviewers: [] as Array, sourcePRLabels: [] as string[],