From ce3ba6e4830f4c52a5cb2017cd42732c9328396f Mon Sep 17 00:00:00 2001 From: Luke Lalor Date: Thu, 20 Jun 2024 18:42:17 -0700 Subject: [PATCH] add a flag to error if missing --- README.md | 1 + src/main.js | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 122e774..a9f09f2 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ jobs: | username | Obtained from the context by default, can also be customized to pass in | string | ✖ | | check-bot | Check whether the user is a bot | boolean | ✖ | | check-contributor | Check whether the user is contributor | boolean | ✖ | +| error-if-missing | Error if require or check if false | boolean | ✖ | - User permission: `admin` > `write` > `read` - `username` support [github context](https://docs.github.com/en/actions/learn-github-actions/contexts#github-context) diff --git a/src/main.js b/src/main.js index fca30da..b6c2361 100644 --- a/src/main.js +++ b/src/main.js @@ -50,14 +50,14 @@ async function run() { return contributors; } - if (checkBot == 'true') { + if (checkBot === 'true') { const { data } = await octokit.users.getByUsername({ username, }); if (data.type === 'Bot') { checkResult = true; } - } else if (checkContributor == 'true') { + } else if (checkContributor === 'true') { let contributors = await queryContributors(); contributors = contributors.map(({ login }) => login); if (contributors.length) { @@ -65,18 +65,27 @@ async function run() { } } + checkFailed = false; if (checkBot || checkContributor) { core.info(`[Action Check] The check result is ${checkResult}.`); core.setOutput('check-result', checkResult); + checkFailed = checkFailed || !checkResult; } if (require) { requireResult = checkPermission(require, permission); core.info(`[Action Require] The ${username} permission check is ${requireResult}.`); core.setOutput('require-result', requireResult); + checkFailed = checkFailed || !requireResult; + } + + const errorIfMissing = core.getInput('error-if-missing'); + if (checkFailed && (errorIfMissing === 'true')) { + core.setFailed('The required check failed.'); } core.info(THANKS); + } catch (error) { core.setFailed(error.message); }