From a893e392b0c0ec79268c129c4da5dcd62f3a7b5e Mon Sep 17 00:00:00 2001 From: Christopher Vachon <5806093+CodeVachon@users.noreply.github.com> Date: Thu, 4 Apr 2024 13:29:29 -0400 Subject: [PATCH] Add a function to ensure that booleans passed as strings are evaluated as booleans (#229) --- src/docker.js | 4 ++-- src/utils.js | 24 +++++++++++++++++++++++- tests/docker.test.js | 12 ++++++++++++ tests/utils.test.js | 31 ++++++++++++++++++++++++++++++- 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/docker.js b/src/docker.js index 01de483..40b3672 100644 --- a/src/docker.js +++ b/src/docker.js @@ -3,7 +3,7 @@ const core = require('@actions/core'); const fs = require('fs'); const { context } = require('@actions/github'); const { isGitHubTag, isBranch, isPullRequest, branchRefToSlug, prRefToSlug, tagRefToSlug } = require('./github'); -const { timestamp, cpOptions } = require('./utils'); +const { timestamp, cpOptions, asBool } = require('./utils'); const GITHUB_REGISTRY_URLS = ['docker.pkg.github.com', 'ghcr.io']; @@ -46,7 +46,7 @@ const createTags = (addLatest, addTimestamp) => { ); } - if (addLatest) { + if (asBool(addLatest)) { dockerTags.push('latest'); } diff --git a/src/utils.js b/src/utils.js index b741fa3..609985c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -14,8 +14,30 @@ const cpOptions = { stdio: 'inherit' }; +/** + * Convert a value to a boolean + * @param {*} value the value to convert + * @returns boolean value + */ +const asBool = value => { + switch ( + String(value ?? '') + .trim() + .toLowerCase() + ) { + case 'true': + case 'y': + case 'yes': + case '1': + return true; + default: + return false; + } +}; + module.exports = { timestamp, parseArray, - cpOptions + cpOptions, + asBool }; diff --git a/tests/docker.test.js b/tests/docker.test.js index b35c527..d73967f 100644 --- a/tests/docker.test.js +++ b/tests/docker.test.js @@ -43,6 +43,18 @@ describe('Create Docker image tags', () => { expect(tags.length).toEqual(2); }); + test('Create from tag push with addLatest passed as string', () => { + context.ref = 'refs/tags/v1.0'; + context.sha = '8d93430eddafb926c668181c71f579556f68668c'; + addLatest = 'true'; + + const tags = docker.createTags(addLatest, addTimestamp); + + expect(tags).toContain('v1.0'); + expect(tags).toContain('latest'); + expect(tags.length).toEqual(2); + }); + test('Create from tag push with addTimestamp', () => { context.ref = 'refs/tags/v1.0'; context.sha = '8d93430eddafb926c668181c71f579556f68668c'; diff --git a/tests/utils.test.js b/tests/utils.test.js index f74eb23..3267bf9 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -1,4 +1,4 @@ -const { parseArray } = require('../src/utils'); +const { parseArray, asBool } = require('../src/utils'); describe('Parse a comma-delimited strings', () => { test('Parse string with spaces and return an array', () => { @@ -37,3 +37,32 @@ describe('Parse a comma-delimited strings', () => { expect(value).toEqual(undefined); }); }); + +describe('Convert a value to a boolean', () => { + test.each` + value | as | expected + ${true} | ${'boolean'} | ${true} + ${false} | ${'boolean'} | ${false} + ${'true'} | ${'string'} | ${true} + ${'false'} | ${'string'} | ${false} + ${'1'} | ${'string'} | ${true} + ${'0'} | ${'string'} | ${false} + ${'-1'} | ${'string'} | ${false} + ${'N'} | ${'string'} | ${false} + ${'n'} | ${'string'} | ${false} + ${'No'} | ${'string'} | ${false} + ${'no'} | ${'string'} | ${false} + ${'Y'} | ${'string'} | ${true} + ${'y'} | ${'string'} | ${true} + ${'Yes'} | ${'string'} | ${true} + ${'yes'} | ${'string'} | ${true} + ${1} | ${'number'} | ${true} + ${0} | ${'number'} | ${false} + ${-1} | ${'number'} | ${false} + ${undefined} | ${'undefined'} | ${false} + `(`returns $expected for '$value' as $as`, ({ value, expected }) => { + const result = asBool(value); + expect(typeof result).toBe('boolean'); + expect(result).toEqual(expected); + }); +});