Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add light preview #254

Merged
merged 99 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
f5ede26
use archiver to zip resources
y-lakhdar May 28, 2021
2b5fff3
updated readme
y-lakhdar May 28, 2021
c0e746f
add base classes
y-lakhdar May 28, 2021
c5cb8fc
move project file
y-lakhdar May 28, 2021
14d1c9b
update import location
y-lakhdar May 28, 2021
dbe2ffb
add UT to Project class
y-lakhdar May 28, 2021
697e538
add UT to snapshot class
y-lakhdar May 29, 2021
d409ad4
apply corrections
y-lakhdar May 29, 2021
b272c59
setup snapshot factory
y-lakhdar May 30, 2021
4c04026
snapshot class squeleton
y-lakhdar May 30, 2021
d23326c
revert readme
y-lakhdar May 30, 2021
fbced1e
lock
y-lakhdar May 30, 2021
9d9f5b3
format readme
y-lakhdar May 30, 2021
be2810c
fix conflicts
y-lakhdar May 30, 2021
0ca9f69
allow the creation of a custom authenticated client
y-lakhdar May 30, 2021
2859c6f
push to organization
y-lakhdar May 31, 2021
804a195
push a snapshot
y-lakhdar May 31, 2021
96d1da2
add TODO
y-lakhdar May 31, 2021
fe57a94
remove unecessary check
y-lakhdar May 31, 2021
9f2577a
add public keyword
y-lakhdar May 31, 2021
506d15e
add patch to get detailed report
y-lakhdar May 31, 2021
1643f91
remove any
y-lakhdar May 31, 2021
c7a1109
set static
y-lakhdar May 31, 2021
8b50867
add TODO
y-lakhdar May 31, 2021
7dfd9dd
better comment
y-lakhdar May 31, 2021
98ffda6
fix type
y-lakhdar May 31, 2021
d2f4c47
Merge branch 'master' into CDX-360
y-lakhdar Jun 1, 2021
e6d7087
Merge branch 'master' into CDX-360
y-lakhdar Jun 1, 2021
eac47f5
Merge branch 'CDX-360' of https://github.com/coveo/cli into CDX-353
y-lakhdar Jun 1, 2021
0f9d91a
clean test
y-lakhdar Jun 1, 2021
b074496
fix conflicts
y-lakhdar Jun 1, 2021
1c1e6d7
lock
y-lakhdar Jun 1, 2021
5058f8f
Update packages/cli/src/lib/project/project.ts
y-lakhdar Jun 1, 2021
37cd540
add public keyword
y-lakhdar Jun 1, 2021
b455911
reorder methods in project class
y-lakhdar Jun 1, 2021
b3052af
Merge branch 'CDX-353' of https://github.com/coveo/cli into CDX-356
y-lakhdar Jun 1, 2021
a5a180c
apply corrections on #getClient
y-lakhdar Jun 1, 2021
96f3c3c
Merge branch 'CDX-356' of https://github.com/coveo/cli into CDX-358
y-lakhdar Jun 1, 2021
7ced17a
reorder methods
y-lakhdar Jun 1, 2021
058806b
lock
y-lakhdar Jun 1, 2021
4897322
simplify snapshot interface
y-lakhdar Jun 1, 2021
af8de57
include report
y-lakhdar Jun 1, 2021
bf525c5
UT
y-lakhdar Jun 1, 2021
a42e2de
add TODO
y-lakhdar Jun 1, 2021
fd8df34
remove unsued import
y-lakhdar Jun 1, 2021
0dd1eb4
Update packages/cli/src/lib/snapshot/snapshot.ts
y-lakhdar Jun 2, 2021
8859163
apply corrections
y-lakhdar Jun 2, 2021
9c02a93
Update packages/cli/src/lib/snapshot/snapshot.spec.ts
y-lakhdar Jun 2, 2021
50fa118
apply corrections
y-lakhdar Jun 2, 2021
bc2d976
initial draft of the report class
y-lakhdar Jun 3, 2021
b648bb9
bump platform-client version
y-lakhdar Jun 3, 2021
7b8e2fe
some basic reporting
y-lakhdar Jun 3, 2021
52138b0
simplify mock
y-lakhdar Jun 4, 2021
d358760
Merge branch 'master' of https://github.com/coveo/cli into CDX-358
y-lakhdar Jun 4, 2021
11c7217
add synchronizationPlan class
y-lakhdar Jun 4, 2021
d55dc84
Merge branch 'master' of https://github.com/coveo/cli into CDX-370
y-lakhdar Jun 4, 2021
65b35ca
refactor snapshot
y-lakhdar Jun 6, 2021
69ecf51
delete synchronizationPlan class... for now
y-lakhdar Jun 6, 2021
0f8126a
light refactor
y-lakhdar Jun 6, 2021
b5a9087
update warning message
y-lakhdar Jun 6, 2021
29b1eb0
lock
y-lakhdar Jun 6, 2021
a21ff37
revert validate change
y-lakhdar Jun 6, 2021
e607aba
snapshot UT
y-lakhdar Jun 7, 2021
e3e2136
remove readonly
y-lakhdar Jun 7, 2021
e4ea437
remove dot file
y-lakhdar Jun 7, 2021
0422170
fix windows path error
y-lakhdar Jun 7, 2021
af22dc7
fix typo
y-lakhdar Jun 7, 2021
a507ca6
Update packages/cli/src/commands/org/config/preview.ts
y-lakhdar Jun 8, 2021
14c6ac9
Update packages/cli/src/commands/org/config/preview.ts
y-lakhdar Jun 8, 2021
7eee3f6
Update packages/cli/src/lib/snapshot/snapshot.ts
y-lakhdar Jun 8, 2021
42b3170
apply corrections
y-lakhdar Jun 8, 2021
d35d87f
delete patch
y-lakhdar Jun 8, 2021
abcb7b1
patch to CDX-382
y-lakhdar Jun 8, 2021
68f5f32
bump platform-client
y-lakhdar Jun 8, 2021
316f47b
update report viewer
y-lakhdar Jun 9, 2021
35b3fd3
Merge branch 'master' into CDX-370
y-lakhdar Jun 9, 2021
123188d
Merge branch 'CDX-370' of https://github.com/coveo/cli into CDX-346
y-lakhdar Jun 9, 2021
fbbba9d
better logging
y-lakhdar Jun 9, 2021
3f10de7
do not delete snapshot if in error
y-lakhdar Jun 9, 2021
34c9df0
remove console.log
y-lakhdar Jun 9, 2021
7ad88a2
print different message if no change to apply
y-lakhdar Jun 9, 2021
189c011
better logging
y-lakhdar Jun 9, 2021
0c3b7b3
update no change detected message
y-lakhdar Jun 9, 2021
ee8b4fd
add UT
y-lakhdar Jun 9, 2021
cc527f4
add UT
y-lakhdar Jun 9, 2021
886761b
comment broken test
y-lakhdar Jun 9, 2021
3b91998
Merge branch 'master' of https://github.com/coveo/cli into CDX-346
y-lakhdar Jun 9, 2021
668b7a5
better UT
y-lakhdar Jun 10, 2021
50d8d10
replace EOL with \n
y-lakhdar Jun 10, 2021
fb32c80
missing a couple awaits
y-lakhdar Jun 10, 2021
d7aa740
UT
y-lakhdar Jun 11, 2021
bf599da
fix error on windows
y-lakhdar Jun 11, 2021
12c80e0
apply corrections
y-lakhdar Jun 11, 2021
034a114
remove platform-client patch
y-lakhdar Jun 11, 2021
e5e566d
oups
y-lakhdar Jun 11, 2021
2242426
fix windows build errors
y-lakhdar Jun 11, 2021
db3aa85
simplify logging
y-lakhdar Jun 11, 2021
137656c
Merge branch 'master' into CDX-346
louis-bompart Jun 14, 2021
723ac9d
Merge branch 'master' into CDX-346
louis-bompart Jun 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"@angular/cli": "^11.1.4",
"@coveord/platform-client": "^19.14.0",
"@coveord/platform-client": "^20.2.0",
"@oclif/command": "^1",
"@oclif/config": "^1",
"@oclif/plugin-help": "^3",
Expand All @@ -16,6 +16,7 @@
"@vue/cli": "^4.5.11",
"abortcontroller-polyfill": "^1.7.1",
"archiver": "^5.3.0",
"chalk": "^4.1.1",
"cli-ux": "^5.5.1",
"coveo.analytics": "^2.18.4",
"create-react-app": "^4.0.3",
Expand All @@ -34,6 +35,7 @@
"@coveo/cra-template": "^1.3.0",
"@coveo/vue-cli-plugin-typescript": "^1.3.0",
"@oclif/dev-cli": "^1.26.0",
"@oclif/errors": "^1.3.4",
"@oclif/test": "^1",
"@types/archiver": "^5.1.0",
"@types/cli-progress": "^3.9.1",
Expand Down
220 changes: 220 additions & 0 deletions packages/cli/src/commands/org/config/preview.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
jest.mock('../../../lib/config/config');
jest.mock('../../../hooks/analytics/analytics');
jest.mock('../../../hooks/prerun/prerun');
jest.mock('../../../lib/platform/authenticatedClient');
jest.mock('../../../lib/snapshot/snapshot');
jest.mock('../../../lib/snapshot/snapshotFactory');
jest.mock('../../../lib/project/project');
jest.mock('@oclif/errors');

