@speedkit/cli 4.35.1 → 4.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +42 -7
  3. package/dist/commands/bulk-deploy.d.ts +19 -0
  4. package/dist/commands/bulk-deploy.js +52 -0
  5. package/dist/commands/deploy.d.ts +2 -0
  6. package/dist/commands/deploy.js +32 -2
  7. package/dist/services/cli/cli-service.js +42 -2
  8. package/dist/services/cli/cli-service.spec.js +41 -0
  9. package/dist/services/deploy/bulk/bulk-deploy-model.d.ts +46 -0
  10. package/dist/services/deploy/bulk/bulk-deploy-model.js +27 -0
  11. package/dist/services/deploy/bulk/bulk-deploy-service-factory.d.ts +11 -0
  12. package/dist/services/deploy/bulk/bulk-deploy-service-factory.js +24 -0
  13. package/dist/services/deploy/bulk/bulk-deploy-service.d.ts +47 -0
  14. package/dist/services/deploy/bulk/bulk-deploy-service.js +201 -0
  15. package/dist/services/deploy/bulk/bulk-deploy-service.spec.d.ts +1 -0
  16. package/dist/services/deploy/bulk/bulk-deploy-service.spec.js +146 -0
  17. package/dist/services/deploy/bulk/config-deploy-factory.d.ts +13 -0
  18. package/dist/services/deploy/bulk/config-deploy-factory.js +20 -0
  19. package/dist/services/deploy/bulk/index.d.ts +5 -0
  20. package/dist/services/deploy/bulk/index.js +5 -0
  21. package/dist/services/deploy/bulk/touched-configs.d.ts +12 -0
  22. package/dist/services/deploy/bulk/touched-configs.js +54 -0
  23. package/dist/services/deploy/bulk/touched-configs.spec.d.ts +1 -0
  24. package/dist/services/deploy/bulk/touched-configs.spec.js +77 -0
  25. package/dist/services/deploy/checks/activation/customer-approval-check.d.ts +3 -1
  26. package/dist/services/deploy/checks/activation/customer-approval-check.js +5 -2
  27. package/dist/services/deploy/checks/activation/customer-approval-check.spec.js +18 -15
  28. package/dist/services/deploy/checks/post-deploy/deprecated-deployment-detection-check.d.ts +3 -1
  29. package/dist/services/deploy/checks/post-deploy/deprecated-deployment-detection-check.js +6 -3
  30. package/dist/services/deploy/checks/post-deploy/save-files-check.d.ts +3 -1
  31. package/dist/services/deploy/checks/post-deploy/save-files-check.js +5 -2
  32. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.d.ts +3 -1
  33. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +9 -5
  34. package/dist/services/deploy/checks/pre-deploy/split-change-check.d.ts +3 -1
  35. package/dist/services/deploy/checks/pre-deploy/split-change-check.js +8 -5
  36. package/dist/services/deploy/checks/pre-deploy/split-change-check.spec.d.ts +1 -0
  37. package/dist/services/deploy/checks/pre-deploy/split-change-check.spec.js +55 -0
  38. package/dist/services/deploy/context/deploy-context.d.ts +21 -1
  39. package/dist/services/deploy/context/deploy-context.js +30 -1
  40. package/dist/services/deploy/deploy-service-factory.d.ts +13 -1
  41. package/dist/services/deploy/deploy-service-factory.js +36 -15
  42. package/dist/services/deploy/error/deploy-aborted-error.d.ts +12 -0
  43. package/dist/services/deploy/error/deploy-aborted-error.js +16 -0
  44. package/dist/services/deploy/handler/customer-config-handler.d.ts +3 -1
  45. package/dist/services/deploy/handler/customer-config-handler.js +11 -7
  46. package/dist/services/deploy/handler/install-resource-handler.d.ts +3 -1
  47. package/dist/services/deploy/handler/install-resource-handler.js +12 -7
  48. package/dist/services/deploy/handler/install-resource-modifier/deleted-file-check.d.ts +3 -1
  49. package/dist/services/deploy/handler/install-resource-modifier/deleted-file-check.js +5 -2
  50. package/dist/services/deploy/handler/module-handler.d.ts +3 -1
  51. package/dist/services/deploy/handler/module-handler.js +7 -4
  52. package/dist/services/deploy/handler/module-handler.spec.d.ts +1 -0
  53. package/dist/services/deploy/handler/module-handler.spec.js +49 -0
  54. package/dist/services/deploy/handler/server-config-handler.d.ts +3 -1
  55. package/dist/services/deploy/handler/server-config-handler.js +6 -3
  56. package/dist/services/deploy/mr/base-config-reader.d.ts +25 -0
  57. package/dist/services/deploy/mr/base-config-reader.js +52 -0
  58. package/dist/services/deploy/mr/index.d.ts +6 -0
  59. package/dist/services/deploy/mr/index.js +6 -0
  60. package/dist/services/deploy/mr/live-state-check.d.ts +38 -0
  61. package/dist/services/deploy/mr/live-state-check.js +81 -0
  62. package/dist/services/deploy/mr/live-state-check.spec.d.ts +1 -0
  63. package/dist/services/deploy/mr/live-state-check.spec.js +74 -0
  64. package/dist/services/deploy/mr/merge-request-deploy-model.d.ts +59 -0
  65. package/dist/services/deploy/mr/merge-request-deploy-model.js +50 -0
  66. package/dist/services/deploy/mr/merge-request-deploy-service-factory.d.ts +20 -0
  67. package/dist/services/deploy/mr/merge-request-deploy-service-factory.js +46 -0
  68. package/dist/services/deploy/mr/merge-request-deploy-service.d.ts +61 -0
  69. package/dist/services/deploy/mr/merge-request-deploy-service.js +189 -0
  70. package/dist/services/deploy/mr/merge-request-description.d.ts +16 -0
  71. package/dist/services/deploy/mr/merge-request-description.js +27 -0
  72. package/dist/services/deploy/mr/merge-request-description.spec.d.ts +1 -0
  73. package/dist/services/deploy/mr/merge-request-description.spec.js +55 -0
  74. package/dist/services/deploy/policy/auto-deploy-policy.d.ts +43 -0
  75. package/dist/services/deploy/policy/auto-deploy-policy.js +77 -0
  76. package/dist/services/deploy/policy/auto-deploy-policy.spec.d.ts +1 -0
  77. package/dist/services/deploy/policy/auto-deploy-policy.spec.js +83 -0
  78. package/dist/services/deploy/policy/deploy-policy-model.d.ts +61 -0
  79. package/dist/services/deploy/policy/deploy-policy-model.js +41 -0
  80. package/dist/services/deploy/policy/index.d.ts +2 -0
  81. package/dist/services/deploy/policy/index.js +2 -0
  82. package/dist/services/deploy/policy/interactive-deploy-policy.d.ts +15 -0
  83. package/dist/services/deploy/policy/interactive-deploy-policy.js +16 -0
  84. package/dist/services/diff/diff-model.d.ts +5 -1
  85. package/dist/services/diff/diff-model.js +5 -1
  86. package/dist/services/diff/diff-service-factory.js +1 -1
  87. package/dist/services/diff/diff-service.d.ts +2 -1
  88. package/dist/services/diff/diff-service.js +6 -4
  89. package/dist/services/git/git-repository-model.d.ts +13 -0
  90. package/dist/services/git/git-repository-model.js +1 -0
  91. package/dist/services/git/git-repository.d.ts +35 -0
  92. package/dist/services/git/git-repository.js +120 -0
  93. package/dist/services/git/git-repository.spec.d.ts +1 -0
  94. package/dist/services/git/git-repository.spec.js +87 -0
  95. package/dist/services/git/index.d.ts +2 -0
  96. package/dist/services/git/index.js +2 -0
  97. package/dist/services/integration-api/spec/speed-kit-config-file.spec.js +1 -1
  98. package/oclif.manifest.json +140 -34
  99. package/package.json +1 -1
