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 push command #260

Merged
merged 140 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 136 commits
Commits
Show all changes
140 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
e0d8742
fix race condition
y-lakhdar Jun 10, 2021
50d8d10
replace EOL with \n
y-lakhdar Jun 10, 2021
33f8f70
add snapshot mock
y-lakhdar Jun 10, 2021
2aa28fd
apply corrections
y-lakhdar Jun 10, 2021
fb32c80
missing a couple awaits
y-lakhdar Jun 10, 2021
d7aa740
UT
y-lakhdar Jun 11, 2021
43ad9cf
add delete flag
y-lakhdar Jun 11, 2021
bf599da
fix error on windows
y-lakhdar Jun 11, 2021
12c80e0
apply corrections
y-lakhdar Jun 11, 2021
6ca602f
only show operations that were specified
y-lakhdar Jun 11, 2021
df5e082
Merge branch 'CDX-346' of https://github.com/coveo/cli into CDX-361
y-lakhdar Jun 11, 2021
fdbe5fd
do not hardcode indentation on rows
y-lakhdar Jun 11, 2021
1c501be
remove unchanged since out of scope
y-lakhdar Jun 11, 2021
52d9a56
add unchanged resources
y-lakhdar Jun 11, 2021
7d51297
fix padding issue
y-lakhdar Jun 11, 2021
c21d7d0
Merge branch 'CDX-361' of https://github.com/coveo/cli into CDX-389
y-lakhdar Jun 11, 2021
034a114
remove platform-client patch
y-lakhdar Jun 11, 2021
f952f06
add UT
y-lakhdar Jun 11, 2021
d87427a
Merge branch 'CDX-361' of https://github.com/coveo/cli into CDX-389
y-lakhdar Jun 11, 2021
e5e566d
oups
y-lakhdar Jun 11, 2021
2242426
fix windows build errors
y-lakhdar Jun 11, 2021
ddca3a5
Merge branch 'CDX-346' of https://github.com/coveo/cli into CDX-361
y-lakhdar Jun 11, 2021
c98bb8a
Merge branch 'CDX-361' of https://github.com/coveo/cli into CDX-389
y-lakhdar Jun 11, 2021
6f32ddf
Merge branch 'CDX-346' of https://github.com/coveo/cli into CDX-387
y-lakhdar Jun 11, 2021
3a8243a
add push command
y-lakhdar Jun 12, 2021
8a106f6
add UT to snapshot class
y-lakhdar Jun 14, 2021
1718a78
yes/no to y/n
y-lakhdar Jun 14, 2021
f3cc8db
add TODO comment
y-lakhdar Jun 14, 2021
0f25ce9
add custom base command
y-lakhdar Jun 15, 2021
a1ba5f1
remove code duplication
y-lakhdar Jun 15, 2021
b5086cc
remove error from comment
y-lakhdar Jun 15, 2021
8bfe8a7
Merge branch 'master' of https://github.com/coveo/cli into CDX-387
y-lakhdar Jun 15, 2021
a295e18
Merge branch 'CDX-387' of https://github.com/coveo/cli into CDX-361
y-lakhdar Jun 15, 2021
eb0eec5
Merge branch 'master' of https://github.com/coveo/cli into CDX-361
y-lakhdar Jun 15, 2021
188a121
Merge branch 'master' into CDX-361
louis-bompart Jun 15, 2021
2fa3da3
reportViewer refactor
y-lakhdar Jun 16, 2021
8086f21
refactor: reportViewer refactor (#266)
y-lakhdar Jun 16, 2021
8cfe857
Merge branch 'CDX-361' of https://github.com/coveo/cli into CDX-361
y-lakhdar Jun 16, 2021
389b1f8
remove old file
y-lakhdar Jun 16, 2021
04208e8
Merge branch 'CDX-361' of https://github.com/coveo/cli into CDX-348
y-lakhdar Jun 16, 2021
669eda1
remove old file
y-lakhdar Jun 16, 2021
f4e8e0b
Merge branch 'CDX-361' of https://github.com/coveo/cli into CDX-348
y-lakhdar Jun 16, 2021
d6564cd
fix merge conflicts
y-lakhdar Jun 17, 2021
788de6e
UT
y-lakhdar Jun 17, 2021
9a70556
forgot to preview before asking to apply
y-lakhdar Jun 17, 2021
a520737
add missing await
y-lakhdar Jun 17, 2021
034a153
add missing delete flag
y-lakhdar Jun 17, 2021
598ff70
Merge branch 'master' of https://github.com/coveo/cli into CDX-348
y-lakhdar Jun 17, 2021
e99f2de
create url file
y-lakhdar Jun 19, 2021
3819976
Merge branch 'master' into CDX-348
louis-bompart Jun 21, 2021
2224ca1
apply corrections
y-lakhdar Jun 21, 2021
306fde5
Merge branch 'CDX-348' of https://github.com/coveo/cli into CDX-348
y-lakhdar Jun 21, 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
148 changes: 148 additions & 0 deletions packages/cli/src/commands/org/config/orgConfigBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import {Command, flags} from '@oclif/command';
import {cli} from 'cli-ux';
import {ReadStream} from 'fs';
import {dedent} from 'ts-dedent';
import {cwd} from 'process';
import {Config} from '../../../lib/config/config';
import {Project} from '../../../lib/project/project';
import {SnapshotFactory} from '../../../lib/snapshot/snapshotFactory';
import {
platformSnapshotSynchronizationUrl,
platformSnapshotUrl,
} 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;
}

