Skip to content

Commit 1448350

Browse files
committed
fix: make OAuth credentials conditional in code commands
OAuth is now only required for code deploy/watch when actually needed: - No code version specified (auto-discovery via OCAPI) - --reload flag is set (reload via OCAPI) When using basic auth with an explicit --code-version, OAuth is not required. Error messages now explain why OAuth is needed and link to configuration docs. Closes #35
1 parent eb22a84 commit 1448350

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

packages/b2c-cli/src/commands/code/deploy.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,33 @@ export default class CodeDeploy extends CartridgeCommand<typeof CodeDeploy> {
5656

5757
async run(): Promise<DeployResult> {
5858
this.requireWebDavCredentials();
59-
this.requireOAuthCredentials();
6059

6160
const hostname = this.resolvedConfig.values.hostname!;
6261
let version = this.resolvedConfig.values.codeVersion;
6362

63+
// OAuth is only required if:
64+
// 1. No code version specified (need to auto-discover via OCAPI)
65+
// 2. --reload flag is set (need to call OCAPI to reload)
66+
const needsOAuth = !version || this.flags.reload;
67+
if (needsOAuth && !this.hasOAuthCredentials()) {
68+
const reason = version
69+
? t(
70+
'commands.code.deploy.oauthRequiredForReload',
71+
'The --reload flag requires OAuth credentials to reload the code version via OCAPI.',
72+
)
73+
: t(
74+
'commands.code.deploy.oauthRequiredForDiscovery',
75+
'No code version specified. OAuth credentials are required to auto-discover the active code version.',
76+
);
77+
this.error(
78+
t(
79+
'commands.code.deploy.oauthRequired',
80+
'{{reason}}\n\nProvide --code-version to use basic auth only, or configure OAuth credentials.\nSee: https://salesforcecommercecloud.github.io/b2c-developer-tooling/guide/configuration.html',
81+
{reason},
82+
),
83+
);
84+
}
85+
6486
// If no code version specified, discover the active one
6587
if (!version) {
6688
this.warn(

packages/b2c-cli/src/commands/code/watch.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,20 @@ export default class CodeWatch extends CartridgeCommand<typeof CodeWatch> {
3333

3434
async run(): Promise<void> {
3535
this.requireWebDavCredentials();
36-
this.requireOAuthCredentials();
3736

3837
const hostname = this.resolvedConfig.values.hostname!;
3938
const version = this.resolvedConfig.values.codeVersion;
4039

40+
// OAuth is only required if no code version specified (need to auto-discover via OCAPI)
41+
if (!version && !this.hasOAuthCredentials()) {
42+
this.error(
43+
t(
44+
'commands.code.watch.oauthRequired',
45+
'No code version specified. OAuth credentials are required to auto-discover the active code version.\n\nProvide --code-version to use basic auth only, or configure OAuth credentials.\nSee: https://salesforcecommercecloud.github.io/b2c-developer-tooling/guide/configuration.html',
46+
),
47+
);
48+
}
49+
4150
this.log(t('commands.code.watch.starting', 'Starting watcher for {{path}}', {path: this.cartridgePath}));
4251
this.log(t('commands.code.watch.target', 'Target: {{hostname}}', {hostname}));
4352
if (version) {

packages/b2c-cli/test/commands/code/deploy.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('code deploy', () => {
2424
function stubCommon(command: any) {
2525
const instance = {config: {hostname: 'example.com', codeVersion: 'v1'}};
2626
sinon.stub(command, 'requireWebDavCredentials').returns(void 0);
27-
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
27+
sinon.stub(command, 'hasOAuthCredentials').returns(true);
2828
sinon.stub(command, 'log').returns(void 0);
2929
sinon.stub(command, 'warn').returns(void 0);
3030
sinon.stub(command, 'resolvedConfig').get(() => ({values: {hostname: 'example.com', codeVersion: 'v1'}}));
@@ -117,7 +117,7 @@ describe('code deploy', () => {
117117
const command: any = await createCommand({}, {cartridgePath: '.'});
118118

119119
sinon.stub(command, 'requireWebDavCredentials').returns(void 0);
120-
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
120+
sinon.stub(command, 'hasOAuthCredentials').returns(true);
121121
sinon.stub(command, 'log').returns(void 0);
122122
sinon.stub(command, 'warn').returns(void 0);
123123

0 commit comments

Comments
 (0)