import {mocked} from 'ts-jest/utils';
import {test} from '@oclif/test';
import {Project} from '../../../lib/project/project';
import {join, normalize} from 'path';
import {cwd} from 'process';
import {Config} from '../../../lib/config/config';
import {SnapshotFactory} from '../../../lib/snapshot/snapshotFactory';
import {Snapshot} from '../../../lib/snapshot/snapshot';
import {warn, error} from '@oclif/errors';

const mockedSnapshotFactory = mocked(SnapshotFactory, true);
const mockedConfig = mocked(Config);
const mockedProject = mocked(Project);
const mockedWarn = mocked(warn);
const mockedError = mocked(error);
const mockedConfigGet = jest.fn();
const mockedDeleteTemporaryZipFile = jest.fn();
const mockedDeleteSnapshot = jest.fn();
const mockedSaveDetailedReport = jest.fn();
const mockedRequiresSynchronization = jest.fn();
const mockedPreviewSnapshot = jest.fn();
const mockedLastReport = jest.fn();

const mockProject = () => {
mockedProject.mockImplementation(
() =>
({
compressResources: () =>
Promise.resolve(normalize(join('path', 'to', 'resources.zip'))),
deleteTemporaryZipFile: mockedDeleteTemporaryZipFile,
} as unknown as Project)
);
};