export default abstract class SnapshotBase extends Command {
public static description = 'Create and validate a snapshot';

public static flags = {
target: flags.string({
char: 't',
description:
'The unique identifier of the organization where to send the changes. If not specified, the organization you are connected to will be used.',
helpValue: 'destinationorganizationg7dg3gd',
required: false,
}),
projectPath: flags.string({
char: 'p',
description: 'The path to your Coveo project.',
helpValue: '/Users/Me/my-project',
default: cwd(),
required: false,
}),
deleteMissingResources: flags.boolean({
char: 'd',
description: 'Whether or not to show resources to delete',
default: false,
required: false,
}),
};

public static hidden = true;

protected async dryRun() {
const project = new Project(normalize(this.flags.projectPath));

cli.action.start('Creating snapshot');
const snapshot = await this.createSnapshotFromProject(project);

cli.action.start('Validating snapshot');
const isValid = await this.validateSnapshot(snapshot);

cli.action.stop(isValid ? green('✔') : red.bold('!'));
return {isValid, snapshot, project};
}

protected async createSnapshotFromProject(
project: Project
): Promise<Snapshot> {
const pathToZip = await project.compressResources();
const targetOrg = await this.getTargetOrg();

return await SnapshotFactory.createFromZip(pathToZip, targetOrg);
}

protected async validateSnapshot(snapshot: Snapshot): Promise<boolean> {
const {isValid} = await snapshot.validate(
this.flags.deleteMissingResources
);

if (!isValid) {
await this.handleReportWithErrors(snapshot);
}

return isValid;
}

protected async getTargetOrg() {
if (this.flags.target) {
return this.flags.target;
}
const cfg = await this.configuration.get();
return cfg.organization;
}

protected async handleReportWithErrors(snapshot: Snapshot) {
// TODO: CDX-362: handle invalid snapshot cases
const pathToReport = snapshot.saveDetailedReport(this.flags.projectPath);
const report = snapshot.latestReport;

if (snapshot.requiresSynchronization()) {
const synchronizationPlanUrl = await this.getSynchronizationPage(
snapshot
);
this.warn(
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}
`
);
return;
}

const snapshotUrl = await this.getSnapshotPage(snapshot);

this.error(
dedent`Invalid snapshot - ${report.resultCode}.
Detailed report saved at ${pathToReport}.

You can also use this link to view the snapshot in the Coveo Admin Console
${snapshotUrl}`
);
}

private get flags() {
const {flags} = this.parse(this.ctor as typeof SnapshotBase);
return flags;
}

private get configuration() {
return new Config(this.config.configDir, this.error);
}

private async getSnapshotPage(snapshot: Snapshot) {
const options = await this.getSnapshotUrlOptions(snapshot);
return platformSnapshotUrl(options);
}

private async getSynchronizationPage(snapshot: Snapshot) {
const options = await this.getSnapshotUrlOptions(snapshot);
return platformSnapshotSynchronizationUrl(options);
}

private async getSnapshotUrlOptions(snapshot: Snapshot) {
const {environment} = await this.configuration.get();
return {
environment,
targetOrgId: snapshot.targetId,
snapshotId: snapshot.id,
};
}
}
114 changes: 4 additions & 110 deletions packages/cli/src/commands/org/config/preview.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,21 @@
import {Command, flags} from '@oclif/command';
import {cli} from 'cli-ux';
import {ReadStream} from 'fs';
import {dedent} from 'ts-dedent';
import {cwd} from 'process';
import {Config} from '../../../lib/config/config';
import {
IsAuthenticated,
Preconditions,
} from '../../../lib/decorators/preconditions';
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';
import SnapshotBase from './orgConfigBase';

export interface CustomFile extends ReadStream {
type?: string;
}

export default class Preview extends Command {
export default class Preview extends SnapshotBase {
public static description = 'Preview resource updates';

public static flags = {
target: flags.string({
char: 't',
description:
'The unique identifier of the organization where to send the changes. If not specified, the organization you are connected to will be used.',
helpValue: 'destinationorganizationg7dg3gd',
required: false,
}),
projectPath: flags.string({
char: 'p',
description: 'The path to your Coveo project.',
helpValue: '/Users/Me/my-project',
default: cwd(),
required: false,
}),
showResourcesToDelete: flags.boolean({
char: 'd',
description: 'Whether or not to show resources to delete',
default: false,
required: false,
}),
...SnapshotBase.flags,
};

public static hidden = true;

@Preconditions(IsAuthenticated())
public async run() {
const {flags} = this.parse(Preview);
const project = new Project(normalize(flags.projectPath));
const pathToZip = await project.compressResources();
const targetOrg = await this.getTargetOrg();

cli.action.start('Creating snapshot');

const snapshot = await SnapshotFactory.createFromZip(pathToZip, targetOrg);

cli.action.start('Validating snapshot');

const {isValid} = await snapshot.validate(flags.showResourcesToDelete);

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

cli.action.stop(isValid ? green('✔') : red.bold('!'));
const {isValid, snapshot, project} = await this.dryRun();

await snapshot.preview();

Expand All @@ -76,59 +25,4 @@ export default class Preview extends Command {

project.deleteTemporaryZipFile();
}

public async getTargetOrg() {
const {flags} = this.parse(Preview);
if (flags.target) {
return flags.target;
}
const cfg = await this.configuration.get();
return cfg.organization;
}

private async handleInvalidSnapshot(snapshot: Snapshot) {
// TODO: CDX-362: handle invalid snapshot cases
const {flags} = this.parse(Preview);
const pathToReport = snapshot.saveDetailedReport(flags.projectPath);
const report = snapshot.latestReport;

if (snapshot.requiresSynchronization()) {
const synchronizationPlanUrl = await this.getSynchronizationPage(
snapshot
);
this.warn(
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}
`
);
return;
}

const snapshotUrl = await this.getSnapshotPage(snapshot);

this.error(
dedent`Invalid snapshot - ${report.resultCode}.
Detailed report saved at ${pathToReport}.

You can also use this link to view the snapshot in the Coveo Admin Console
${snapshotUrl}`
);
}

