Skip to content

Commit 220a669

Browse files
Chhavi-Mandowaradhruvparekh12
authored andcommitted
fix: fix behaviour of redeploy-latest and redeploy-last-upload flag for existing project to not show prompt
1 parent dba5275 commit 220a669

File tree

5 files changed

+132
-57
lines changed

5 files changed

+132
-57
lines changed

src/adapters/file-upload.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,36 @@ export default class FileUpload extends BaseClass {
2626
*/
2727
async run(): Promise<void> {
2828
if (this.config.isExistingProject) {
29-
await this.initApolloClient();
30-
const uploadLastFile =
31-
this.config['redeploy-last-upload'] ||
32-
(await cliux.inquire({
33-
type: 'confirm',
34-
default: false,
35-
name: 'uploadLastFile',
36-
message: 'Redeploy with last file upload?',
37-
}));
38-
if (!uploadLastFile) {
39-
await this.createSignedUploadUrl();
40-
const { zipName, zipPath } = await this.archive();
41-
await this.uploadFile(zipName, zipPath);
42-
}
43-
44-
const { uploadUid } = this.signedUploadUrlData || {
45-
uploadUid: undefined,
46-
};
47-
await this.createNewDeployment(true, uploadUid);
29+
await this.handleExistingProject();
4830
} else {
49-
await this.prepareForNewProjectCreation();
50-
await this.createNewProject();
31+
await this.handleNewProject();
5132
}
52-
33+
5334
this.prepareLaunchConfig();
5435
await this.showLogs();
5536
this.showDeploymentUrl();
5637
this.showSuggestion();
5738
}
39+
40+
private async handleExistingProject(): Promise<void> {
41+
await this.initApolloClient();
42+
43+
let redeployLatest = this.config['redeploy-latest'];
44+
45+
if (redeployLatest) {
46+
await this.createSignedUploadUrl();
47+
const { zipName, zipPath } = await this.archive();
48+
await this.uploadFile(zipName, zipPath);
49+
}
50+
51+
const { uploadUid } = this.signedUploadUrlData || { uploadUid: undefined };
52+
await this.createNewDeployment(true, uploadUid);
53+
}
54+
55+
private async handleNewProject(): Promise<void> {
56+
await this.prepareForNewProjectCreation();
57+
await this.createNewProject();
58+
}
5859

5960
/**
6061
* @method createNewProject - Create new launch project

src/adapters/github.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,10 @@ export default class GitHub extends BaseClass {
2121
* @memberof GitHub
2222
*/
2323
async run(): Promise<void> {
24-
// NOTE New project creation Flow
2524
if (this.config.isExistingProject) {
26-
await this.initApolloClient();
27-
await this.createNewDeployment();
25+
await this.handleExistingProject();
2826
} else {
29-
// NOTE Existing project flow
30-
// NOTE Step 1: Check is Github connected
31-
if (await this.checkGitHubConnected()) {
32-
// NOTE Step 2: check is the git remote available in the user's repo list
33-
if (await this.checkGitRemoteAvailableAndValid()) {
34-
if (await this.checkUserGitHubAccess()) {
35-
// NOTE Step 3: check is the user has proper git access
36-
await this.prepareForNewProjectCreation();
37-
}
38-
}
39-
}
40-
41-
await this.createNewProject();
27+
await this.handleNewProject();
4228
}
4329

4430
this.prepareLaunchConfig();
@@ -47,6 +33,34 @@ export default class GitHub extends BaseClass {
4733
this.showSuggestion();
4834
}
4935

36+
private async handleExistingProject(): Promise<void> {
37+
await this.initApolloClient();
38+
const redeployLastUpload = this.config['redeploy-last-upload'];
39+
40+
if (redeployLastUpload) {
41+
this.log('redeploy-last-upload flag is not supported for Github Project.', 'error');
42+
this.exit(1);
43+
return;
44+
}
45+
46+
await this.initApolloClient();
47+
await this.createNewDeployment();
48+
}
49+
50+
private async handleNewProject(): Promise<void> {
51+
// NOTE Step 1: Check is Github connected
52+
if (await this.checkGitHubConnected()) {
53+
// NOTE Step 2: check is the git remote available in the user's repo list
54+
if (await this.checkGitRemoteAvailableAndValid()) {
55+
if (await this.checkUserGitHubAccess()) {
56+
// NOTE Step 3: check is the user has proper git access
57+
await this.prepareForNewProjectCreation();
58+
}
59+
}
60+
}
61+
await this.createNewProject();
62+
}
63+
5064
/**
5165
* @method createNewProject - Create new launch project
5266
*

src/adapters/pre-check.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ export default class PreCheck extends BaseClass {
4747
this.log('Existing launch project identified', 'info');
4848

4949
await this.displayPreDeploymentDetails();
50-
const deployLatestCode =
51-
this.config['redeploy-latest'] ||
52-
(await ux.inquire({
53-
type: 'confirm',
54-
name: 'deployLatestSource',
55-
message: 'Redeploy latest commit/code?',
56-
}));
57-
if (!deployLatestCode) {
58-
this.exit(1);
59-
}
6050
}
6151
}
6252
}

test/unit/adapters/file-upload.test.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { FileUpload, BaseClass } from '../../../src/adapters';
77
import { BaseCommand } from '../../../src/base-command';
88

99
describe('File Upload', () => {
10-
let inquireStub, prepareApiClientsStub, prepareConfigStub, getConfigStub;
10+
let inquireStub, exitStub, prepareApiClientsStub, prepareConfigStub, getConfigStub;
1111
let adapterConstructorInputs;
1212

1313
beforeEach(() => {
1414
inquireStub = stub(cliux, 'inquire');
15+
exitStub = stub(BaseCommand.prototype, 'exit');
1516
prepareConfigStub = stub(BaseCommand.prototype, 'prepareConfig').resolves();
1617
prepareApiClientsStub = stub(BaseCommand.prototype, 'prepareApiClients').resolves();
1718
getConfigStub = stub(BaseCommand.prototype, 'getConfig').resolves();
@@ -25,6 +26,7 @@ describe('File Upload', () => {
2526

2627
afterEach(() => {
2728
inquireStub.restore();
29+
exitStub.restore();
2830
prepareConfigStub.restore();
2931
getConfigStub.restore();
3032
prepareApiClientsStub.restore();
@@ -43,9 +45,6 @@ describe('File Upload', () => {
4345
showDeploymentUrlStub,
4446
showSuggestionStub;
4547

46-
let adapterConstructorOptions = {
47-
config: { isExistingProject: true, currentConfig: { uid: '123244', organizationUid: 'bltxxxxxxxx' } },
48-
};
4948
beforeEach(() => {
5049
initApolloClientStub = stub(BaseClass.prototype, 'initApolloClient').resolves();
5150
createSignedUploadUrlStub = stub(FileUpload.prototype, 'createSignedUploadUrl').resolves();
@@ -75,8 +74,40 @@ describe('File Upload', () => {
7574
showSuggestionStub.restore();
7675
});
7776

78-
it('should run github flow', async () => {
79-
new FileUpload(adapterConstructorOptions).run();
77+
describe('Redeploy existing project', () => {
78+
79+
it('should run file upload flow for existing project where flag passed is redeploy-latest', async () => {
80+
let adapterConstructorOptions = {
81+
config: {
82+
isExistingProject: true,
83+
currentConfig: { uid: '123244', organizationUid: 'bltxxxxxxxx', },
84+
'redeploy-latest': true
85+
},
86+
};
87+
new FileUpload(adapterConstructorOptions).run();
88+
});
89+
90+
it('should run file upload flow for existing project where flag passed is redeploy-last-upload', async () => {
91+
let adapterConstructorOptions = {
92+
config: {
93+
isExistingProject: true,
94+
currentConfig: { uid: '123244', organizationUid: 'bltxxxxxxxx', },
95+
'redeploy-last-upload': true
96+
},
97+
};
98+
new FileUpload(adapterConstructorOptions).run();
99+
});
100+
});
101+
102+
describe('Deploy new project', () => {
103+
let adapterConstructorOptions = {
104+
config: {
105+
isExistingProject: false
106+
},
107+
};
108+
it('should run file upload flow for new project', async () => {
109+
new FileUpload(adapterConstructorOptions).run();
110+
});
80111
});
81112
});
82113

test/unit/adapters/github.test.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { cliux } from '@contentstack/cli-utilities';
55
import { githubAdapterMockData } from '../mock/index';
66
import { GitHub, BaseClass } from '../../../src/adapters';
77
import { BaseCommand } from '../../../src/base-command';
8+
import { exit } from 'process';
9+
import fs from 'fs';
810

911
describe('GitHub', () => {
1012
let inquireStub, prepareApiClientsStub, prepareConfigStub, getConfigStub;
@@ -41,6 +43,7 @@ describe('GitHub', () => {
4143
prepareLaunchConfigStub,
4244
showLogsStub,
4345
showDeploymentUrlStub,
46+
exitStub,
4447
showSuggestionStub;
4548

4649
beforeEach(() => {
@@ -53,11 +56,11 @@ describe('GitHub', () => {
5356
checkUserGitHubAccessStub = stub(GitHub.prototype, 'checkUserGitHubAccess').resolves(true);
5457
prepareForNewProjectCreationStub = stub(GitHub.prototype, 'prepareForNewProjectCreation').resolves();
5558
createNewProjectStub = stub(GitHub.prototype, 'createNewProject').resolves();
56-
5759
prepareLaunchConfigStub = stub(BaseClass.prototype, 'prepareLaunchConfig').resolves();
5860
showLogsStub = stub(BaseClass.prototype, 'showLogs').resolves();
5961
showDeploymentUrlStub = stub(BaseClass.prototype, 'showDeploymentUrl').resolves();
6062
showSuggestionStub = stub(BaseClass.prototype, 'showSuggestion').resolves();
63+
exitStub = stub(BaseCommand.prototype, 'exit').resolves();
6164
});
6265

6366
afterEach(() => {
@@ -72,10 +75,46 @@ describe('GitHub', () => {
7275
showLogsStub.restore();
7376
showDeploymentUrlStub.restore();
7477
showSuggestionStub.restore();
78+
exitStub.restore();
79+
});
80+
81+
describe('Redeploy existing project', () => {
82+
it('should abort github flow for existing project and flag redeploy-last-upload is passed', async () => {
83+
const adapterConstructorOptions = {
84+
config: {
85+
isExistingProject: true,
86+
'redeploy-last-upload': true
87+
},
88+
};
89+
const exitStub = stub(process, 'exit');
90+
const githubInstance = new GitHub(adapterConstructorOptions);
91+
92+
await githubInstance.handleExistingProject();
93+
94+
expect(exitStub.calledOnceWithExactly(1)).to.be.true;
95+
});
96+
97+
it('should run github flow for existing project and flag redeploy-latest is passed ', async () => {
98+
let adapterConstructorOptions = {
99+
config: {
100+
isExistingProject: true,
101+
'redeploy-latest': true
102+
},
103+
};
104+
105+
new GitHub(adapterConstructorOptions).run()
106+
});
75107
});
76108

77-
it('should run github flow', async () => {
78-
new GitHub(adapterConstructorInputs).run();
109+
describe('Deploy new project', () => {
110+
let adapterConstructorOptions = {
111+
config: {
112+
isExistingProject: false
113+
},
114+
};
115+
it('should run file upload flow for new project', async () => {
116+
new GitHub(adapterConstructorOptions).run();
117+
});
79118
});
80119
});
81120

0 commit comments

Comments
 (0)