@@ -0,0 +1,201 @@
1
+ import { ApprovalAnswer } from "../policy/auto-deploy-policy.js";
2
+ import { DeployOutcome, resolveDescription, } from "../mr/index.js";
3
+ import { productionConfigOf, touchedCustomers } from "./touched-configs.js";
4
+ /**
5
+ * Deploys every customer configuration of a merged merge request.
6
+ *
7
+ * The run asks its questions once, then plans every configuration without
8
+ * writing anything, and applies only what the plan found deployable. The
9
+ * summary names every configuration it left out, so a developer can take those
10
+ * over by hand.
11
+ */
12
+ export class BulkDeployService {
13
+ context;
14
+ repository;
15
+ request;
16
+ deployFactory;
17
+ cli;
18
+ constructor(context, repository, request, deployFactory, cli) {
19
+ this.context = context;
20
+ this.repository = repository;
21
+ this.request = request;
22
+ this.deployFactory = deployFactory;
23
+ this.cli = cli;
24
+ }
25
+ async run() {
26
+ const configs = this.productionConfigs();
27
+ this.cli.spacer();
28
+ this.cli.write(`!${this.request.iid} ${this.request.title}`);
29
+ if (configs.length === 0) {
30
+ this.cli.writeWarning(`!${this.request.iid} changes no customer configuration`);
31
+ return [];
32
+ }
33
+ this.cli.write(`${configs.length} customer(s)`);
34
+ const answers = await this.ask(configs);
35
+ const planned = await this.planAll(configs, answers);
36
+ this.writeTable("Plan", planned);
37
+ const deployable = planned.filter((entry) => entry.result.outcome === DeployOutcome.Deployable);
38
+ if (deployable.length > 0 && (await this.isConfirmed(deployable.length))) {
39
+ await this.applyAll(deployable);
40
+ }
41
+ else if (deployable.length > 0) {
42
+ for (const entry of deployable) {
43
+ entry.result = {
44
+ ...entry.result,
45
+ outcome: DeployOutcome.Skipped,
46
+ reason: "the run was not confirmed",
47
+ };
48
+ }
49
+ }
50
+ return this.summarize(planned);
51
+ }
52
+ /**
53
+ * The production config of every customer the merge request changes.
54
+ *
55
+ * A merge request deploys production alone. Staging and every other
56
+ * environment stay in the hand of a developer.
57
+ */
58
+ productionConfigs() {
59
+ const configs = [];
60
+ const without = [];
61
+ for (const directory of touchedCustomers(this.repository, this.repository.changedPaths(this.request))) {
62
+ const config = productionConfigOf(this.repository.root, directory);
63
+ if (config) {
64
+ configs.push(config);
65
+ }
66
+ else {
67
+ without.push(directory);
68
+ }
69
+ }
70
+ if (without.length > 0) {
71
+ this.cli.writeWarning(`No production config, so left out: ${without.join(", ")}`);
72
+ }
73
+ return configs;
74
+ }
75
+ /** The questions of the whole run, asked before the first configuration. */
76
+ async ask(configs) {
77
+ return {
78
+ description: await resolveDescription(this.cli, this.request, this.context.description),
79
+ approval: await this.resolveApproval(configs),
80
+ };
81
+ }
82
+ /**
83
+ * What happens with the customers that approve every rollout themselves.
84
+ *
85
+ * The answer is asked only while such a customer is part of the run, and it
86
+ * changes nothing for every other customer.
87
+ */
88
+ async resolveApproval(configs) {
89
+ if (this.context.approval) {
90
+ return this.context.approval;
91
+ }
92
+ const approving = configs.filter((config) => config.needsApproval);
93
+ if (approving.length === 0) {
94
+ return ApprovalAnswer.Approved;
95
+ }
96
+ this.cli.spacer();
97
+ this.cli.writeWarning("These customers approve every rollout themselves:");
98
+ this.cli.code([...new Set(approving.map((c) => c.directory))].join(", "));
99
+ const answer = await this.cli.select("What happens with them?", [
100
+ { name: "leave them to a developer", value: ApprovalAnswer.Skip },
101
+ {
102
+ name: "activate as a testing installation",
103
+ value: ApprovalAnswer.Testing,
104
+ },
105
+ {
106
+ name: "the customers approved this change, activate it",
107
+ value: ApprovalAnswer.Approved,
108
+ },
109
+ ], ApprovalAnswer.Skip);
110
+ return answer;
111
+ }
112
+ /** Plans every configuration, without writing anything. */
113
+ async planAll(configs, answers) {
114
+ const planned = [];
115
+ for (const config of configs) {
116
+ const name = `${config.directory} [${config.configName}]`;
117
+ this.cli.startAction(`BULK:PLAN:${name}`, `plan ${name}`);
118
+ const entry = await this.planOne(config, answers);
119
+ this.cli.successAction(`BULK:PLAN:${name}`, `${name}: ${entry.result.outcome}`);
120
+ planned.push(entry);
121
+ }
122
+ return planned;
123
+ }
124
+ async planOne(config, answers) {
125
+ try {
126
+ const service = await this.deployFactory.build(config, answers);
127
+ return { config, service, result: await service.plan() };
128
+ }
129
+ catch (error) {
130
+ // A configuration that cannot even be read, or an app that refuses the
131
+ // login, is one result of the run, never its end.
132
+ return { config, result: this.failed(config, error) };
133
+ }
134
+ }
135
+ /** Deploys every configuration the plan found deployable. */
136
+ async applyAll(deployable) {
137
+ for (const entry of deployable) {
138
+ const name = `${entry.config.directory} [${entry.config.configName}]`;
139
+ this.cli.spacer();
140
+ this.cli.write(`${name}`);
141
+ try {
142
+ // The deploy plans again, because live can change between the plan
143
+ // and the deploy.
144
+ entry.result = await entry.service.run();
145
+ }
146
+ catch (error) {
147
+ entry.result = this.failed(entry.config, error);
148
+ }
149
+ }
150
+ }
151
+ async isConfirmed(count) {
152
+ if (this.context.assumeYes) {
153
+ return true;
154
+ }
155
+ const what = this.context.dryRun ? "Dry run" : "Deploy";
156
+ return this.cli.confirm(`${what} ${count} configuration(s) of !${this.request.iid}?`, false);
157
+ }
158
+ /** The summary of the run, and everything a developer has to take over. */
159
+ summarize(planned) {
160
+ const results = planned.map((entry) => entry.result);
161
+ const applied = results.filter((result) => result.outcome === DeployOutcome.Applied);
162
+ // Without a deploy the plan above already is the summary.
163
+ if (applied.length > 0) {
164
+ this.writeTable("Summary", planned.filter((entry) => entry.result.outcome !== DeployOutcome.Applied));
165
+ }
166
+ const byHand = results.flatMap((result) => result.notDeployed ?? []);
167
+ if (byHand.length > 0) {
168
+ this.cli.writeWarning("Deploy these files by hand:");
169
+ this.cli.code([...new Set(byHand)].join(", "));
170
+ }
171
+ this.cli.spacer();
172
+ this.cli.write(`${applied.length} of ${results.length} configuration(s) deployed`);
173
+ return results;
174
+ }
175
+ writeTable(title, planned) {
176
+ if (planned.length === 0) {
177
+ return;
178
+ }
179
+ this.cli.spacer();
180
+ this.cli.write(title);
181
+ // Every configuration of a merge request run is production, so the name
182
+ // of the config is no column of its own.
183
+ const rows = planned.map((entry) => ({
184
+ customer: entry.config.directory.split("/").pop(),
185
+ outcome: entry.result.outcome,
186
+ reason: entry.result.reason ?? "",
187
+ }));
188
+ const hasReason = rows.some((row) => row.reason.length > 0);
189
+ this.cli.table(rows, hasReason
190
+ ? { customer: {}, outcome: {}, reason: {} }
191
+ : { customer: {}, outcome: {} });
192
+ }
193
+ failed(config, error) {
194
+ return {
195
+ customerPath: config.customerPath,
196
+ configName: config.configName,
197
+ outcome: DeployOutcome.Failed,
198
+ reason: error.message,
199
+ };
200
+ }
201
+ }
@@ -0,0 +1,146 @@
1
+ import { expect } from "chai";
2
+ import { after, before, describe, it } from "mocha";
3
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
4
+ import os from "node:os";
5
+ import path from "node:path";
6
+ import { BulkDeployService } from "./bulk-deploy-service.js";
7
+ import { BulkDeployContext, } from "./bulk-deploy-model.js";
8
+ import { GitRepository } from "../../git/index.js";
9
+ import { DeployOutcome, } from "../mr/index.js";
10
+ import { ApprovalAnswer } from "../policy/auto-deploy-policy.js";
11
+ const REQUEST = {
12
+ iid: 8017,
13
+ sha: "3aaa523",
14
+ baseSha: "83d9d58",
15
+ title: "key the cache on the served theme",
16
+ };
17
+ let root;
18
+ before(() => {
19
+ root = mkdtempSync(path.join(os.tmpdir(), "sk-bulk-run-"));
20
+ writeCustomer("customers/gohaus", {
21
+ production: { app: "gohaus" },
22
+ staging: { name: "staging" },
23
+ });
24
+ writeCustomer("customers/bonprix", {
25
+ production: { app: "bonprix", needsCustomerApproval: true },
26
+ });
27
+ });
28
+ after(() => rmSync(root, { recursive: true, force: true }));
29
+ function writeCustomer(directory, config) {
30
+ mkdirSync(path.join(root, directory), { recursive: true });
31
+ writeFileSync(path.join(root, directory, "config_customer.json"), JSON.stringify(config));
32
+ }
33
+ /** A CLI that answers every question the same way, and records them. */
34
+ function cliAnswering(confirmed, selected = ApprovalAnswer.Skip) {
35
+ const questions = [];
36
+ const cli = {
37
+ spacer: () => undefined,
38
+ write: () => undefined,
39
+ writeWarning: () => undefined,
40
+ code: () => undefined,
41
+ table: () => undefined,
42
+ startAction: () => undefined,
43
+ successAction: () => undefined,
44
+ prompt: async () => "a description",
45
+ confirm: async (message) => {
46
+ questions.push(message);
47
+ return confirmed;
48
+ },
49
+ select: async (message) => {
50
+ questions.push(message);
51
+ return selected;
52
+ },
53
+ };
54
+ return { cli, questions };
55
+ }
56
+ /** A deploy that plans as told, and records what was deployed. */
57
+ function deployFactory(plans, deployed, failing = []) {
58
+ return {
59
+ build: async (config, answers) => {
60
+ const name = `${config.directory}[${config.configName}]`;
61
+ if (failing.includes(name)) {
62
+ throw new Error(`Could not login to app ${config.directory}`);
63
+ }
64
+ const result = (outcome) => ({
65
+ customerPath: config.customerPath,
66
+ configName: config.configName,
67
+ outcome,
68
+ reason: answers.description,
69
+ });
70
+ return {
71
+ plan: async () => result(plans[name] ?? DeployOutcome.Deployable),
72
+ run: async () => {
73
+ deployed.push(name);
74
+ return result(DeployOutcome.Applied);
75
+ },
76
+ };
77
+ },
78
+ };
79
+ }
80
+ function serviceOf(changedPaths, factory, cli, approval) {
81
+ const repository = new GitRepository(root, () => "");
82
+ repository.changedPaths = () => changedPaths;
83
+ return new BulkDeployService(new BulkDeployContext(root, REQUEST.iid, approval, "a description"), repository, REQUEST, factory, cli);
84
+ }
85
+ describe("BulkDeployService", () => {
86
+ it("deploys every configuration the plan found deployable", async () => {
87
+ const deployed = [];
88
+ const { cli } = cliAnswering(true);
89
+ const factory = deployFactory({}, deployed);
90
+ const results = await serviceOf(["customers/gohaus/config_SpeedKit.js"], factory, cli, ApprovalAnswer.Approved).run();
91
+ expect(deployed).to.deep.equal(["customers/gohaus[production]"]);
92
+ expect(results.map((result) => result.outcome)).to.deep.equal([
93
+ DeployOutcome.Applied,
94
+ ]);
95
+ });
96
+ it("deploys the plan without asking while yes is set", async () => {
97
+ const deployed = [];
98
+ const { cli, questions } = cliAnswering(false);
99
+ const repository = new GitRepository(root, () => "");
100
+ repository.changedPaths = () => ["customers/gohaus/config_SpeedKit.js"];
101
+ await new BulkDeployService(new BulkDeployContext(root, REQUEST.iid, ApprovalAnswer.Approved, "a description", false, true), repository, REQUEST, deployFactory({}, deployed), cli).run();
102
+ expect(deployed).to.deep.equal(["customers/gohaus[production]"]);
103
+ expect(questions).to.deep.equal([]);
104
+ });
105
+ it("deploys nothing while the run is not confirmed", async () => {
106
+ const deployed = [];
107
+ const { cli } = cliAnswering(false);
108
+ const results = await serviceOf(["customers/gohaus/config_SpeedKit.js"], deployFactory({}, deployed), cli, ApprovalAnswer.Approved).run();
109
+ expect(deployed).to.deep.equal([]);
110
+ expect(results.map((result) => result.outcome)).to.deep.equal([
111
+ DeployOutcome.Skipped,
112
+ ]);
113
+ expect(results[0].reason).to.equal("the run was not confirmed");
114
+ });
115
+ it("keeps the run going while one configuration cannot be built", async () => {
116
+ const deployed = [];
117
+ const { cli } = cliAnswering(true);
118
+ const factory = deployFactory({}, deployed, [
119
+ "customers/bonprix[production]",
120
+ ]);
121
+ const results = await serviceOf([
122
+ "customers/gohaus/config_SpeedKit.js",
123
+ "customers/bonprix/config_SpeedKit.js",
124
+ ], factory, cli, ApprovalAnswer.Approved).run();
125
+ expect(deployed).to.deep.equal(["customers/gohaus[production]"]);
126
+ expect(results[0].outcome).to.equal(DeployOutcome.Failed);
127
+ expect(results[0].reason).to.contain("Could not login");
128
+ });
129
+ it("asks nothing while the merge request changes no customer", async () => {
130
+ const { cli, questions } = cliAnswering(true);
131
+ const results = await serviceOf(["rules/kameleoon.md"], deployFactory({}, []), cli).run();
132
+ expect(results).to.deep.equal([]);
133
+ expect(questions).to.deep.equal([]);
134
+ });
135
+ it("asks about the customers that approve every rollout themselves", async () => {
136
+ const deployed = [];
137
+ const { cli, questions } = cliAnswering(true, ApprovalAnswer.Skip);
138
+ await serviceOf(["customers/bonprix/config_SpeedKit.js"], deployFactory({}, deployed), cli).run();
139
+ expect(questions[0]).to.equal("What happens with them?");
140
+ });
141
+ it("does not ask while no customer of the run approves rollouts", async () => {
142
+ const { cli, questions } = cliAnswering(true);
143
+ await serviceOf(["customers/gohaus/config_SpeedKit.js"], deployFactory({}, []), cli).run();
144
+ expect(questions).to.deep.equal(["Deploy 1 configuration(s) of !8017?"]);
145
+ });
146
+ });
@@ -0,0 +1,13 @@
1
+ import { UserCliConfig } from "../../../helpers/cli-config.js";
2
+ import { GitRepository, MergedRequest } from "../../git/index.js";
3
+ import { MergeRequestDeployService } from "../mr/index.js";
4
+ import { BulkAnswers, ConfigDeployFactoryInterface, TouchedConfig } from "./bulk-deploy-model.js";
5
+ /** Builds the deploy of one configuration of the bulk run. */
6
+ export declare class ConfigDeployFactory implements ConfigDeployFactoryInterface {
7
+ private readonly cliConfig;
8
+ private readonly repository;
9
+ private readonly request;
10
+ private readonly dryRun;
11
+ constructor(cliConfig: UserCliConfig, repository: GitRepository, request: MergedRequest, dryRun: boolean);
12
+ build(config: TouchedConfig, answers: BulkAnswers): Promise<MergeRequestDeployService>;
13
+ }
@@ -0,0 +1,20 @@
1
+ import { MergeRequestDeployContext, MergeRequestDeployServiceFactory, } from "../mr/index.js";
2
+ /** Builds the deploy of one configuration of the bulk run. */
3
+ export class ConfigDeployFactory {
4
+ cliConfig;
5
+ repository;
6
+ request;
7
+ dryRun;
8
+ constructor(cliConfig, repository, request, dryRun) {
9
+ this.cliConfig = cliConfig;
10
+ this.repository = repository;
11
+ this.request = request;
12
+ this.dryRun = dryRun;
13
+ }
14
+ async build(config, answers) {
15
+ return new MergeRequestDeployServiceFactory(new MergeRequestDeployContext(config.customerPath, config.configName, this.request.iid, answers.approval, answers.description, this.dryRun,
16
+ // A run over many customers pays for every check once per customer,
17
+ // so the checks after the upload are left to sk deploy.
18
+ true), this.cliConfig, this.repository, this.request).buildService();
19
+ }
20
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./bulk-deploy-model.js";
2
+ export * from "./touched-configs.js";
3
+ export * from "./bulk-deploy-service.js";
4
+ export * from "./config-deploy-factory.js";
5
+ export * from "./bulk-deploy-service-factory.js";
@@ -0,0 +1,5 @@
1
+ export * from "./bulk-deploy-model.js";
2
+ export * from "./touched-configs.js";
3
+ export * from "./bulk-deploy-service.js";
4
+ export * from "./config-deploy-factory.js";
5
+ export * from "./bulk-deploy-service-factory.js";
@@ -0,0 +1,12 @@
1
+ import { GitRepository } from "../../git/index.js";
2
+ import { TouchedConfig } from "./bulk-deploy-model.js";
3
+ /**
4
+ * The customer directories that the changed paths belong to.
5
+ *
6
+ * A path belongs to the closest directory above it that holds a
7
+ * config_customer.json, so a change outside the customers, such as one in the
8
+ * rules or the tools, belongs to none.
9
+ */
10
+ export declare function touchedCustomers(repository: GitRepository, changedPaths: string[]): string[];
11
+ /** The production configuration of the customer, while it declares one. */
12
+ export declare function productionConfigOf(root: string, directory: string): TouchedConfig | null;
@@ -0,0 +1,54 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import path from "node:path";
3
+ /** The file that makes a directory a customer configuration. */
4
+ const CUSTOMER_CONFIG = "config_customer.json";
5
+ /**
6
+ * The only environment a merge request deploys.
7
+ *
8
+ * Staging and every other environment stay in the hand of a developer.
9
+ */
10
+ const PRODUCTION = "production";
11
+ /**
12
+ * The customer directories that the changed paths belong to.
13
+ *
14
+ * A path belongs to the closest directory above it that holds a
15
+ * config_customer.json, so a change outside the customers, such as one in the
16
+ * rules or the tools, belongs to none.
17
+ */
18
+ export function touchedCustomers(repository, changedPaths) {
19
+ const directories = new Set();
20
+ for (const changedPath of changedPaths) {
21
+ const directory = customerDirectoryOf(repository.root, changedPath);
22
+ if (directory) {
23
+ directories.add(directory);
24
+ }
25
+ }
26
+ return [...directories].sort();
27
+ }
28
+ /** The production configuration of the customer, while it declares one. */
29
+ export function productionConfigOf(root, directory) {
30
+ const customerPath = path.join(root, directory);
31
+ const configs = JSON.parse(readFileSync(path.join(customerPath, CUSTOMER_CONFIG), "utf8"));
32
+ const production = configs[PRODUCTION];
33
+ if (!production || typeof production !== "object") {
34
+ return null;
35
+ }
36
+ return {
37
+ directory,
38
+ customerPath,
39
+ configName: PRODUCTION,
40
+ needsApproval: Boolean(production.needsCustomerApproval),
41
+ };
42
+ }
43
+ /** The customer directory the path belongs to, as git spells it. */
44
+ function customerDirectoryOf(root, changedPath) {
45
+ const segments = changedPath.split("/").slice(0, -1);
46
+ while (segments.length > 0) {
47
+ const directory = segments.join("/");
48
+ if (existsSync(path.join(root, directory, CUSTOMER_CONFIG))) {
49
+ return directory;
50
+ }
51
+ segments.pop();
52
+ }
53
+ return null;
54
+ }
@@ -0,0 +1,77 @@
1
+ import { expect } from "chai";
2
+ import { after, before, describe, it } from "mocha";
3
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
4
+ import os from "node:os";
5
+ import path from "node:path";
6
+ import { productionConfigOf, touchedCustomers } from "./touched-configs.js";
7
+ import { GitRepository } from "../../git/index.js";
8
+ let root;
9
+ /** A repository with two customers, one of them with two environments. */
10
+ before(() => {
11
+ root = mkdtempSync(path.join(os.tmpdir(), "sk-bulk-"));
12
+ writeCustomer("customers/gohaus", {
13
+ $schema: "../schema.json",
14
+ production: { app: "gohaus", origins: ["https://www.gohaus.com"] },
15
+ staging: { name: "staging", origins: ["https://staging.gohaus.com"] },
16
+ });
17
+ writeCustomer("customers/bonprix", {
18
+ production: { app: "bonprix", needsCustomerApproval: true },
19
+ staging: { name: "staging" },
20
+ });
21
+ writeCustomer("customers/staging-only", {
22
+ staging: { name: "staging" },
23
+ });
24
+ mkdirSync(path.join(root, "rules"), { recursive: true });
25
+ });
26
+ after(() => rmSync(root, { recursive: true, force: true }));
27
+ function writeCustomer(directory, config) {
28
+ mkdirSync(path.join(root, directory), { recursive: true });
29
+ writeFileSync(path.join(root, directory, "config_customer.json"), JSON.stringify(config));
30
+ }
31
+ function repository() {
32
+ return new GitRepository(root, () => "");
33
+ }
34
+ describe("touchedCustomers", () => {
35
+ it("names the customer of a changed file", () => {
36
+ expect(touchedCustomers(repository(), ["customers/gohaus/config_SpeedKit.js"])).to.deep.equal(["customers/gohaus"]);
37
+ });
38
+ it("names a customer once, whatever it changed", () => {
39
+ const customers = touchedCustomers(repository(), [
40
+ "customers/gohaus/config_SpeedKit.js",
41
+ "customers/gohaus/config_documentHandler.js",
42
+ "customers/gohaus/test/ux-report.plan.json",
43
+ ]);
44
+ expect(customers).to.deep.equal(["customers/gohaus"]);
45
+ });
46
+ it("leaves out a change that belongs to no customer", () => {
47
+ const customers = touchedCustomers(repository(), [
48
+ "rules/kameleoon.md",
49
+ "tools/worktree.mjs",
50
+ "package.json",
51
+ ]);
52
+ expect(customers).to.deep.equal([]);
53
+ });
54
+ it("sorts the customers of several changes", () => {
55
+ const customers = touchedCustomers(repository(), [
56
+ "customers/gohaus/config_SpeedKit.js",
57
+ "customers/bonprix/config_SpeedKit.js",
58
+ "rules/kameleoon.md",
59
+ ]);
60
+ expect(customers).to.deep.equal(["customers/bonprix", "customers/gohaus"]);
61
+ });
62
+ });
63
+ describe("productionConfigOf", () => {
64
+ it("reads the production config, and no other environment", () => {
65
+ const config = productionConfigOf(root, "customers/gohaus");
66
+ expect(config.configName).to.equal("production");
67
+ expect(config.directory).to.equal("customers/gohaus");
68
+ expect(config.customerPath).to.equal(path.join(root, "customers/gohaus"));
69
+ expect(config.needsApproval).to.equal(false);
70
+ });
71
+ it("reads the approval of the customer", () => {
72
+ expect(productionConfigOf(root, "customers/bonprix").needsApproval).to.equal(true);
73
+ });
74
+ it("reports a customer without a production config", () => {
75
+ expect(productionConfigOf(root, "customers/staging-only")).to.equal(null);
76
+ });
77
+ });
@@ -1,6 +1,7 @@
1
1
  import { CliServiceInterface } from "../../../cli/index.js";
2
2
  import { CustomerConfig } from "../../../integration-api/index.js";
3
3
  import Install from "../../interface/install.js";
4
+ import { DeployPolicyInterface } from "../../policy/index.js";
4
5
  /**
5
6
  * Some customers reserve the go-live decision for themselves: nothing may reach
6
7
  * their real users before they signed it off. `needsCustomerApproval` in
@@ -11,7 +12,8 @@ import Install from "../../interface/install.js";
11
12
  export declare class CustomerApprovalCheck {
12
13
  private cli;
13
14
  private customer;
14
- constructor(cli: CliServiceInterface, customer: CustomerConfig);
15
+ private policy;
16
+ constructor(cli: CliServiceInterface, customer: CustomerConfig, policy: DeployPolicyInterface);
15
17
  /**
16
18
  * Whether the installation may be offered for regular activation. `false`
17
19
  * means the deploy is limited to a testing installation.
@@ -1,5 +1,6 @@
1
1
  import { VirtualSpeedKitConfigFile, } from "../../../integration-api/index.js";
2
2
  import { safe } from "../../../../helpers/safe.js";
3
+ import { DeployQuestion } from "../../policy/index.js";
3
4
  /** The install resource key config_SpeedKit.js is stored under. */
4
5
  const SPEED_KIT_CONFIG_KEY = "config";
5
6
  /**
@@ -12,9 +13,11 @@ const SPEED_KIT_CONFIG_KEY = "config";
12
13
  export class CustomerApprovalCheck {
13
14
  cli;
14
15
  customer;
15
- constructor(cli, customer) {
16
+ policy;
17
+ constructor(cli, customer, policy) {
16
18
  this.cli = cli;
17
19
  this.customer = customer;
20
+ this.policy = policy;
18
21
  }
19
22
  /**
20
23
  * Whether the installation may be offered for regular activation. `false`
@@ -32,7 +35,7 @@ export class CustomerApprovalCheck {
32
35
  this.cli.writeWarning("This customer approves every rollout themselves and the configuration " +
33
36
  "to be activated has a split greater than 0:");
34
37
  this.cli.table(splits, { origin: {}, split: {} });
35
- return this.cli.confirm("Was this change approved by customer?", false);
38
+ return this.policy.confirm(DeployQuestion.CustomerApproval, "Was this change approved by customer?", false);
36
39
  }
37
40
  async getSplitsGreaterZero(install) {
38
41
  const speedKitConfig = install[SPEED_KIT_CONFIG_KEY];
@@ -21,43 +21,46 @@ function installWithSplit(split) {
21
21
  function cliAsking(answer) {
22
22
  const questions = [];
23
23
  const cli = {
24
- confirm: async (message) => {
25
- questions.push(message);
26
- return answer;
27
- },
28
24
  table: () => undefined,
29
25
  writeWarning: () => undefined,
30
26
  writeError: () => undefined,
31
27
  };
32
- return { cli, questions };
28
+ const policy = {
29
+ confirm: async (question, message) => {
30
+ questions.push(message);
31
+ return answer;
32
+ },
33
+ text: async () => "",
34
+ };
35
+ return { cli, policy, questions };
33
36
  }
34
37
  describe("CustomerApprovalCheck", () => {
35
38
  it("approves without asking when the customer does not need to approve", async () => {
36
- const { cli, questions } = cliAsking(false);
37
- const approved = await new CustomerApprovalCheck(cli, customer(false)).isActivationApproved(installWithSplit("1"));
39
+ const { cli, policy, questions } = cliAsking(false);
40
+ const approved = await new CustomerApprovalCheck(cli, customer(false), policy).isActivationApproved(installWithSplit("1"));
38
41
  expect(approved).to.equal(true);
39
42
  expect(questions).to.deep.equal([]);
40
43
  });
41
44
  it("approves without asking while no origin serves traffic", async () => {
42
- const { cli, questions } = cliAsking(false);
43
- const approved = await new CustomerApprovalCheck(cli, customer(true)).isActivationApproved(installWithSplit("0"));
45
+ const { cli, policy, questions } = cliAsking(false);
46
+ const approved = await new CustomerApprovalCheck(cli, customer(true), policy).isActivationApproved(installWithSplit("0"));
44
47
  expect(approved).to.equal(true);
45
48
  expect(questions).to.deep.equal([]);
46
49
  });
47
50
  it("asks once a single origin has a split greater than 0", async () => {
48
- const { cli, questions } = cliAsking(true);
49
- const approved = await new CustomerApprovalCheck(cli, customer(true)).isActivationApproved(installWithSplit('window.location.hostname === "www.example.com" ? 0.5 : 0'));
51
+ const { cli, policy, questions } = cliAsking(true);
52
+ const approved = await new CustomerApprovalCheck(cli, customer(true), policy).isActivationApproved(installWithSplit('window.location.hostname === "www.example.com" ? 0.5 : 0'));
50
53
  expect(approved).to.equal(true);
51
54
  expect(questions).to.deep.equal(["Was this change approved by customer?"]);
52
55
  });
53
56
  it("withholds the approval when the question is answered with no", async () => {
54
- const { cli } = cliAsking(false);
55
- const approved = await new CustomerApprovalCheck(cli, customer(true)).isActivationApproved(installWithSplit("0.1"));
57
+ const { cli, policy } = cliAsking(false);
58
+ const approved = await new CustomerApprovalCheck(cli, customer(true), policy).isActivationApproved(installWithSplit("0.1"));
56
59
  expect(approved).to.equal(false);
57
60
  });
58
61
  it("asks when the split cannot be evaluated", async () => {
59
- const { cli, questions } = cliAsking(false);
60
- const approved = await new CustomerApprovalCheck(cli, customer(true)).isActivationApproved(installWithSplit("({}).missing.split"));
62
+ const { cli, policy, questions } = cliAsking(false);
63
+ const approved = await new CustomerApprovalCheck(cli, customer(true), policy).isActivationApproved(installWithSplit("({}).missing.split"));
61
64
  expect(approved).to.equal(false);
62
65
  expect(questions).to.deep.equal(["Was this change approved by customer?"]);
63
66
  });