const mockConfig = () => {
mockedConfigGet.mockReturnValue(
Promise.resolve({
region: 'us-east-1',
organization: 'foo',
environment: 'prod',
})
);

// TODO: use prototype
mockedConfig.mockImplementation(
() =>
({
get: mockedConfigGet,
} as unknown as Config)
);
};

const mockSnapshotFactory = async (validResponse: unknown) => {
mockedSnapshotFactory.createFromZip.mockReturnValue(
Promise.resolve({
validate: () => Promise.resolve(validResponse),
preview: mockedPreviewSnapshot,
delete: mockedDeleteSnapshot,
saveDetailedReport: mockedSaveDetailedReport,
requiresSynchronization: mockedRequiresSynchronization,
latestReport: mockedLastReport,
id: 'banana-snapshot',
targetId: 'potato-org',
} as unknown as Snapshot)
);
};

const mockSnapshotFactoryReturningValidSnapshot = async () => {
await mockSnapshotFactory({isValid: true, report: {}});
};

const mockSnapshotFactoryReturningInvalidSnapshot = async () => {
await mockSnapshotFactory({isValid: false, report: {}});
};

describe('org:config:preview', () => {
beforeAll(() => {
mockConfig();
mockProject();
});

describe('when the report contains no resources in error', () => {
beforeAll(async () => {
await mockSnapshotFactoryReturningValidSnapshot();
});

afterAll(() => {
mockedSnapshotFactory.mockReset();
});

test.command(['org:config:preview']).it('should use cwd as project', () => {
expect(mockedProject).toHaveBeenCalledWith(cwd());
});

test
.command(['org:config:preview', '-p', 'path/to/project'])
.it('should use specifeid path for project', () => {
expect(mockedProject).toHaveBeenCalledWith(
normalize(join('path', 'to', 'project'))
);
});

test
.command(['org:config:preview'])
.it('should work with default connected org', () => {
expect(mockedSnapshotFactory.createFromZip).toHaveBeenCalledWith(
normalize(join('path', 'to', 'resources.zip')),
'foo'
);
});

test
.command(['org:config:preview', '-t', 'myorg'])
.it('should work with specified target org', () => {
expect(mockedSnapshotFactory.createFromZip).toHaveBeenCalledWith(
normalize(join('path', 'to', 'resources.zip')),
'myorg'
);
});

test
.command(['org:config:preview'])
.it('should preview the snapshot', () => {
expect(mockedPreviewSnapshot).toHaveBeenCalledTimes(1);
});

test
.command(['org:config:preview'])
.it('should delete the compressed folder', () => {
expect(mockedDeleteTemporaryZipFile).toHaveBeenCalledTimes(1);
});

test
.command(['org:config:preview'])
.it('should delete the snapshot', () => {
expect(mockedDeleteSnapshot).toHaveBeenCalledTimes(1);
});
});

describe('when the report contains resources in error', () => {
beforeAll(async () => {
await mockSnapshotFactoryReturningInvalidSnapshot();
});

beforeEach(() => {
mockedRequiresSynchronization.mockReturnValueOnce(false);
mockedSaveDetailedReport.mockReturnValueOnce(
normalize(join('saved', 'snapshot'))
);
});

afterAll(() => {
mockedSnapshotFactory.mockReset();
});

test
.command(['org:config:preview'])
.it('should throw an error for invalid snapshots', () => {
expect(mockedError).toHaveBeenCalledWith(
expect.stringContaining('Invalid snapshot'),
{}
);
});

test
.command(['org:config:preview'])
.it('should print an URL to the snapshot page', () => {
expect(mockedError).toHaveBeenCalledWith(
expect.stringContaining(
'https://platform.cloud.coveo.com/admin/#potato-org/organization/resource-snapshots/banana-snapshot'
),
{}
);
});
});

describe('when the snapshot is not in sync with the target org', () => {
beforeAll(async () => {
await mockSnapshotFactoryReturningInvalidSnapshot();
});

beforeEach(() => {
mockedRequiresSynchronization.mockReturnValueOnce(true);
mockedSaveDetailedReport.mockReturnValueOnce(join('saved', 'snapshot'));
});

afterAll(() => {
mockedSnapshotFactory.mockReset();
});

test
.command(['org:config:preview'])
.it('should have detected some conflicts', () => {
expect(mockedWarn).toHaveBeenCalledWith(
expect.stringContaining(
'Some conflicts were detected while comparing changes between the snapshot and the target organization'
)
);
});

test
.command(['org:config:preview'])
.it('should print an url to the synchronization page', () => {
expect(mockedWarn).toHaveBeenCalledWith(
expect.stringContaining(
'https://platform.cloud.coveo.com/admin/#potato-org/organization/resource-snapshots/banana-snapshot/synchronization'
)
);
});
});
});
27 changes: 16 additions & 11 deletions packages/cli/src/commands/org/config/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {Project} from '../../../lib/project/project';
import {SnapshotFactory} from '../../../lib/snapshot/snapshotFactory';
import {platformUrl} from '../../../lib/platform/environment';
import {Snapshot} from '../../../lib/snapshot/snapshot';
import {red, green} from 'chalk';
import {normalize} from 'path';

