Skip to content
This repository has been archived by the owner on Apr 26, 2023. It is now read-only.

Commit

Permalink
fix: correct reading of sync-labels input.
Browse files Browse the repository at this point in the history
Contrary to the assumptions made in the unit tests, core.getInput
always returns a string, and !!'false' is true.

Fixes actions#112
  • Loading branch information
adam-azarchs committed Jan 11, 2023
1 parent 36adcc2 commit 6e9bf21
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('run', () => {
let mockInput = {
'repo-token': 'foo',
'configuration-path': 'bar',
'sync-labels': true
'sync-labels': 'true'
};

jest
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('run', () => {
let mockInput = {
'repo-token': 'foo',
'configuration-path': 'bar',
'sync-labels': false
'sync-labels': 'false'
};

jest
Expand Down
15 changes: 14 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function run() {
try {
const token = core.getInput('repo-token', { required: true });
const configPath = core.getInput('configuration-path', { required: true });
const syncLabels = !!core.getInput('sync-labels', { required: false });
const syncLabels = parseBool(core.getInput('sync-labels', { required: false }));
const prNumber = getPrNumber();
if (!prNumber) {
console.log('Could not get pull request number from context, exiting');
Expand Down Expand Up @@ -89,6 +89,19 @@ function run() {
});
}
exports.run = run;
// parseBool returns true if the input value is a valid truth value in yaml.
// Unlike core.getBooleanInput, it also accepts an empty string,
// for the sake of backwards compatibility.
function parseBool(val) {
const trueValue = ['true', 'True', 'TRUE', true];
const falseValue = ['false', 'False', 'FALSE', '', null, undefined, false];
if (trueValue.includes(val))
return true;
if (falseValue.includes(val))
return false;
throw new TypeError(`Input does not meet YAML 1.2 "Core Schema" specification: ${name}\n` +
`Support boolean input list: \`true | True | TRUE | false | False | FALSE\``);
}
function getPrNumber() {
const pullRequest = github.context.payload.pull_request;
if (!pullRequest) {
Expand Down
18 changes: 17 additions & 1 deletion src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export async function run() {
try {
const token = core.getInput('repo-token', {required: true});
const configPath = core.getInput('configuration-path', {required: true});
const syncLabels = !!core.getInput('sync-labels', {required: false});
const syncLabels = parseBool(
core.getInput('sync-labels', {required: false})
);

const prNumber = getPrNumber();
if (!prNumber) {
Expand Down Expand Up @@ -62,6 +64,20 @@ export async function run() {
}
}

// parseBool returns true if the input value is a valid truth value in yaml.
// Unlike core.getBooleanInput, it also accepts an empty string,
// for the sake of backwards compatibility.
function parseBool(val: string) {
const trueValue = ['true', 'True', 'TRUE', true];
const falseValue = ['false', 'False', 'FALSE', '', null, undefined, false];
if (trueValue.includes(val)) return true;
if (falseValue.includes(val)) return false;
throw new TypeError(
`Input does not meet YAML 1.2 "Core Schema" specification: ${name}\n` +
`Support boolean input list: \`true | True | TRUE | false | False | FALSE\``
);
}

function getPrNumber(): number | undefined {
const pullRequest = github.context.payload.pull_request;
if (!pullRequest) {
Expand Down

0 comments on commit 6e9bf21

Please sign in to comment.