Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evaluate truthy values for addLatest #229

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -46,7 +46,7 @@ const createTags = (addLatest, addTimestamp) => {
);
}

if (addLatest) {
if (asBool(addLatest)) {
dockerTags.push('latest');
}

Expand Down
24 changes: 23 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
12 changes: 12 additions & 0 deletions tests/docker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
31 changes: 30 additions & 1 deletion tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
Loading