Skip to content

Commit 7c5abe2

Browse files
authored
validate branch and app id inputs for pipeline-deploy command (#2301)
* validate branch and app id inputs for pipeline-deploy command * update error * update logic for falling back to sandbox resolver for backend id
1 parent fc7e4d4 commit 7c5abe2

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

.changeset/flat-donkeys-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-cli': patch
3+
---
4+
5+
validate branch and app id inputs for pipeline-deploy command

.changeset/olive-singers-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-cli': patch
3+
---
4+
5+
update logic for falling back to sandbox resolver for backend id

packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,30 @@ void it('does not use sandbox id if the default identifier resolver fails and th
7979
});
8080
assert.deepEqual(resolvedId, undefined);
8181
});
82+
83+
// stack, appId and branch can be empty string if option is added to command but no value is present (eg. 'ampx generate outputs --stack')
84+
// this shows intent for deployed backend id so we should not fallback to sandbox id
85+
void it('does not use sandbox id if the default identifier resolver fails and stack, appId or branch are empty strings', async () => {
86+
const appName = 'testAppName';
87+
const namespaceResolver = {
88+
resolve: () => Promise.resolve(appName),
89+
};
90+
91+
const defaultResolver = new AppBackendIdentifierResolver(namespaceResolver);
92+
const username = 'test-user';
93+
const sandboxResolver = new SandboxBackendIdResolver(
94+
namespaceResolver,
95+
() =>
96+
({
97+
username,
98+
} as never)
99+
);
100+
const backendIdResolver = new BackendIdentifierResolverWithFallback(
101+
defaultResolver,
102+
sandboxResolver
103+
);
104+
const resolvedId = await backendIdResolver.resolveDeployedBackendIdentifier({
105+
stack: '',
106+
});
107+
assert.deepEqual(resolvedId, undefined);
108+
});

packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export class BackendIdentifierResolverWithFallback
2323
resolveDeployedBackendIdentifier = async (
2424
args: BackendIdentifierParameters
2525
) => {
26-
if (args.stack || args.appId || args.branch) {
26+
if (
27+
args.stack !== undefined ||
28+
args.appId !== undefined ||
29+
args.branch !== undefined
30+
) {
2731
return this.defaultResolver.resolveDeployedBackendIdentifier(args);
2832
}
2933

@@ -33,7 +37,11 @@ export class BackendIdentifierResolverWithFallback
3337
* Resolves deployed backend id to backend id, falling back to the sandbox id if stack or appId and branch inputs are not provided
3438
*/
3539
resolveBackendIdentifier = async (args: BackendIdentifierParameters) => {
36-
if (args.stack || args.appId || args.branch) {
40+
if (
41+
args.stack !== undefined ||
42+
args.appId !== undefined ||
43+
args.branch !== undefined
44+
) {
3745
return this.defaultResolver.resolveBackendIdentifier(args);
3846
}
3947

packages/cli/src/commands/pipeline-deploy/pipeline_deploy_command.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,32 @@ void describe('deploy command', () => {
238238
ClientConfigFormat.DART,
239239
]);
240240
});
241+
242+
void it('throws when --branch argument has no input', async () => {
243+
await assert.rejects(
244+
async () =>
245+
await getCommandRunner(true).runCommand(
246+
'pipeline-deploy --app-id abc --branch'
247+
),
248+
(error: TestCommandError) => {
249+
assert.strictEqual(error.error.name, 'InvalidCommandInputError');
250+
assert.strictEqual(error.error.message, 'Invalid --branch or --app-id');
251+
return true;
252+
}
253+
);
254+
});
255+
256+
void it('throws when --app-id argument has no input', async () => {
257+
await assert.rejects(
258+
async () =>
259+
await getCommandRunner(true).runCommand(
260+
'pipeline-deploy --app-id --branch testBranch'
261+
),
262+
(error: TestCommandError) => {
263+
assert.strictEqual(error.error.name, 'InvalidCommandInputError');
264+
assert.strictEqual(error.error.message, 'Invalid --branch or --app-id');
265+
return true;
266+
}
267+
);
268+
});
241269
});

packages/cli/src/commands/pipeline-deploy/pipeline_deploy_command.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ export class PipelineDeployCommand
119119
type: 'string',
120120
array: false,
121121
choices: Object.values(ClientConfigFormat),
122+
})
123+
.check(async (argv) => {
124+
if (argv['branch'].length === 0 || argv['app-id'].length === 0) {
125+
throw new AmplifyUserError('InvalidCommandInputError', {
126+
message: 'Invalid --branch or --app-id',
127+
resolution: '--branch and --app-id must be at least 1 character',
128+
});
129+
}
122130
});
123131
};
124132
}

0 commit comments

Comments
 (0)