private get configuration() {
return new Config(this.config.configDir, this.error);
}

private async getSnapshotPage(snapshot: Snapshot) {
const {environment} = await this.configuration.get();
const url = platformUrl({environment});
const targetOrg = snapshot.targetId;
return `${url}/admin/#${targetOrg}/organization/resource-snapshots/${snapshot.id}`;
}

private async getSynchronizationPage(snapshot: Snapshot) {
return `${await this.getSnapshotPage(snapshot)}/synchronization`;
}
}
66 changes: 66 additions & 0 deletions packages/cli/src/commands/org/config/push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {cli} from 'cli-ux';
import {ReadStream} from 'fs';
import {
IsAuthenticated,
Preconditions,
} from '../../../lib/decorators/preconditions';
import {Snapshot} from '../../../lib/snapshot/snapshot';
import {red, green, bold} from 'chalk';
import SnapshotBase from './orgConfigBase';

export interface CustomFile extends ReadStream {
type?: string;
}

export default class Push extends SnapshotBase {
public static description =
'Preview, validate and deploy your changes to the destination org';

public static flags = {
...SnapshotBase.flags,
};

public static hidden = true;

@Preconditions(IsAuthenticated())
public async run() {
const {isValid, snapshot, project} = await this.dryRun();

await snapshot.preview();

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

project.deleteTemporaryZipFile();
}

private async handleValidReport(snapshot: Snapshot) {
if (!snapshot.hasChangedResources()) {
return;
}
const targetOrg = await this.getTargetOrg();
const canBeApplied = await cli.confirm(
`\nWould you like to apply these changes to the org ${bold(
targetOrg
)}? (y/n)`
);

if (canBeApplied) {
await this.applySnapshot(snapshot);
}
}

private async applySnapshot(snapshot: Snapshot) {
cli.action.start('Applying snapshot');
const {flags} = this.parse(Push);
const {isValid} = await snapshot.apply(flags.deleteMissingResources);

if (!isValid) {
await this.handleReportWithErrors(snapshot);
}

cli.action.stop(isValid ? green('✔') : red.bold('!'));
}
}
14 changes: 14 additions & 0 deletions packages/cli/src/lib/platform/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ export function platformUrl<E extends PlatformEnvironment = 'prod'>(options?: {
return `https://platform${urlEnv}${urlRegion}.cloud.coveo.com`;
}

export function platformSnapshotUrl<
E extends PlatformEnvironment = 'prod'
>(options: {environment: E; targetOrgId: string; snapshotId: string}) {
const url = platformUrl({environment: options.environment});
return `${url}/admin/#${options.targetOrgId}/organization/resource-snapshots/${options.snapshotId}`;
}

export function platformSnapshotSynchronizationUrl<
E extends PlatformEnvironment = 'prod'
>(options: {environment: E; targetOrgId: string; snapshotId: string}) {
const url = platformSnapshotUrl(options);
return `${url}/synchronization`;
}

y-lakhdar marked this conversation as resolved.
Show resolved Hide resolved
export function castEnvironmentToPlatformClient(
e: PlatformEnvironment
): Environment {
Expand Down
Loading