Skip to content

Commit f16f6d7

Browse files
authored
Coerce action inputs since all come as string (#2)
* Coerce action inputs since all come as string * Bump version * Fix workflow
1 parent 5e2d7e4 commit f16f6d7

File tree

7 files changed

+157
-23
lines changed

7 files changed

+157
-23
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: build
22
on:
33
pull_request:
4-
branches-ignore:
4+
branches:
55
- master
66
paths:
77
- '**.js'

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Security - in case of vulnerabilities.
1818

1919
_TBD_
2020

21-
## [1.0.0] yyyy-mm-dd
21+
## [1.0.1] 2019-11-11
22+
### Fixed
23+
24+
- Fixed the inputs types at which the application did not work correctly, especially for booleans.
25+
26+
## [1.0.0] 2019-11-11
2227

2328
Initial release.

dist/index.js

+41-7
Original file line numberDiff line numberDiff line change
@@ -15766,15 +15766,23 @@ const {
1576615766
readMetric,
1576715767
generateStatus,
1576815768
generateTable,
15769+
loadConfig,
1576915770
} = __webpack_require__(858);
1577015771

1577115772
async function run() {
1577215773
if (!github.context.payload.pull_request) {
1577315774
throw new Error('Action supports only pull_request event');
1577415775
}
1577515776

15776-
const comment = core.getInput('comment');
15777-
const check = core.getInput('check');
15777+
const {
15778+
comment,
15779+
check,
15780+
githubToken,
15781+
cloverFile,
15782+
thresholdAlert,
15783+
thresholdWarning,
15784+
statusContext,
15785+
} = loadConfig(core);
1577815786

1577915787
if (!check && !comment) {
1578015788
return;
@@ -15789,17 +15797,12 @@ async function run() {
1578915797
after: sha,
1579015798
} = context.payload;
1579115799

15792-
const githubToken = core.getInput('github_token');
1579315800
const client = new github.GitHub(githubToken);
1579415801

15795-
const cloverFile = core.getInput('clover_file');
15796-
const thresholdAlert = core.getInput('threshold_alert');
15797-
const thresholdWarning = core.getInput('threshold_warning');
1579815802
const coverage = await readFile(cloverFile);
1579915803
const metric = readMetric(coverage, { thresholdAlert, thresholdWarning });
1580015804

1580115805
if (check) {
15802-
const statusContext = core.getInput('status_context');
1580315806
client.repos.createStatus({
1580415807
...context.repo,
1580515808
sha,
@@ -19902,6 +19905,36 @@ function generateStatus({
1990219905
};
1990319906
}
1990419907

19908+
function toBool(value) {
19909+
return typeof value === 'boolean'
19910+
? value
19911+
: value === 'true';
19912+
}
19913+
19914+
function toInt(value) {
19915+
return value * 1;
19916+
}
19917+
19918+
function loadConfig({ getInput }) {
19919+
const comment = toBool(getInput('comment'));
19920+
const check = toBool(getInput('check'));
19921+
const githubToken = getInput('github_token');
19922+
const cloverFile = getInput('clover_file');
19923+
const thresholdAlert = toInt(getInput('threshold_alert'));
19924+
const thresholdWarning = toInt(getInput('threshold_warning'));
19925+
const statusContext = getInput('status_context');
19926+
19927+
return {
19928+
comment,
19929+
check,
19930+
githubToken,
19931+
cloverFile,
19932+
thresholdAlert,
19933+
thresholdWarning,
19934+
statusContext,
19935+
};
19936+
}
19937+
1990519938
module.exports = {
1990619939
readFile,
1990719940
readMetric,
@@ -19910,6 +19943,7 @@ module.exports = {
1991019943
generateTable,
1991119944
calculateLevel,
1991219945
generateStatus,
19946+
loadConfig,
1991319947
};
1991419948

1991519949

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coverage-monitor-action",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"private": true,
55
"description": "A GitHub Action that monitor coverage.",
66
"main": "src/index.js",

src/functions.js

+31
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@ function generateStatus({
126126
};
127127
}
128128

129+
function toBool(value) {
130+
return typeof value === 'boolean'
131+
? value
132+
: value === 'true';
133+
}
134+
135+
function toInt(value) {
136+
return value * 1;
137+
}
138+
139+
function loadConfig({ getInput }) {
140+
const comment = toBool(getInput('comment'));
141+
const check = toBool(getInput('check'));
142+
const githubToken = getInput('github_token');
143+
const cloverFile = getInput('clover_file');
144+
const thresholdAlert = toInt(getInput('threshold_alert'));
145+
const thresholdWarning = toInt(getInput('threshold_warning'));
146+
const statusContext = getInput('status_context');
147+
148+
return {
149+
comment,
150+
check,
151+
githubToken,
152+
cloverFile,
153+
thresholdAlert,
154+
thresholdWarning,
155+
statusContext,
156+
};
157+
}
158+
129159
module.exports = {
130160
readFile,
131161
readMetric,
@@ -134,4 +164,5 @@ module.exports = {
134164
generateTable,
135165
calculateLevel,
136166
generateStatus,
167+
loadConfig,
137168
};

src/index.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ const {
55
readMetric,
66
generateStatus,
77
generateTable,
8+
loadConfig,
89
} = require('./functions');
910

1011
async function run() {
1112
if (!github.context.payload.pull_request) {
1213
throw new Error('Action supports only pull_request event');
1314
}
1415

15-
const comment = core.getInput('comment');
16-
const check = core.getInput('check');
16+
const {
17+
comment,
18+
check,
19+
githubToken,
20+
cloverFile,
21+
thresholdAlert,
22+
thresholdWarning,
23+
statusContext,
24+
} = loadConfig(core);
1725

1826
if (!check && !comment) {
1927
return;
@@ -28,17 +36,12 @@ async function run() {
2836
after: sha,
2937
} = context.payload;
3038

31-
const githubToken = core.getInput('github_token');
3239
const client = new github.GitHub(githubToken);
3340

34-
const cloverFile = core.getInput('clover_file');
35-
const thresholdAlert = core.getInput('threshold_alert');
36-
const thresholdWarning = core.getInput('threshold_warning');
3741
const coverage = await readFile(cloverFile);
3842
const metric = readMetric(coverage, { thresholdAlert, thresholdWarning });
3943

4044
if (check) {
41-
const statusContext = core.getInput('status_context');
4245
client.repos.createStatus({
4346
...context.repo,
4447
sha,

tests/functions.test.js

+67-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('functions', () => {
1010
await expect(parser.readFile(filename)).rejects.toThrow('no such file or directory');
1111
});
1212

13-
it('parse XML to JS', async () => {
13+
it('parses XML to JS', async () => {
1414
expect.hasAssertions();
1515

1616
const filename = path.join(__dirname, '/clover.xml');
@@ -40,7 +40,7 @@ describe('functions', () => {
4040
expect(metric.level).toStrictEqual('yellow'); // 79.59 < 90
4141
});
4242

43-
it('calculate level', async () => {
43+
it('calculates level', async () => {
4444
expect.hasAssertions();
4545

4646
[
@@ -56,7 +56,7 @@ describe('functions', () => {
5656
);
5757
});
5858

59-
it('generate status', async () => {
59+
it('generates status', async () => {
6060
expect.hasAssertions();
6161
const targetUrl = 'https://example.com';
6262
const statusContext = 'coverage';
@@ -96,7 +96,7 @@ describe('functions', () => {
9696
});
9797
});
9898

99-
it('generate badge URL', async () => {
99+
it('generates badge URL', async () => {
100100
expect.hasAssertions();
101101

102102
const metric = {
@@ -107,13 +107,13 @@ describe('functions', () => {
107107
expect(parser.generateBadgeUrl(metric)).toStrictEqual('https://img.shields.io/static/v1?label=coverage&message=9%&color=green');
108108
});
109109

110-
it('generate emoji', async () => {
110+
it('generates emoji', async () => {
111111
expect.hasAssertions();
112112
expect(parser.generateEmoji({ lines: { rate: 100 } })).toStrictEqual(' 🎉');
113113
expect(parser.generateEmoji({ lines: { rate: 99.99 } })).toStrictEqual('');
114114
});
115115

116-
it('generate table', async () => {
116+
it('generates table', async () => {
117117
expect.hasAssertions();
118118

119119
const metric = {
@@ -151,4 +151,65 @@ describe('functions', () => {
151151

152152
expect(parser.generateTable(metric)).toStrictEqual(expectedString);
153153
});
154+
155+
function createConfigReader(inputs) {
156+
return {
157+
getInput(name) {
158+
return inputs[
159+
name.split('_').reduce(
160+
(carry, item) => (carry === null ? item : `${carry}${item[0].toUpperCase() + item.slice(1)}`),
161+
null,
162+
)
163+
];
164+
},
165+
};
166+
}
167+
168+
it('loads config', async () => {
169+
expect.hasAssertions();
170+
171+
const inputs = {
172+
comment: true,
173+
check: false,
174+
githubToken: '***',
175+
cloverFile: 'clover.xml',
176+
thresholdAlert: 10,
177+
thresholdWarning: 20,
178+
statusContext: 'Coverage',
179+
};
180+
181+
const reader = createConfigReader(inputs);
182+
const config = parser.loadConfig(reader);
183+
184+
expect(config).toStrictEqual(inputs);
185+
});
186+
187+
it('coerces config values', async () => {
188+
expect.hasAssertions();
189+
190+
const inputs = {
191+
comment: 'true',
192+
check: 'false',
193+
githubToken: '***',
194+
cloverFile: 'clover.xml',
195+
thresholdAlert: '10',
196+
thresholdWarning: '20',
197+
statusContext: 'Coverage',
198+
};
199+
200+
const expected = {
201+
comment: true,
202+
check: false,
203+
githubToken: '***',
204+
cloverFile: 'clover.xml',
205+
thresholdAlert: 10,
206+
thresholdWarning: 20,
207+
statusContext: 'Coverage',
208+
};
209+
210+
const reader = createConfigReader(inputs);
211+
const config = parser.loadConfig(reader);
212+
213+
expect(config).toStrictEqual(expected);
214+
});
154215
});

0 commit comments

Comments
 (0)