Skip to content

Commit 2dc606f

Browse files
authored
Refactor inputs as named parameters (#121)
We change the arguments of `parseInputs` and the `Inputs` constructor to receive an object with named arguments. This allows for adding new properties without breaking existing tests and more clarity on the inputs used on each test. Signed-off-by: Luiz Ferraz <luiz@lferraz.com>
1 parent 1a7e3de commit 2dc606f

File tree

5 files changed

+171
-99
lines changed

5 files changed

+171
-99
lines changed

dist/index.js

+51-20
Original file line numberDiff line numberDiff line change
@@ -32638,15 +32638,15 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
3263832638
exports.parseVerbs = exports.parseInputs = exports.MaybeInputs = exports.Inputs = void 0;
3263932639
const fs_1 = __importDefault(__nccwpck_require__(7147));
3264032640
class Inputs {
32641-
constructor(hasAdditionalVerbsInput, pathToAdditionalVerbs, allowOneLiners, additionalVerbs, maxSubjectLength, maxBodyLineLength, enforceSignOff, skipBodyCheck) {
32642-
this.hasAdditionalVerbsInput = hasAdditionalVerbsInput;
32643-
this.pathToAdditionalVerbs = pathToAdditionalVerbs;
32644-
this.allowOneLiners = allowOneLiners;
32645-
this.additionalVerbs = additionalVerbs;
32646-
this.maxSubjectLength = maxSubjectLength;
32647-
this.maxBodyLineLength = maxBodyLineLength;
32648-
this.enforceSignOff = enforceSignOff;
32649-
this.skipBodyCheck = skipBodyCheck;
32641+
constructor(values) {
32642+
this.hasAdditionalVerbsInput = values.hasAdditionalVerbsInput;
32643+
this.pathToAdditionalVerbs = values.pathToAdditionalVerbs;
32644+
this.allowOneLiners = values.allowOneLiners;
32645+
this.additionalVerbs = values.additionalVerbs;
32646+
this.maxSubjectLength = values.maxSubjectLength;
32647+
this.maxBodyLineLength = values.maxBodyLineLength;
32648+
this.enforceSignOff = values.enforceSignOff;
32649+
this.skipBodyCheck = values.skipBodyCheck;
3265032650
}
3265132651
}
3265232652
exports.Inputs = Inputs;
@@ -32670,7 +32670,8 @@ class MaybeInputs {
3267032670
}
3267132671
}
3267232672
exports.MaybeInputs = MaybeInputs;
32673-
function parseInputs(additionalVerbsInput, pathToAdditionalVerbsInput, allowOneLinersInput, maxSubjectLengthInput, maxBodyLineLengthInput, enforceSignOffInput, skipBodyCheckInput) {
32673+
function parseInputs(rawInputs) {
32674+
const { additionalVerbsInput = '', pathToAdditionalVerbsInput = '', allowOneLinersInput = '', maxSubjectLengthInput = '', maxBodyLineLengthInput = '', enforceSignOffInput = '', skipBodyCheckInput = '' } = rawInputs;
3267432675
const additionalVerbs = new Set();
3267532676
const hasAdditionalVerbsInput = additionalVerbsInput.length > 0;
3267632677
if (additionalVerbsInput) {
@@ -32723,7 +32724,16 @@ function parseInputs(additionalVerbsInput, pathToAdditionalVerbsInput, allowOneL
3272332724
return new MaybeInputs(null, 'Unexpected value for skip-body-check. ' +
3272432725
`Expected either 'true' or 'false', got: ${skipBodyCheckInput}`);
3272532726
}
32726-
return new MaybeInputs(new Inputs(hasAdditionalVerbsInput, pathToAdditionalVerbsInput, allowOneLiners, additionalVerbs, maxSubjectLength, maxBodyLineLength, enforceSignOff, skipBodyCheck), null);
32727+
return new MaybeInputs(new Inputs({
32728+
hasAdditionalVerbsInput,
32729+
pathToAdditionalVerbs: pathToAdditionalVerbsInput,
32730+
allowOneLiners,
32731+
additionalVerbs,
32732+
maxSubjectLength,
32733+
maxBodyLineLength,
32734+
enforceSignOff,
32735+
skipBodyCheck
32736+
}), null);
3272732737
}
3272832738
exports.parseInputs = parseInputs;
3272932739
function parseVerbs(text) {
@@ -33103,19 +33113,40 @@ const inspection = __importStar(__nccwpck_require__(7647));
3310333113
const represent = __importStar(__nccwpck_require__(4478));
3310433114
const input = __importStar(__nccwpck_require__(6747));
3310533115
function runWithExceptions() {
33106-
var _a, _b, _c, _d, _e, _f, _g;
3310733116
const messages = commitMessages.retrieve();
3310833117
////
3310933118
// Parse inputs
3311033119
////
33111-
const additionalVerbsInput = (_a = core.getInput('additional-verbs', { required: false })) !== null && _a !== void 0 ? _a : '';
33112-
const pathToAdditionalVerbsInput = (_b = core.getInput('path-to-additional-verbs', { required: false })) !== null && _b !== void 0 ? _b : '';
33113-
const allowOneLinersInput = (_c = core.getInput('allow-one-liners', { required: false })) !== null && _c !== void 0 ? _c : '';
33114-
const maxSubjectLengthInput = (_d = core.getInput('max-subject-line-length', { required: false })) !== null && _d !== void 0 ? _d : '';
33115-
const maxBodyLineLengthInput = (_e = core.getInput('max-body-line-length', { required: false })) !== null && _e !== void 0 ? _e : '';
33116-
const enforceSignOffInput = (_f = core.getInput('enforce-sign-off', { required: false })) !== null && _f !== void 0 ? _f : '';
33117-
const skipBodyCheckInput = (_g = core.getInput('skip-body-check', { required: false })) !== null && _g !== void 0 ? _g : '';
33118-
const maybeInputs = input.parseInputs(additionalVerbsInput, pathToAdditionalVerbsInput, allowOneLinersInput, maxSubjectLengthInput, maxBodyLineLengthInput, enforceSignOffInput, skipBodyCheckInput);
33120+
const additionalVerbsInput = core.getInput('additional-verbs', {
33121+
required: false
33122+
});
33123+
const pathToAdditionalVerbsInput = core.getInput('path-to-additional-verbs', {
33124+
required: false
33125+
});
33126+
const allowOneLinersInput = core.getInput('allow-one-liners', {
33127+
required: false
33128+
});
33129+
const maxSubjectLengthInput = core.getInput('max-subject-line-length', {
33130+
required: false
33131+
});
33132+
const maxBodyLineLengthInput = core.getInput('max-body-line-length', {
33133+
required: false
33134+
});
33135+
const enforceSignOffInput = core.getInput('enforce-sign-off', {
33136+
required: false
33137+
});
33138+
const skipBodyCheckInput = core.getInput('skip-body-check', {
33139+
required: false
33140+
});
33141+
const maybeInputs = input.parseInputs({
33142+
additionalVerbsInput,
33143+
pathToAdditionalVerbsInput,
33144+
allowOneLinersInput,
33145+
maxSubjectLengthInput,
33146+
maxBodyLineLengthInput,
33147+
enforceSignOffInput,
33148+
skipBodyCheckInput
33149+
});
3311933150
if (maybeInputs.error !== null) {
3312033151
core.error(maybeInputs.error);
3312133152
core.setFailed(maybeInputs.error);

src/__tests__/input.test.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ it('parses the inputs.', () => {
3030
throw new Error(`Unexpected readFileSync in the unit test from: ${path}`);
3131
};
3232

33-
const maybeInputs = input.parseInputs(
34-
'integrate\nanalyze',
35-
pathToVerbs,
36-
'true',
37-
'90',
38-
'100',
39-
'true',
40-
'true'
41-
);
33+
const maybeInputs = input.parseInputs({
34+
additionalVerbsInput: 'integrate\nanalyze',
35+
pathToAdditionalVerbsInput: pathToVerbs,
36+
allowOneLinersInput: 'true',
37+
maxSubjectLengthInput: '90',
38+
maxBodyLineLengthInput: '100',
39+
enforceSignOffInput: 'true',
40+
skipBodyCheckInput: 'true',
41+
});
4242

4343
expect(maybeInputs.error).toBeNull();
4444

src/__tests__/inspection.test.ts

+42-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as input from '../input';
22
import * as inspection from '../inspection';
33

44
const defaultInputs: input.Inputs = input
5-
.parseInputs('', '', '', '', '', '', '')
5+
.parseInputs({})
66
.mustInputs();
77

88
it('reports no errors on correct multi-line message.', () => {
@@ -28,7 +28,9 @@ it('reports no errors on OK multi-line message with allowed one-liners.', () =>
2828
});
2929

3030
it('reports no errors on OK single-line message with allowed one-liners.', () => {
31-
const inputs = input.parseInputs('', '', 'true', '', '', '', '').mustInputs();
31+
const inputs = input
32+
.parseInputs({allowOneLinersInput: 'true'})
33+
.mustInputs();
3234

3335
const message = 'Change SomeClass to OtherClass';
3436

@@ -64,13 +66,13 @@ it('reports no errors on any message when body check is disabled.', () => {
6466
'be checked.';
6567

6668
const inputCheckingBody = input
67-
.parseInputs('', '', '', '', '', '', '')
69+
.parseInputs({skipBodyCheckInput: 'false'})
6870
.mustInputs();
6971

7072
expect(inspection.check(message, inputCheckingBody)).not.toEqual([]);
7173

7274
const inputNotCheckingBody = input
73-
.parseInputs('', '', '', '', '', '', 'true')
75+
.parseInputs({skipBodyCheckInput: 'true'})
7476
.mustInputs();
7577

7678
expect(inspection.check(message, inputNotCheckingBody)).toEqual([]);
@@ -83,7 +85,9 @@ it('reports missing body with disallowed one-liners.', () => {
8385
});
8486

8587
it('reports missing body with allowed one-liners.', () => {
86-
const inputs = input.parseInputs('', '', 'true', '', '', '', '').mustInputs();
88+
const inputs = input
89+
.parseInputs({allowOneLinersInput: 'true'})
90+
.mustInputs();
8791

8892
const message = 'Change SomeClass to OtherClass\n';
8993
const errors = inspection.check(message, inputs);
@@ -170,7 +174,10 @@ it(
170174
'with additional verbs given as direct input.',
171175
() => {
172176
const inputs = input
173-
.parseInputs('table', '', 'false', '', '', '', '')
177+
.parseInputs({
178+
additionalVerbsInput: 'table',
179+
allowOneLinersInput: 'false'
180+
})
174181
.mustInputs();
175182

176183
const message =
@@ -204,16 +211,16 @@ it(
204211
'reports the subject starting with a non-verb ' +
205212
'with additional verbs given in a path.',
206213
() => {
207-
const inputs = new input.Inputs(
208-
false,
209-
'/some/path',
210-
false,
211-
new Set<string>('table'),
212-
50,
213-
72,
214-
false,
215-
false
216-
);
214+
const inputs = new input.Inputs({
215+
hasAdditionalVerbsInput: false,
216+
pathToAdditionalVerbs: '/some/path',
217+
allowOneLiners: false,
218+
additionalVerbs: new Set<string>('table'),
219+
maxSubjectLength: 50,
220+
maxBodyLineLength: 72,
221+
enforceSignOff: false,
222+
skipBodyCheck: false,
223+
});
217224

218225
const message =
219226
'Replaced SomeClass to OtherClass\n' +
@@ -244,7 +251,10 @@ it(
244251

245252
it('accepts the subject starting with an additional verb.', () => {
246253
const inputs = input
247-
.parseInputs('table', '', 'false', '', '', '', '')
254+
.parseInputs({
255+
additionalVerbsInput: 'table',
256+
allowOneLinersInput: 'false',
257+
})
248258
.mustInputs();
249259

250260
const message = 'Table that for me\n\nThis is a dummy commit.';
@@ -267,7 +277,9 @@ it('reports the subject ending in a dot.', () => {
267277
});
268278

269279
it('reports an incorrect one-line message with allowed one-liners.', () => {
270-
const inputs = input.parseInputs('', '', 'true', '', '', '', '').mustInputs();
280+
const inputs = input
281+
.parseInputs({allowOneLinersInput: 'true'})
282+
.mustInputs();
271283

272284
const message = 'Change SomeClass to OtherClass.';
273285

@@ -300,7 +312,9 @@ it('reports too long a subject line with custom max length.', () => {
300312
'This replaces the SomeClass with OtherClass in all of the module\n' +
301313
'since Some class was deprecated.';
302314

303-
const inputs = input.parseInputs('', '', '', '60', '', '', '').mustInputs();
315+
const inputs = input
316+
.parseInputs({maxSubjectLengthInput: '60'})
317+
.mustInputs();
304318

305319
const errors = inspection.check(message, inputs);
306320
expect(errors).toEqual([
@@ -334,7 +348,9 @@ it('reports too long a body line with custom max length.', () => {
334348
'This replaces the SomeClass with OtherClass in all of the module ' +
335349
'since Some class was deprecated.';
336350

337-
const inputs = input.parseInputs('', '', '', '', '90', '', '').mustInputs();
351+
const inputs = input
352+
.parseInputs({maxBodyLineLengthInput: '90'})
353+
.mustInputs();
338354

339355
const errors = inspection.check(message, inputs);
340356
expect(errors).toEqual([
@@ -493,7 +509,9 @@ The ${long} line is too long.`;
493509
});
494510

495511
it('accepts the valid body when enforcing the sign-off.', () => {
496-
const inputs = input.parseInputs('', '', '', '', '', 'true', '').mustInputs();
512+
const inputs = input
513+
.parseInputs({enforceSignOffInput: 'true'})
514+
.mustInputs();
497515

498516
const message = `Do something
499517
@@ -512,7 +530,9 @@ Signed-off-by: Somebody Else <some@body-else.com>
512530
});
513531

514532
it('rejects invalid sign-offs.', () => {
515-
const inputs = input.parseInputs('', '', '', '', '', 'true', '').mustInputs();
533+
const inputs = input
534+
.parseInputs({enforceSignOffInput: 'true'})
535+
.mustInputs();
516536

517537
const message = `Do something
518538

src/input.ts

+46-32
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
import fs from 'fs';
22

3-
export class Inputs {
3+
interface InputValues {
4+
hasAdditionalVerbsInput: boolean;
5+
pathToAdditionalVerbs: string;
6+
allowOneLiners: boolean;
7+
additionalVerbs: Set<string>;
8+
maxSubjectLength: number;
9+
maxBodyLineLength: number;
10+
enforceSignOff: boolean;
11+
skipBodyCheck: boolean;
12+
}
13+
14+
export class Inputs implements InputValues {
415
public hasAdditionalVerbsInput: boolean;
516
public pathToAdditionalVerbs: string;
617
public allowOneLiners: boolean;
718
public maxSubjectLength: number;
819
public maxBodyLineLength: number;
920
public skipBodyCheck: boolean;
1021

11-
// This is a complete appendix to the whiltelist parsed both from
22+
// This is a complete appendix to the whitelist parsed both from
1223
// the GitHub action input "additional-verbs" and from the file
1324
// specified by the input "path-to-additional-verbs".
1425
additionalVerbs: Set<string>;
1526

1627
public enforceSignOff: boolean;
1728

18-
constructor(
19-
hasAdditionalVerbsInput: boolean,
20-
pathToAdditionalVerbs: string,
21-
allowOneLiners: boolean,
22-
additionalVerbs: Set<string>,
23-
maxSubjectLength: number,
24-
maxBodyLineLength: number,
25-
enforceSignOff: boolean,
26-
skipBodyCheck: boolean
27-
) {
28-
this.hasAdditionalVerbsInput = hasAdditionalVerbsInput;
29-
this.pathToAdditionalVerbs = pathToAdditionalVerbs;
30-
this.allowOneLiners = allowOneLiners;
31-
this.additionalVerbs = additionalVerbs;
32-
this.maxSubjectLength = maxSubjectLength;
33-
this.maxBodyLineLength = maxBodyLineLength;
34-
this.enforceSignOff = enforceSignOff;
35-
this.skipBodyCheck = skipBodyCheck;
29+
constructor(values: InputValues) {
30+
this.hasAdditionalVerbsInput = values.hasAdditionalVerbsInput;
31+
this.pathToAdditionalVerbs = values.pathToAdditionalVerbs;
32+
this.allowOneLiners = values.allowOneLiners;
33+
this.additionalVerbs = values.additionalVerbs;
34+
this.maxSubjectLength = values.maxSubjectLength;
35+
this.maxBodyLineLength = values.maxBodyLineLength;
36+
this.enforceSignOff = values.enforceSignOff;
37+
this.skipBodyCheck = values.skipBodyCheck;
3638
}
3739
}
3840

@@ -66,15 +68,27 @@ export class MaybeInputs {
6668
}
6769
}
6870

69-
export function parseInputs(
70-
additionalVerbsInput: string,
71-
pathToAdditionalVerbsInput: string,
72-
allowOneLinersInput: string,
73-
maxSubjectLengthInput: string,
74-
maxBodyLineLengthInput: string,
75-
enforceSignOffInput: string,
76-
skipBodyCheckInput: string
77-
): MaybeInputs {
71+
interface RawInputs {
72+
additionalVerbsInput?: string;
73+
pathToAdditionalVerbsInput?: string;
74+
allowOneLinersInput?: string;
75+
maxSubjectLengthInput?: string;
76+
maxBodyLineLengthInput?: string;
77+
enforceSignOffInput?: string;
78+
skipBodyCheckInput?: string;
79+
}
80+
81+
export function parseInputs(rawInputs: RawInputs): MaybeInputs {
82+
const {
83+
additionalVerbsInput = '',
84+
pathToAdditionalVerbsInput = '',
85+
allowOneLinersInput = '',
86+
maxSubjectLengthInput = '',
87+
maxBodyLineLengthInput = '',
88+
enforceSignOffInput = '',
89+
skipBodyCheckInput = ''
90+
} = rawInputs;
91+
7892
const additionalVerbs = new Set<string>();
7993

8094
const hasAdditionalVerbsInput = additionalVerbsInput.length > 0;
@@ -162,16 +176,16 @@ export function parseInputs(
162176
}
163177

164178
return new MaybeInputs(
165-
new Inputs(
179+
new Inputs({
166180
hasAdditionalVerbsInput,
167-
pathToAdditionalVerbsInput,
181+
pathToAdditionalVerbs: pathToAdditionalVerbsInput,
168182
allowOneLiners,
169183
additionalVerbs,
170184
maxSubjectLength,
171185
maxBodyLineLength,
172186
enforceSignOff,
173187
skipBodyCheck
174-
),
188+
}),
175189
null
176190
);
177191
}

0 commit comments

Comments
 (0)