diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 8ddc0af..ddfdbae 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -115,7 +115,7 @@ describe("run", () => { mockConfig.recreate = true const {core} = await runMain() expect(core.setFailed).toHaveBeenCalledWith( - "delete and recreate cannot be both set to true", + "delete and recreate cannot be set to true simultaneously", ) }) @@ -124,7 +124,7 @@ describe("run", () => { mockConfig.onlyCreateComment = true const {core} = await runMain() expect(core.setFailed).toHaveBeenCalledWith( - "delete and only_create cannot be both set to true", + "delete and only_create cannot be set to true simultaneously", ) }) @@ -133,7 +133,7 @@ describe("run", () => { mockConfig.hideOldComment = true const {core} = await runMain() expect(core.setFailed).toHaveBeenCalledWith( - "delete and hide cannot be both set to true", + "delete and hide cannot be set to true simultaneously", ) }) @@ -151,7 +151,16 @@ describe("run", () => { mockConfig.hideAndRecreate = true const {core} = await runMain() expect(core.setFailed).toHaveBeenCalledWith( - "hide and hide_and_recreate cannot be both set to true", + "hide and hide_and_recreate cannot be set to true simultaneously", + ) + }) + + test("fails when deleteOldComment and hideAndRecreate are both true", async () => { + mockConfig.deleteOldComment = true + mockConfig.hideAndRecreate = true + const {core} = await runMain() + expect(core.setFailed).toHaveBeenCalledWith( + "delete and hide_and_recreate cannot be set to true simultaneously", ) }) diff --git a/src/main.ts b/src/main.ts index 790bda7..aa165a3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -46,26 +46,22 @@ async function run(): Promise { throw new Error("Either message or path input is required") } - if (deleteOldComment && recreate) { - throw new Error("delete and recreate cannot be both set to true") - } - - if (deleteOldComment && onlyCreateComment) { - throw new Error("delete and only_create cannot be both set to true") - } - - if (deleteOldComment && hideOldComment) { - throw new Error("delete and hide cannot be both set to true") + const exclusiveModes: [string, boolean][] = [ + ["delete", deleteOldComment], + ["recreate", recreate], + ["only_create", onlyCreateComment], + ["hide", hideOldComment], + ["hide_and_recreate", hideAndRecreate], + ] + const enabledModes = exclusiveModes.filter(([, flag]) => flag).map(([name]) => name) + if (enabledModes.length > 1) { + throw new Error(`${enabledModes.join(" and ")} cannot be set to true simultaneously`) } if (onlyCreateComment && onlyUpdateComment) { throw new Error("only_create and only_update cannot be both set to true") } - if (hideOldComment && hideAndRecreate) { - throw new Error("hide and hide_and_recreate cannot be both set to true") - } - const octokit = github.getOctokit(githubToken) const previous = await findPreviousComment(octokit, repo, pullRequestNumber, header)