Skip to content

Commit

Permalink
validate branch and app id inputs for pipeline-deploy command (#2301)
Browse files Browse the repository at this point in the history
* validate branch and app id inputs for pipeline-deploy command

* update error

* update logic for falling back to sandbox resolver for backend id
  • Loading branch information
rtpascual authored Dec 6, 2024
1 parent fc7e4d4 commit 7c5abe2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-donkeys-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': patch
---

validate branch and app id inputs for pipeline-deploy command
5 changes: 5 additions & 0 deletions .changeset/olive-singers-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': patch
---

update logic for falling back to sandbox resolver for backend id
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,30 @@ void it('does not use sandbox id if the default identifier resolver fails and th
});
assert.deepEqual(resolvedId, undefined);
});

// stack, appId and branch can be empty string if option is added to command but no value is present (eg. 'ampx generate outputs --stack')
// this shows intent for deployed backend id so we should not fallback to sandbox id
void it('does not use sandbox id if the default identifier resolver fails and stack, appId or branch are empty strings', async () => {
const appName = 'testAppName';
const namespaceResolver = {
resolve: () => Promise.resolve(appName),
};

const defaultResolver = new AppBackendIdentifierResolver(namespaceResolver);
const username = 'test-user';
const sandboxResolver = new SandboxBackendIdResolver(
namespaceResolver,
() =>
({
username,
} as never)
);
const backendIdResolver = new BackendIdentifierResolverWithFallback(
defaultResolver,
sandboxResolver
);
const resolvedId = await backendIdResolver.resolveDeployedBackendIdentifier({
stack: '',
});
assert.deepEqual(resolvedId, undefined);
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export class BackendIdentifierResolverWithFallback
resolveDeployedBackendIdentifier = async (
args: BackendIdentifierParameters
) => {
if (args.stack || args.appId || args.branch) {
if (
args.stack !== undefined ||
args.appId !== undefined ||
args.branch !== undefined
) {
return this.defaultResolver.resolveDeployedBackendIdentifier(args);
}

Expand All @@ -33,7 +37,11 @@ export class BackendIdentifierResolverWithFallback
* Resolves deployed backend id to backend id, falling back to the sandbox id if stack or appId and branch inputs are not provided
*/
resolveBackendIdentifier = async (args: BackendIdentifierParameters) => {
if (args.stack || args.appId || args.branch) {
if (
args.stack !== undefined ||
args.appId !== undefined ||
args.branch !== undefined
) {
return this.defaultResolver.resolveBackendIdentifier(args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,32 @@ void describe('deploy command', () => {
ClientConfigFormat.DART,
]);
});

void it('throws when --branch argument has no input', async () => {
await assert.rejects(
async () =>
await getCommandRunner(true).runCommand(
'pipeline-deploy --app-id abc --branch'
),
(error: TestCommandError) => {
assert.strictEqual(error.error.name, 'InvalidCommandInputError');
assert.strictEqual(error.error.message, 'Invalid --branch or --app-id');
return true;
}
);
});

void it('throws when --app-id argument has no input', async () => {
await assert.rejects(
async () =>
await getCommandRunner(true).runCommand(
'pipeline-deploy --app-id --branch testBranch'
),
(error: TestCommandError) => {
assert.strictEqual(error.error.name, 'InvalidCommandInputError');
assert.strictEqual(error.error.message, 'Invalid --branch or --app-id');
return true;
}
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ export class PipelineDeployCommand
type: 'string',
array: false,
choices: Object.values(ClientConfigFormat),
})
.check(async (argv) => {
if (argv['branch'].length === 0 || argv['app-id'].length === 0) {
throw new AmplifyUserError('InvalidCommandInputError', {
message: 'Invalid --branch or --app-id',
resolution: '--branch and --app-id must be at least 1 character',
});
}
});
};
}

0 comments on commit 7c5abe2

Please sign in to comment.