export interface CustomFile extends ReadStream {
type?: string;
Expand Down Expand Up @@ -42,7 +44,7 @@ export default class Preview extends Command {
@Preconditions(IsAuthenticated())
public async run() {
const {flags} = this.parse(Preview);
const project = new Project(flags.projectPath);
const project = new Project(normalize(flags.projectPath));
const pathToZip = await project.compressResources();
const targetOrg = await this.getTargetOrg();

Expand All @@ -55,15 +57,18 @@ export default class Preview extends Command {
const {isValid} = await snapshot.validate();

if (!isValid) {
this.handleInvalidSnapshot(snapshot);
} else {
await snapshot.preview();
await this.handleInvalidSnapshot(snapshot);
}

cli.action.stop(isValid ? green('✔') : red.bold('!'));

await snapshot.preview();

if (isValid) {
await snapshot.delete();
}

project.deleteTemporaryZipFile();

cli.action.stop();
}

public async getTargetOrg() {
Expand All @@ -82,20 +87,20 @@ export default class Preview extends Command {
const report = snapshot.latestReport;

if (snapshot.requiresSynchronization()) {
cli.action.start('Synchronization');

const synchronizationPlanUrl = await this.getSynchronizationPage(
snapshot
);
this.warn(
dedent`Some conflicts were detected while comparing changes between the snapshot and the target organization.
dedent`
Some conflicts were detected while comparing changes between the snapshot and the target organization.
Click on the URL below to synchronize your snapshot with your organization before running the command again.
${synchronizationPlanUrl}`
${synchronizationPlanUrl}
`
);
return;
}

const snapshotUrl = this.getSnapshotPage(snapshot);
const snapshotUrl = await this.getSnapshotPage(snapshot);

this.error(
dedent`Invalid snapshot - ${report.resultCode}.
Expand Down
Loading