From c23605fcb2b0807911f30bf19060ddeda84e105a Mon Sep 17 00:00:00 2001 From: yi_Xu Date: Thu, 18 Feb 2021 03:56:51 +0800 Subject: [PATCH] fix: get the correct boolean value of the input (#233) --- action.yml | 10 ++++++ index.js | 41 +++++++++++++----------- test/release-please.js | 72 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 96 insertions(+), 27 deletions(-) diff --git a/action.yml b/action.yml index f94f5802..c47dc731 100644 --- a/action.yml +++ b/action.yml @@ -9,9 +9,11 @@ inputs: fork: description: 'should the PR be proposed from a fork (does not work with secrets.GITHUB_TOKEN)' required: false + default: false clean: description: 'Should stale release PRs be closed post release? Defaults to true' required: false + default: true package-name: description: 'name of the distributions releases are being created for, e.g., "name" in package.json, or "setup.py"' required: true @@ -21,27 +23,35 @@ inputs: bump-minor-pre-major: description: 'should breaking changes before 1.0.0 produce minor bumps' required: false + default: false path: description: "create a release from a path other than the repository's root" required: false + default: '' monorepo-tags: description: 'add prefix to tags and branches, allowing multiple libraries to be released from the same repository' required: false + default: false changelog-path: description: 'specify a CHANGELOG path other than the root CHANGELOG.md' required: false + default: '' changelog-types: description: 'changlelog commit types' required: false + default: '' command: description: 'release-please command to run, either "github-release", or "release-pr" (defaults to running both)' required: false + default: '' version-file: description: 'provide a path to a version file to increment (used by ruby releaser)' required: false + default: '' default-branch: description: 'branch to open pull release PR against (detected by default)' required: false + default: '' runs: using: 'node12' main: 'dist/index.js' diff --git a/index.js b/index.js index 6f7ec56e..b8cd7b33 100644 --- a/index.js +++ b/index.js @@ -5,25 +5,29 @@ const RELEASE_LABEL = 'autorelease: pending' const GITHUB_RELEASE_COMMAND = 'github-release' const GITHUB_RELEASE_PR_COMMAND = 'release-pr' +function getBooleanInput (input) { + const trueValue = ['true', 'True', 'TRUE', 'yes', 'Yes', 'YES', 'y', 'Y', 'on', 'On', 'ON'] + const falseValue = ['false', 'False', 'FALSE', 'no', 'No', 'NO', 'n', 'N', 'off', 'Off', 'OFF'] + const stringInput = core.getInput(input) + if (trueValue.indexOf(stringInput) > -1) return true + if (falseValue.indexOf(stringInput) > -1) return false + throw TypeError(`Wrong boolean value of the input '${input}'`) +} + async function main () { - const bumpMinorPreMajor = Boolean(core.getInput('bump-minor-pre-major')) - const monorepoTags = Boolean(core.getInput('monorepo-tags')) - const packageName = core.getInput('package-name') - const path = core.getInput('path') ? core.getInput('path') : undefined - const releaseType = core.getInput('release-type') - const token = core.getInput('token') - const fork = core.getInput('fork') ? true : undefined - const changelogPath = core.getInput('changelog-path') ? core.getInput('changelog-path') : undefined + const bumpMinorPreMajor = getBooleanInput('bump-minor-pre-major') + const monorepoTags = getBooleanInput('monorepo-tags') + const packageName = core.getInput('package-name', { required: true }) + const path = core.getInput('path') || undefined + const releaseType = core.getInput('release-type', { required: true }) + const token = core.getInput('token', { required: true }) + const fork = getBooleanInput('fork') + const changelogPath = core.getInput('changelog-path') || undefined const changelogTypes = core.getInput('changelog-types') - const command = core.getInput('command') ? core.getInput('command') : undefined - const versionFile = core.getInput('version-file') ? core.getInput('version-file') : undefined - const defaultBranch = core.getInput('default-branch') ? core.getInput('default-branch') : undefined - - // Parse the changelogTypes if there are any - let changelogSections - if (changelogTypes) { - changelogSections = JSON.parse(changelogTypes) - } + const changelogSections = changelogTypes && JSON.parse(changelogTypes) + const command = core.getInput('command') || undefined + const versionFile = core.getInput('version-file') || undefined + const defaultBranch = core.getInput('default-branch') || undefined // First we check for any merged release PRs (PRs merged with the label // "autorelease: pending"): @@ -74,7 +78,8 @@ async function main () { } const releasePlease = { - main + main, + getBooleanInput } if (require.main === module) { diff --git a/test/release-please.js b/test/release-please.js index f7af2f99..5be42cde 100644 --- a/test/release-please.js +++ b/test/release-please.js @@ -5,6 +5,19 @@ const core = require('@actions/core') const sinon = require('sinon') const { factory, GitHubRelease } = require('release-please/build/src') const { Node } = require('release-please/build/src/releasers/node') +// As defined in action.yml +const defaultInput = { + fork: 'false', + clean: 'true', + 'bump-minor-pre-major': 'false', + path: '', + 'monorepo-tags': 'false', + 'changelog-path': '', + 'changelog-types': '', + command: '', + 'version-file': '', + 'default-branch': '' +} const sandbox = sinon.createSandbox() process.env.GITHUB_REPOSITORY = 'google/cloud' @@ -14,6 +27,47 @@ describe('release-please-action', () => { sandbox.restore() }) + const trueValue = ['true', 'True', 'TRUE', 'yes', 'Yes', 'YES', 'y', 'Y', 'on', 'On', 'ON'] + const falseValue = ['false', 'False', 'FALSE', 'no', 'No', 'NO', 'n', 'N', 'off', 'Off', 'OFF'] + + trueValue.forEach(value => { + it(`get the boolean true with the input of '${value}'`, () => { + const input = { + fork: value + } + core.getInput = (name) => { + return input[name] || defaultInput[name] + } + const actual = action.getBooleanInput('fork') + assert.strictEqual(actual, true) + }) + }) + + falseValue.forEach(value => { + it(`get the boolean with the input of '${value}'`, () => { + const input = { + fork: value + } + core.getInput = (name) => { + return input[name] || defaultInput[name] + } + const actual = action.getBooleanInput('fork') + assert.strictEqual(actual, false) + }) + }) + + it('get an error when inputting the wrong boolean value', () => { + const input = { + fork: 'wrong' + } + core.getInput = (name) => { + return input[name] || defaultInput[name] + } + assert.throws(() => { + action.getBooleanInput('fork') + }, { name: 'TypeError', message: 'Wrong boolean value of the input \'fork\'' }) + }) + it('both opens PR to the default branch and tags GitHub releases by default', async () => { const output = {} core.setOutput = (name, value) => { @@ -23,7 +77,7 @@ describe('release-please-action', () => { 'release-type': 'node' } core.getInput = (name) => { - return input[name] + return input[name] || defaultInput[name] } const runCommandStub = sandbox.stub(factory, 'runCommand') @@ -61,7 +115,7 @@ describe('release-please-action', () => { 'default-branch': 'dev' } core.getInput = (name) => { - return input[name] + return input[name] || defaultInput[name] } const runCommandStub = sandbox.stub(factory, 'runCommand') @@ -100,7 +154,7 @@ describe('release-please-action', () => { command: 'release-pr' } core.getInput = (name) => { - return input[name] + return input[name] || defaultInput[name] } const runCommandStub = sandbox.stub(factory, 'runCommand') @@ -129,7 +183,7 @@ describe('release-please-action', () => { command: 'github-release' } core.getInput = (name) => { - return input[name] + return input[name] || defaultInput[name] } const runCommandStub = sandbox.stub(factory, 'runCommand') @@ -170,7 +224,7 @@ describe('release-please-action', () => { command: 'github-release' } core.getInput = (name) => { - return input[name] + return input[name] || defaultInput[name] } const runCommandStub = sandbox.stub(factory, 'runCommand') @@ -195,7 +249,7 @@ describe('release-please-action', () => { command: 'release-pr' } core.getInput = (name) => { - return input[name] + return input[name] || defaultInput[name] } const runCommandStub = sandbox.stub(factory, 'runCommand') @@ -217,7 +271,7 @@ describe('release-please-action', () => { command: 'release-pr' } core.getInput = (name) => { - return input[name] + return input[name] || defaultInput[name] } const runCommandStub = sandbox.stub(factory, 'runCommand') @@ -239,7 +293,7 @@ describe('release-please-action', () => { command: 'release-pr' } core.getInput = (name) => { - return input[name] + return input[name] || defaultInput[name] } await action.main() assert.ok(maybeReleasePR instanceof Node) @@ -255,7 +309,7 @@ describe('release-please-action', () => { command: 'github-release' } core.getInput = (name) => { - return input[name] + return input[name] || defaultInput[name] } await action.main() assert.ok(maybeGitHubRelease instanceof GitHubRelease)