Skip to content

Commit f307f6d

Browse files
authored
fix(commands): trim input parameters [FUS-358] (#976)
Trim input parameter to ensure valid values
1 parent 825b32b commit f307f6d

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

src/command-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function trimInput(str: string): Promise<string> {
2+
return Promise.resolve(str.trim())
3+
}

src/commands/apply/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from '../../analytics'
2424
import { config } from '../../config'
2525
import cleanStack from 'clean-stack'
26+
import { trimInput } from '../../command-utils'
2627

2728
initSentry()
2829

@@ -46,21 +47,32 @@ export default class Apply extends Command {
4647
]
4748

4849
static flags = {
49-
space: Flags.string({ default: undefined, description: 'Space id', required: true }),
50-
environment: Flags.string({ default: undefined, description: 'Target environment id', required: true }),
50+
space: Flags.string({ default: undefined, description: 'Space id', required: true, parse: trimInput }),
51+
environment: Flags.string({
52+
default: undefined,
53+
description: 'Target environment id',
54+
required: true,
55+
parse: trimInput,
56+
}),
5157
file: Flags.string({ description: 'File path to changeset file', required: true, default: undefined }),
5258
'cma-token': Flags.string({
5359
default: undefined,
5460
description: 'CMA token, defaults to env: $CMA_TOKEN',
5561
required: true,
5662
env: 'CMA_TOKEN',
63+
parse: trimInput,
5764
}),
5865
yes: Flags.boolean({
5966
description: 'Skips any confirmation before applying the changeset',
6067
required: false,
6168
default: false,
6269
}),
63-
host: Flags.string({ default: 'api.contentful.com', description: 'Contentful API host', required: false }),
70+
host: Flags.string({
71+
default: 'api.contentful.com',
72+
description: 'Contentful API host',
73+
required: false,
74+
parse: trimInput,
75+
}),
6476
}
6577

6678
private async writeFileLog() {
@@ -90,7 +102,7 @@ export default class Apply extends Command {
90102

91103
if (!['Y', 'y'].includes(answer)) {
92104
this.terminatedByUser = true
93-
this.log(chalk.bold.yellow('⚠️ Merge aborted'))
105+
this.log(chalk.bold.yellow('⚠️ Merge aborted'))
94106
return
95107
}
96108
}

src/commands/create/index.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { renderErrorOutputForCreate } from '../../engine/utils/render-error-outp
2323
import { detectErrorLevel, OutputFormatter, renderFilePaths } from '../../engine/utils'
2424
import { config } from '../../config'
2525
import cleanStack from 'clean-stack'
26+
import { trimInput } from '../../command-utils'
2627

2728
initSentry()
2829

@@ -46,18 +47,34 @@ export default class Create extends Command {
4647
]
4748

4849
static flags = {
49-
space: Flags.string({ default: undefined, description: 'Space id', required: true }),
50-
source: Flags.string({ default: undefined, description: 'Source environment id', required: true }),
51-
target: Flags.string({ default: undefined, description: 'Target environment id', required: true }),
50+
space: Flags.string({ default: undefined, description: 'Space id', required: true, parse: trimInput }),
51+
source: Flags.string({
52+
default: undefined,
53+
description: 'Source environment id',
54+
required: true,
55+
parse: trimInput,
56+
}),
57+
target: Flags.string({
58+
default: undefined,
59+
description: 'Target environment id',
60+
required: true,
61+
parse: trimInput,
62+
}),
5263
'cda-token': Flags.string({
5364
default: undefined,
5465
description: 'CDA token, defaults to env: $CDA_TOKEN',
5566
required: true,
5667
env: 'CDA_TOKEN',
68+
parse: trimInput,
5769
}),
5870
'request-batch-size': Flags.integer({ description: 'Limit for every single request', default: 1000 }),
5971
'output-file': Flags.string({ default: undefined, description: 'File path to changeset file', required: false }),
60-
host: Flags.string({ default: 'api.contentful.com', description: 'Contentful API host', required: false }),
72+
host: Flags.string({
73+
default: 'api.contentful.com',
74+
description: 'Contentful API host',
75+
required: false,
76+
parse: trimInput,
77+
}),
6178
'query-entries': Flags.string({
6279
default: undefined,
6380
description: 'Query parameters for entries based on CDA',

test/unit/commands/apply/index.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ApplyChangesetContext } from '../../../../src/engine/apply-changeset/ty
1313
import { createApplyChangesetContext } from '../../fixtures/apply-changeset-context-fixture'
1414
import * as client from '../../../../src/engine/client'
1515
import { createMockClient } from '../../fixtures/create-mock-client'
16+
import * as analytics from '../../../../src/analytics'
1617

1718
const cmd = new ApplyCommand(
1819
[],
@@ -125,7 +126,7 @@ describe('Apply Command', () => {
125126

126127
fancy
127128
.stdout()
128-
.stdin('Y\n', 10) // small delay to make sure the stdin is read
129+
.stdin('Y\n', 10)
129130
.do(() =>
130131
new ApplyCommand(
131132
[
@@ -146,4 +147,30 @@ describe('Apply Command', () => {
146147
process.stdin.once('data', (data) => expect(data.toString()).to.equal('Y\n'))
147148
expect(output.stdout).not.to.include('[Skipping confirmation because --yes flag was provided]')
148149
})
150+
151+
fancy
152+
.stdout()
153+
.stdin('Y\n', 10)
154+
.stub(analytics, 'trackApplyCommandStarted', (stub) => stub.returns({})) // small delay to make sure the stdin is read
155+
.do(() =>
156+
new ApplyCommand(
157+
[
158+
'--space',
159+
' some-space-id',
160+
'--environment',
161+
'target-env-id ',
162+
'--cma-token',
163+
'some-cma-token',
164+
'--file',
165+
'some-file-path',
166+
],
167+
{} as unknown as Config, // Runtime config, but not required for tests.
168+
).run(),
169+
)
170+
.it('should trim input params', () => {
171+
expect((analytics.trackApplyCommandStarted as sinon.SinonStub).getCall(0).args[0]).to.contain({
172+
space_key: 'some-space-id',
173+
target_environment_key: 'target-env-id',
174+
})
175+
})
149176
})

0 commit comments

Comments
 (0)