From 05d1a59331ad92e2bb8c10cfe83bd4b27404a520 Mon Sep 17 00:00:00 2001 From: Jonas Schubert Date: Mon, 1 Jul 2024 10:08:58 +0200 Subject: [PATCH] feat(commenting): disable if set to false --- README.md | 4 ++-- lib/fail.js | 2 ++ lib/success.js | 2 ++ test/fail.test.js | 15 +++++++++++++++ test/success.test.js | 16 ++++++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 41518fbd..0ae85479 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ The success comment condition is generated with [Lodash template](https://lodash ##### successCommentCondition example -- do no create any comments at all: `"<% return false; %>"` +- do no create any comments at all: set to `false` or templating: `"<% return false; %>"` - to only comment on issues: `"<% return issue %>"` - to only comment on merge requests: `"<% return mergeRequest %>"` - you can use labels to filter issues: `"<% return issue.labels?.includes('semantic-release-relevant') %>"` @@ -204,7 +204,7 @@ The fail comment condition is generated with [Lodash template](https://lodash.co ##### failCommentCondition example -- do no create any comments at all: `"<% return false; %>"` +- do no create any comments at all: set to `false` or templating: `"<% return false; %>"` - to only comment on main branch: `"<% return branch.name === 'main' %>"` - you can use labels to filter issues, i.e. to not comment if the issue is labeled with `wip`: `"<% return !issue.labels?.includes('wip') %>"` diff --git a/lib/fail.js b/lib/fail.js index 1421da3f..018f4519 100644 --- a/lib/fail.js +++ b/lib/fail.js @@ -24,6 +24,8 @@ export default async (pluginConfig, context) => { logger.log("Skip issue creation."); logger.error(`Failure reporting should be disabled via 'failCommentCondition'. Using 'false' for 'failComment' or 'failTitle' is deprecated and will be removed in a future major version.`); + } else if (failCommentCondition === false) { + logger.log("Skip issue creation."); } else { const encodedFailTitle = encodeURIComponent(failTitle); const description = failComment ? template(failComment)({ branch, errors }) : getFailComment(branch, errors); diff --git a/lib/success.js b/lib/success.js index 891d94a1..910f0201 100644 --- a/lib/success.js +++ b/lib/success.js @@ -27,6 +27,8 @@ export default async (pluginConfig, context) => { logger.log("Skip commenting on issues and pull requests."); logger.error(`Issue and pull request comments should be disabled via 'successCommentCondition'. Using 'false' for 'successComment' is deprecated and will be removed in a future major version.`); + } else if (successCommentCondition === false) { + logger.log("Skip commenting on issues and pull requests."); } else { const releaseInfos = releases.filter((release) => Boolean(release.name)); try { diff --git a/test/fail.test.js b/test/fail.test.js index 30c69889..9c6f52df 100644 --- a/test/fail.test.js +++ b/test/fail.test.js @@ -346,3 +346,18 @@ test.serial("Post new issue if none exists yet with disabled comment on existing "https://gitlab.com/test_user/test_repo/-/issues/3", ]); }); + +test.serial("Does not post comments when failCommentCondition is set to false", async (t) => { + const owner = "test_user"; + const repo = "test_repo"; + const env = { GITLAB_TOKEN: "gitlab_token" }; + const pluginConfig = { failCommentCondition: false }; + const branch = { name: "main" }; + const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` }; + const errors = [{ message: "An error occured" }]; + const gitlab = authenticate(env); + + await fail(pluginConfig, { env, options, branch, errors, logger: t.context.logger }); + + t.true(gitlab.isDone()); +}); diff --git a/test/success.test.js b/test/success.test.js index 4276367e..9e6272f3 100644 --- a/test/success.test.js +++ b/test/success.test.js @@ -289,3 +289,19 @@ test.serial( t.true(gitlab.isDone()); } ); + +test.serial("Does not post comments when successCommentCondition is set to false", async (t) => { + const owner = "test_user"; + const repo = "test_repo"; + const env = { GITLAB_TOKEN: "gitlab_token" }; + const pluginConfig = { successCommentCondition: false }; + const nextRelease = { version: "1.0.0" }; + const releases = [{ name: RELEASE_NAME, url: "https://gitlab.com/test_user/test_repo/-/releases/v1.0.0" }]; + const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` }; + const commits = [{ hash: "abcdef" }, { hash: "fedcba" }]; + const gitlab = authenticate(env); + + await success(pluginConfig, { env, options, nextRelease, logger: t.context.logger, commits, releases }); + + t.true(gitlab.isDone()); +});