@speedkit/cli 4.35.0 → 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 (103) hide show
  1. package/CHANGELOG.md +29 -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/hooks/init/http1-global-dispatcher.d.ts +12 -0
  8. package/dist/hooks/init/http1-global-dispatcher.js +14 -0
  9. package/dist/hooks/init/http1-global-dispatcher.spec.d.ts +1 -0
  10. package/dist/hooks/init/http1-global-dispatcher.spec.js +42 -0
  11. package/dist/services/cli/cli-service.js +42 -2
  12. package/dist/services/cli/cli-service.spec.js +41 -0
  13. package/dist/services/deploy/bulk/bulk-deploy-model.d.ts +46 -0
  14. package/dist/services/deploy/bulk/bulk-deploy-model.js +27 -0
  15. package/dist/services/deploy/bulk/bulk-deploy-service-factory.d.ts +11 -0
  16. package/dist/services/deploy/bulk/bulk-deploy-service-factory.js +24 -0
  17. package/dist/services/deploy/bulk/bulk-deploy-service.d.ts +47 -0
  18. package/dist/services/deploy/bulk/bulk-deploy-service.js +201 -0
  19. package/dist/services/deploy/bulk/bulk-deploy-service.spec.d.ts +1 -0
  20. package/dist/services/deploy/bulk/bulk-deploy-service.spec.js +146 -0
  21. package/dist/services/deploy/bulk/config-deploy-factory.d.ts +13 -0
  22. package/dist/services/deploy/bulk/config-deploy-factory.js +20 -0
  23. package/dist/services/deploy/bulk/index.d.ts +5 -0
  24. package/dist/services/deploy/bulk/index.js +5 -0
  25. package/dist/services/deploy/bulk/touched-configs.d.ts +12 -0
  26. package/dist/services/deploy/bulk/touched-configs.js +54 -0
  27. package/dist/services/deploy/bulk/touched-configs.spec.d.ts +1 -0
  28. package/dist/services/deploy/bulk/touched-configs.spec.js +77 -0
  29. package/dist/services/deploy/checks/activation/customer-approval-check.d.ts +3 -1
  30. package/dist/services/deploy/checks/activation/customer-approval-check.js +5 -2
  31. package/dist/services/deploy/checks/activation/customer-approval-check.spec.js +18 -15
  32. package/dist/services/deploy/checks/post-deploy/deprecated-deployment-detection-check.d.ts +3 -1
  33. package/dist/services/deploy/checks/post-deploy/deprecated-deployment-detection-check.js +6 -3
  34. package/dist/services/deploy/checks/post-deploy/save-files-check.d.ts +3 -1
  35. package/dist/services/deploy/checks/post-deploy/save-files-check.js +5 -2
  36. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.d.ts +3 -1
  37. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +9 -5
  38. package/dist/services/deploy/checks/pre-deploy/split-change-check.d.ts +3 -1
  39. package/dist/services/deploy/checks/pre-deploy/split-change-check.js +8 -5
  40. package/dist/services/deploy/checks/pre-deploy/split-change-check.spec.d.ts +1 -0
  41. package/dist/services/deploy/checks/pre-deploy/split-change-check.spec.js +55 -0
  42. package/dist/services/deploy/context/deploy-context.d.ts +21 -1
  43. package/dist/services/deploy/context/deploy-context.js +30 -1
  44. package/dist/services/deploy/deploy-service-factory.d.ts +13 -1
  45. package/dist/services/deploy/deploy-service-factory.js +36 -15
  46. package/dist/services/deploy/error/deploy-aborted-error.d.ts +12 -0
  47. package/dist/services/deploy/error/deploy-aborted-error.js +16 -0
  48. package/dist/services/deploy/handler/customer-config-handler.d.ts +3 -1
  49. package/dist/services/deploy/handler/customer-config-handler.js +11 -7
  50. package/dist/services/deploy/handler/install-resource-handler.d.ts +3 -1
  51. package/dist/services/deploy/handler/install-resource-handler.js +12 -7
  52. package/dist/services/deploy/handler/install-resource-modifier/deleted-file-check.d.ts +3 -1
  53. package/dist/services/deploy/handler/install-resource-modifier/deleted-file-check.js +5 -2
  54. package/dist/services/deploy/handler/module-handler.d.ts +3 -1
  55. package/dist/services/deploy/handler/module-handler.js +7 -4
  56. package/dist/services/deploy/handler/module-handler.spec.d.ts +1 -0
  57. package/dist/services/deploy/handler/module-handler.spec.js +49 -0
  58. package/dist/services/deploy/handler/server-config-handler.d.ts +3 -1
  59. package/dist/services/deploy/handler/server-config-handler.js +6 -3
  60. package/dist/services/deploy/mr/base-config-reader.d.ts +25 -0
  61. package/dist/services/deploy/mr/base-config-reader.js +52 -0
  62. package/dist/services/deploy/mr/index.d.ts +6 -0
  63. package/dist/services/deploy/mr/index.js +6 -0
  64. package/dist/services/deploy/mr/live-state-check.d.ts +38 -0
  65. package/dist/services/deploy/mr/live-state-check.js +81 -0
  66. package/dist/services/deploy/mr/live-state-check.spec.d.ts +1 -0
  67. package/dist/services/deploy/mr/live-state-check.spec.js +74 -0
  68. package/dist/services/deploy/mr/merge-request-deploy-model.d.ts +59 -0
  69. package/dist/services/deploy/mr/merge-request-deploy-model.js +50 -0
  70. package/dist/services/deploy/mr/merge-request-deploy-service-factory.d.ts +20 -0
  71. package/dist/services/deploy/mr/merge-request-deploy-service-factory.js +46 -0
  72. package/dist/services/deploy/mr/merge-request-deploy-service.d.ts +61 -0
  73. package/dist/services/deploy/mr/merge-request-deploy-service.js +189 -0
  74. package/dist/services/deploy/mr/merge-request-description.d.ts +16 -0
  75. package/dist/services/deploy/mr/merge-request-description.js +27 -0
  76. package/dist/services/deploy/mr/merge-request-description.spec.d.ts +1 -0
  77. package/dist/services/deploy/mr/merge-request-description.spec.js +55 -0
  78. package/dist/services/deploy/policy/auto-deploy-policy.d.ts +43 -0
  79. package/dist/services/deploy/policy/auto-deploy-policy.js +77 -0
  80. package/dist/services/deploy/policy/auto-deploy-policy.spec.d.ts +1 -0
  81. package/dist/services/deploy/policy/auto-deploy-policy.spec.js +83 -0
  82. package/dist/services/deploy/policy/deploy-policy-model.d.ts +61 -0
  83. package/dist/services/deploy/policy/deploy-policy-model.js +41 -0
  84. package/dist/services/deploy/policy/index.d.ts +2 -0
  85. package/dist/services/deploy/policy/index.js +2 -0
  86. package/dist/services/deploy/policy/interactive-deploy-policy.d.ts +15 -0
  87. package/dist/services/deploy/policy/interactive-deploy-policy.js +16 -0
  88. package/dist/services/diff/diff-model.d.ts +5 -1
  89. package/dist/services/diff/diff-model.js +5 -1
  90. package/dist/services/diff/diff-service-factory.js +1 -1
  91. package/dist/services/diff/diff-service.d.ts +2 -1
  92. package/dist/services/diff/diff-service.js +6 -4
  93. package/dist/services/git/git-repository-model.d.ts +13 -0
  94. package/dist/services/git/git-repository-model.js +1 -0
  95. package/dist/services/git/git-repository.d.ts +35 -0
  96. package/dist/services/git/git-repository.js +120 -0
  97. package/dist/services/git/git-repository.spec.d.ts +1 -0
  98. package/dist/services/git/git-repository.spec.js +87 -0
  99. package/dist/services/git/index.d.ts +2 -0
  100. package/dist/services/git/index.js +2 -0
  101. package/dist/services/integration-api/spec/speed-kit-config-file.spec.js +1 -1
  102. package/oclif.manifest.json +107 -1
  103. package/package.json +4 -3
@@ -0,0 +1,83 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import { ApprovalAnswer, AutoDeployPolicy } from "./auto-deploy-policy.js";
4
+ import { DeployQuestion } from "./deploy-policy-model.js";
5
+ const REVIEWED = [
6
+ DeployQuestion.UpdateFile,
7
+ DeployQuestion.UpdateOrigins,
8
+ DeployQuestion.DeleteInstallResourceKey,
9
+ DeployQuestion.UploadInstallResource,
10
+ DeployQuestion.UploadModules,
11
+ DeployQuestion.UploadServerConfig,
12
+ ];
13
+ /** A person who answers every question the same way, and records them. */
14
+ function personAnswering(answer) {
15
+ const questions = [];
16
+ const cli = {
17
+ confirm: async (message) => {
18
+ questions.push(message);
19
+ return answer;
20
+ },
21
+ };
22
+ return { cli, questions };
23
+ }
24
+ const NOT_IN_A_MERGE_REQUEST = [
25
+ DeployQuestion.CreateConfig,
26
+ DeployQuestion.SplitChange,
27
+ DeployQuestion.DeviceDetectionChange,
28
+ DeployQuestion.SaveLocalFiles,
29
+ DeployQuestion.DeleteDeprecatedJob,
30
+ DeployQuestion.DeleteDeprecatedModule,
31
+ ];
32
+ function policy(approval) {
33
+ return new AutoDeployPolicy("!42 a reviewed change", approval);
34
+ }
35
+ describe("AutoDeployPolicy", () => {
36
+ it("confirms the change the merge request carries", async () => {
37
+ for (const question of REVIEWED) {
38
+ expect(await policy(ApprovalAnswer.Approved).confirm(question)).to.equal(true, question);
39
+ }
40
+ });
41
+ it("declines every decision a merge request does not carry", async () => {
42
+ for (const question of NOT_IN_A_MERGE_REQUEST) {
43
+ expect(await policy(ApprovalAnswer.Approved).confirm(question)).to.equal(false, question);
44
+ }
45
+ });
46
+ it("activates a reviewed change, whatever the approval answer is", async () => {
47
+ for (const approval of Object.values(ApprovalAnswer)) {
48
+ expect(await policy(approval).confirm(DeployQuestion.Activate)).to.equal(true, approval);
49
+ }
50
+ });
51
+ it("approves for the customers that approve every rollout themselves", async () => {
52
+ const approved = policy(ApprovalAnswer.Approved);
53
+ expect(await approved.confirm(DeployQuestion.CustomerApproval)).to.equal(true);
54
+ expect(await approved.confirm(DeployQuestion.ActivateAsTesting)).to.equal(false);
55
+ });
56
+ it("holds such a customer back to a testing installation", async () => {
57
+ const testing = policy(ApprovalAnswer.Testing);
58
+ expect(await testing.confirm(DeployQuestion.CustomerApproval)).to.equal(false);
59
+ expect(await testing.confirm(DeployQuestion.ActivateAsTesting)).to.equal(true);
60
+ });
61
+ it("activates nothing of such a customer while the approval is missing", async () => {
62
+ const skip = policy(ApprovalAnswer.Skip);
63
+ expect(await skip.confirm(DeployQuestion.CustomerApproval)).to.equal(false);
64
+ expect(await skip.confirm(DeployQuestion.ActivateAsTesting)).to.equal(false);
65
+ });
66
+ it("leaves the upload to the person who watches the deploy", async () => {
67
+ const { cli, questions } = personAnswering(false);
68
+ const policy = new AutoDeployPolicy("a change", ApprovalAnswer.Approved, cli);
69
+ expect(await policy.confirm(DeployQuestion.UploadInstallResource, "InstallResource will be created now. Proceed?")).to.equal(false);
70
+ expect(questions).to.deep.equal([
71
+ "InstallResource will be created now. Proceed?",
72
+ ]);
73
+ });
74
+ it("asks nobody about a file, even with a person watching", async () => {
75
+ const { cli, questions } = personAnswering(false);
76
+ const policy = new AutoDeployPolicy("a change", ApprovalAnswer.Approved, cli);
77
+ expect(await policy.confirm(DeployQuestion.UpdateFile, "Update config?")).to.equal(true);
78
+ expect(questions).to.deep.equal([]);
79
+ });
80
+ it("describes the installation with the merge request", async () => {
81
+ expect(await policy(ApprovalAnswer.Approved).text(DeployQuestion.Description)).to.equal("!42 a reviewed change");
82
+ });
83
+ });
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Every decision `sk deploy` asks a person for.
3
+ *
4
+ * A caller that runs without a person answers by key, so the key is part of
5
+ * the contract while the message text is not.
6
+ */
7
+ export declare enum DeployQuestion {
8
+ /** Take over a changed or new file into the installation. */
9
+ UpdateFile = "updateFile",
10
+ /** Take over the local origins into the installation. */
11
+ UpdateOrigins = "updateOrigins",
12
+ /** Create a configuration that does not exist in the app yet. */
13
+ CreateConfig = "createConfig",
14
+ /** Drop a resource whose local file is gone. */
15
+ DeleteInstallResourceKey = "deleteInstallResourceKey",
16
+ /** Upload the new installation. */
17
+ UploadInstallResource = "uploadInstallResource",
18
+ /** Upload the changed modules. */
19
+ UploadModules = "uploadModules",
20
+ /** Upload the changed server config. */
21
+ UploadServerConfig = "uploadServerConfig",
22
+ /** The description of the new installation. */
23
+ Description = "description",
24
+ /** The customer approved the rollout of this installation. */
25
+ CustomerApproval = "customerApproval",
26
+ /** Activate the new installation for real users. */
27
+ Activate = "activate",
28
+ /** Activate the new installation for the debug parameter only. */
29
+ ActivateAsTesting = "activateAsTesting",
30
+ /** Deploy although the split changes. */
31
+ SplitChange = "splitChange",
32
+ /** Deploy although the device detection changes. */
33
+ DeviceDetectionChange = "deviceDetectionChange",
34
+ /** Write the content taken over from the installation to the local files. */
35
+ SaveLocalFiles = "saveLocalFiles",
36
+ /** Delete the job of the deprecated deployment detection. */
37
+ DeleteDeprecatedJob = "deleteDeprecatedJob",
38
+ /** Delete the module of the deprecated deployment detection. */
39
+ DeleteDeprecatedModule = "deleteDeprecatedModule"
40
+ }
41
+ export interface DeployPolicyInterface {
42
+ /**
43
+ * The answer to a yes/no question.
44
+ *
45
+ * @param question the decision behind the message
46
+ * @param message the wording for the person at the keyboard
47
+ * @param defaultAnswer the answer a person confirms with Enter
48
+ */
49
+ confirm(question: DeployQuestion, message: string, defaultAnswer?: boolean): Promise<boolean>;
50
+ /**
51
+ * The answer to an open question.
52
+ *
53
+ * @param question the decision behind the message
54
+ * @param message the wording for the person at the keyboard
55
+ * @param options the default answer and the validator of the input
56
+ */
57
+ text(question: DeployQuestion, message: string, options?: {
58
+ defaultAnswer?: string;
59
+ validator?: (input: string) => string;
60
+ }): Promise<string>;
61
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Every decision `sk deploy` asks a person for.
3
+ *
4
+ * A caller that runs without a person answers by key, so the key is part of
5
+ * the contract while the message text is not.
6
+ */
7
+ export var DeployQuestion;
8
+ (function (DeployQuestion) {
9
+ /** Take over a changed or new file into the installation. */
10
+ DeployQuestion["UpdateFile"] = "updateFile";
11
+ /** Take over the local origins into the installation. */
12
+ DeployQuestion["UpdateOrigins"] = "updateOrigins";
13
+ /** Create a configuration that does not exist in the app yet. */
14
+ DeployQuestion["CreateConfig"] = "createConfig";
15
+ /** Drop a resource whose local file is gone. */
16
+ DeployQuestion["DeleteInstallResourceKey"] = "deleteInstallResourceKey";
17
+ /** Upload the new installation. */
18
+ DeployQuestion["UploadInstallResource"] = "uploadInstallResource";
19
+ /** Upload the changed modules. */
20
+ DeployQuestion["UploadModules"] = "uploadModules";
21
+ /** Upload the changed server config. */
22
+ DeployQuestion["UploadServerConfig"] = "uploadServerConfig";
23
+ /** The description of the new installation. */
24
+ DeployQuestion["Description"] = "description";
25
+ /** The customer approved the rollout of this installation. */
26
+ DeployQuestion["CustomerApproval"] = "customerApproval";
27
+ /** Activate the new installation for real users. */
28
+ DeployQuestion["Activate"] = "activate";
29
+ /** Activate the new installation for the debug parameter only. */
30
+ DeployQuestion["ActivateAsTesting"] = "activateAsTesting";
31
+ /** Deploy although the split changes. */
32
+ DeployQuestion["SplitChange"] = "splitChange";
33
+ /** Deploy although the device detection changes. */
34
+ DeployQuestion["DeviceDetectionChange"] = "deviceDetectionChange";
35
+ /** Write the content taken over from the installation to the local files. */
36
+ DeployQuestion["SaveLocalFiles"] = "saveLocalFiles";
37
+ /** Delete the job of the deprecated deployment detection. */
38
+ DeployQuestion["DeleteDeprecatedJob"] = "deleteDeprecatedJob";
39
+ /** Delete the module of the deprecated deployment detection. */
40
+ DeployQuestion["DeleteDeprecatedModule"] = "deleteDeprecatedModule";
41
+ })(DeployQuestion || (DeployQuestion = {}));
@@ -0,0 +1,2 @@
1
+ export * from "./deploy-policy-model.js";
2
+ export * from "./interactive-deploy-policy.js";
@@ -0,0 +1,2 @@
1
+ export * from "./deploy-policy-model.js";
2
+ export * from "./interactive-deploy-policy.js";
@@ -0,0 +1,15 @@
1
+ import { CliServiceInterface } from "../../cli/index.js";
2
+ import { DeployPolicyInterface, DeployQuestion } from "./deploy-policy-model.js";
3
+ /**
4
+ * The policy of a `sk deploy` that a person drives: every question goes to the
5
+ * keyboard, and the key of the question does not matter.
6
+ */
7
+ export declare class InteractiveDeployPolicy implements DeployPolicyInterface {
8
+ private readonly cli;
9
+ constructor(cli: CliServiceInterface);
10
+ confirm(question: DeployQuestion, message: string, defaultAnswer?: boolean): Promise<boolean>;
11
+ text(question: DeployQuestion, message: string, options?: {
12
+ defaultAnswer?: string;
13
+ validator?: (input: string) => string;
14
+ }): Promise<string>;
15
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The policy of a `sk deploy` that a person drives: every question goes to the
3
+ * keyboard, and the key of the question does not matter.
4
+ */
5
+ export class InteractiveDeployPolicy {
6
+ cli;
7
+ constructor(cli) {
8
+ this.cli = cli;
9
+ }
10
+ async confirm(question, message, defaultAnswer = true) {
11
+ return this.cli.confirm(message, defaultAnswer);
12
+ }
13
+ async text(question, message, options) {
14
+ return this.cli.prompt(message, options);
15
+ }
16
+ }
@@ -1,5 +1,9 @@
1
1
  export declare class DiffContext {
2
2
  readonly executablePath: string;
3
3
  readonly executableTemplate: string;
4
- constructor(executablePath: string, executableTemplate: string);
4
+ /** A deploy that nobody drives never opens a diff tool. */
5
+ readonly opensDiffTool: boolean;
6
+ constructor(executablePath: string, executableTemplate: string,
7
+ /** A deploy that nobody drives never opens a diff tool. */
8
+ opensDiffTool?: boolean);
5
9
  }
@@ -1,8 +1,12 @@
1
1
  export class DiffContext {
2
2
  executablePath;
3
3
  executableTemplate;
4
- constructor(executablePath, executableTemplate) {
4
+ opensDiffTool;
5
+ constructor(executablePath, executableTemplate,
6
+ /** A deploy that nobody drives never opens a diff tool. */
7
+ opensDiffTool = true) {
5
8
  this.executablePath = executablePath;
6
9
  this.executableTemplate = executableTemplate;
10
+ this.opensDiffTool = opensDiffTool;
7
11
  }
8
12
  }
@@ -9,7 +9,7 @@ export class DiffServiceFactory {
9
9
  getService() {
10
10
  if (!(this.service instanceof DiffService)) {
11
11
  const cliService = new CliServiceFactory().getService();
12
- this.service = new DiffService(cliService, this.context.executablePath, this.context.executableTemplate);
12
+ this.service = new DiffService(cliService, this.context.executablePath, this.context.executableTemplate, this.context.opensDiffTool);
13
13
  }
14
14
  return this.service;
15
15
  }
@@ -3,8 +3,9 @@ export declare class DiffService {
3
3
  private cli;
4
4
  private diffExecutablePath?;
5
5
  private diffExecutableTemplate?;
6
+ private opensDiffTool;
6
7
  private tempPath;
7
- constructor(cli: CliService, diffExecutablePath?: string, diffExecutableTemplate?: string);
8
+ constructor(cli: CliService, diffExecutablePath?: string, diffExecutableTemplate?: string, opensDiffTool?: boolean);
8
9
  diff(localPath: string, fileName: string, temporaryContent: string): Promise<boolean>;
9
10
  diffContent(fileName: string, firstContent: string, secondContent: string, diffSuffix?: string[]): Promise<{
10
11
  updatedFile: string;
@@ -4,15 +4,18 @@ import { execSync } from "node:child_process";
4
4
  import { createTemporalDirectory } from "../../helpers/file-helper.js";
5
5
  import { unescapeSpaces } from "../../helpers/normalize.js";
6
6
  import { DEFAULT_INTELLIJ_IDEA_PATH_MACOS } from "../../models/os.js";
7
+ import ApplicationError from "../error-handling/error/application-error.js";
7
8
  export class DiffService {
8
9
  cli;
9
10
  diffExecutablePath;
10
11
  diffExecutableTemplate;
12
+ opensDiffTool;
11
13
  tempPath = null;
12
- constructor(cli, diffExecutablePath, diffExecutableTemplate) {
14
+ constructor(cli, diffExecutablePath, diffExecutableTemplate, opensDiffTool = true) {
13
15
  this.cli = cli;
14
16
  this.diffExecutablePath = diffExecutablePath;
15
17
  this.diffExecutableTemplate = diffExecutableTemplate;
18
+ this.opensDiffTool = opensDiffTool;
16
19
  }
17
20
  async diff(localPath, fileName, temporaryContent) {
18
21
  const temporaryFilePath = await this.writeContentToTemporalFile(fileName, temporaryContent);
@@ -36,8 +39,7 @@ export class DiffService {
36
39
  this.tempPath = await createTemporalDirectory();
37
40
  }
38
41
  if (!this.tempPath) {
39
- this.cli.writeError("No temporary Directory found and unable to create a new temporary Directory");
40
- this.cli.exit(1);
42
+ throw new ApplicationError("No temporary directory found and unable to create a new temporary directory");
41
43
  }
42
44
  return this.tempPath;
43
45
  }
@@ -94,7 +96,7 @@ export class DiffService {
94
96
  if (this.isContentEqual(firstContent, secondContent)) {
95
97
  return true;
96
98
  }
97
- if (skipDiffExecution) {
99
+ if (skipDiffExecution || !this.opensDiffTool) {
98
100
  return false;
99
101
  }
100
102
  // try custom diff tool
@@ -0,0 +1,13 @@
1
+ /** Runs a git command in the repository and returns its output. */
2
+ export type GitRunner = (args: string[]) => string;
3
+ /** A merge request that reached the default branch, as git recorded it. */
4
+ export interface MergedRequest {
5
+ /** The number the merge request has in GitLab. */
6
+ iid: number;
7
+ /** The merge commit. */
8
+ sha: string;
9
+ /** The state of the repository before the merge request. */
10
+ baseSha: string;
11
+ /** The first line of the merge commit body, which GitLab fills with the title. */
12
+ title: string;
13
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
1
+ import { GitRunner, MergedRequest } from "./git-repository-model.js";
2
+ /**
3
+ * The repository that holds the customer configurations.
4
+ *
5
+ * Every answer comes from the local clone. A merge request is found through
6
+ * the reference GitLab writes into the merge commit, so the lookup needs no
7
+ * token and no network.
8
+ */
9
+ export declare class GitRepository {
10
+ readonly root: string;
11
+ private readonly run;
12
+ constructor(root: string, run: GitRunner);
13
+ /** The repository that contains `somePath`. */
14
+ static at(somePath: string, run?: GitRunner): GitRepository;
15
+ /** The path of `somePath` as git spells it. */
16
+ relative(somePath: string): string;
17
+ /**
18
+ * The merge request with this number, as it was merged.
19
+ *
20
+ * @throws ApplicationError when the history holds no or several such merges
21
+ */
22
+ mergeRequest(iid: number): MergedRequest;
23
+ /** The paths the merge request changed. */
24
+ changedPaths(request: MergedRequest): string[];
25
+ /** The paths below `directory` that the commit holds. */
26
+ pathsAt(sha: string, directory: string): string[];
27
+ /** The content of one file in the commit. */
28
+ contentAt(sha: string, filePath: string): string;
29
+ /** The paths below `directory` that changed between the commit and HEAD. */
30
+ changesSince(sha: string, directory: string): string[];
31
+ /** The paths below `directory` that the working tree changed. */
32
+ uncommittedChanges(directory: string): string[];
33
+ /** The title GitLab wrote into the merge commit. */
34
+ private titleOf;
35
+ }
@@ -0,0 +1,120 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import path from "node:path";
3
+ import ApplicationError from "../error-handling/error/application-error.js";
4
+ /**
5
+ * The repository that holds the customer configurations.
6
+ *
7
+ * Every answer comes from the local clone. A merge request is found through
8
+ * the reference GitLab writes into the merge commit, so the lookup needs no
9
+ * token and no network.
10
+ */
11
+ export class GitRepository {
12
+ root;
13
+ run;
14
+ constructor(root, run) {
15
+ this.root = root;
16
+ this.run = run;
17
+ }
18
+ /** The repository that contains `somePath`. */
19
+ static at(somePath, run) {
20
+ const probe = run ?? runGit(somePath);
21
+ const root = probe(["rev-parse", "--show-toplevel"]).trim();
22
+ return new GitRepository(root, run ?? runGit(root));
23
+ }
24
+ /** The path of `somePath` as git spells it. */
25
+ relative(somePath) {
26
+ return path
27
+ .relative(this.root, path.resolve(somePath))
28
+ .replaceAll("\\", "/");
29
+ }
30
+ /**
31
+ * The merge request with this number, as it was merged.
32
+ *
33
+ * @throws ApplicationError when the history holds no or several such merges
34
+ */
35
+ mergeRequest(iid) {
36
+ const commits = this.run([
37
+ "log",
38
+ "--extended-regexp",
39
+ `--grep=^See merge request [^ ]+!${iid}$`,
40
+ "--format=%H",
41
+ "HEAD",
42
+ ])
43
+ .split("\n")
44
+ .filter((line) => line.length > 0);
45
+ if (commits.length === 0) {
46
+ throw new ApplicationError(`No merge of !${iid} found in this history`, [
47
+ "Merge requests are found through the reference in the merge commit",
48
+ `Pull the default branch of ${this.root} and try again`,
49
+ ]);
50
+ }
51
+ if (commits.length > 1) {
52
+ throw new ApplicationError(`The history holds ${commits.length} merges of !${iid}`);
53
+ }
54
+ const sha = commits[0];
55
+ const parents = this.run(["log", "-1", "--format=%P", sha])
56
+ .trim()
57
+ .split(" ");
58
+ if (parents.length < 2) {
59
+ throw new ApplicationError(`The merge of !${iid} is no merge commit, so its base is unknown`);
60
+ }
61
+ return {
62
+ iid,
63
+ sha,
64
+ baseSha: parents[0],
65
+ title: this.titleOf(sha, iid),
66
+ };
67
+ }
68
+ /** The paths the merge request changed. */
69
+ changedPaths(request) {
70
+ return this.run(["diff", "--name-only", request.baseSha, request.sha])
71
+ .split("\n")
72
+ .filter((line) => line.length > 0);
73
+ }
74
+ /** The paths below `directory` that the commit holds. */
75
+ pathsAt(sha, directory) {
76
+ return this.run(["ls-tree", "-r", "--name-only", sha, "--", directory])
77
+ .split("\n")
78
+ .filter((line) => line.length > 0);
79
+ }
80
+ /** The content of one file in the commit. */
81
+ contentAt(sha, filePath) {
82
+ return this.run(["show", `${sha}:${filePath}`]);
83
+ }
84
+ /** The paths below `directory` that changed between the commit and HEAD. */
85
+ changesSince(sha, directory) {
86
+ return this.run(["diff", "--name-only", sha, "HEAD", "--", directory])
87
+ .split("\n")
88
+ .filter((line) => line.length > 0);
89
+ }
90
+ /** The paths below `directory` that the working tree changed. */
91
+ uncommittedChanges(directory) {
92
+ return this.run(["status", "--porcelain", "--", directory])
93
+ .split("\n")
94
+ .filter((line) => line.length > 0)
95
+ .map((line) => line.slice(3));
96
+ }
97
+ /** The title GitLab wrote into the merge commit. */
98
+ titleOf(sha, iid) {
99
+ const body = this.run(["log", "-1", "--format=%b", sha])
100
+ .split("\n")
101
+ .map((line) => line.trim())
102
+ .filter((line) => line.length > 0 && !line.startsWith("See merge request"));
103
+ return body[0] ?? `!${iid}`;
104
+ }
105
+ }
106
+ /** Runs git in a directory, and reports a failure with the output of git. */
107
+ function runGit(cwd) {
108
+ return (args) => {
109
+ try {
110
+ return execFileSync("git", args, {
111
+ cwd,
112
+ encoding: "utf8",
113
+ maxBuffer: 32 * 1024 * 1024,
114
+ });
115
+ }
116
+ catch (error) {
117
+ throw new ApplicationError(`git ${args[0]} failed: ${error.stderr?.toString().trim() || error.message}`);
118
+ }
119
+ };
120
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,87 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import { GitRepository } from "./git-repository.js";
4
+ const MERGE_SHA = "3aaa523e92e9a8ccba03ded37bf04c8bdecd3b0f";
5
+ const BASE_SHA = "83d9d5805a760f059245030f9282ec7354f382f6";
6
+ /** A git that answers from a table, and records what it was asked. */
7
+ function gitAnswering(answers) {
8
+ const calls = [];
9
+ const run = (args) => {
10
+ calls.push(args);
11
+ for (const [command, output] of Object.entries(answers)) {
12
+ if (args.join(" ").includes(command)) {
13
+ return output;
14
+ }
15
+ }
16
+ return "";
17
+ };
18
+ return { run, calls };
19
+ }
20
+ function repositoryOf(answers) {
21
+ const { run, calls } = gitAnswering(answers);
22
+ return { repository: new GitRepository("/repo", run), calls };
23
+ }
24
+ describe("GitRepository", () => {
25
+ it("finds a merge request through the reference in the merge commit", () => {
26
+ const { repository, calls } = repositoryOf({
27
+ "--format=%H": `${MERGE_SHA}\n`,
28
+ "--format=%P": `${BASE_SHA} 7c1b0d1\n`,
29
+ "--format=%b": "chore(gohaus): update from production\n\nSee merge request team-pi/sk-onboarding!8009\n",
30
+ });
31
+ const request = repository.mergeRequest(8009);
32
+ expect(request).to.deep.equal({
33
+ iid: 8009,
34
+ sha: MERGE_SHA,
35
+ baseSha: BASE_SHA,
36
+ title: "chore(gohaus): update from production",
37
+ });
38
+ expect(calls[0]).to.include("--grep=^See merge request [^ ]+!8009$");
39
+ });
40
+ it("reports a merge request that this history does not hold", () => {
41
+ const { repository } = repositoryOf({});
42
+ expect(() => repository.mergeRequest(8009)).to.throw("No merge of !8009");
43
+ });
44
+ it("reports a number that matches several merges", () => {
45
+ const { repository } = repositoryOf({
46
+ "--format=%H": `${MERGE_SHA}\n7c1b0d1\n`,
47
+ });
48
+ expect(() => repository.mergeRequest(8009)).to.throw("2 merges of !8009");
49
+ });
50
+ it("reports a reference that no merge commit carries", () => {
51
+ const { repository } = repositoryOf({
52
+ "--format=%H": `${MERGE_SHA}\n`,
53
+ "--format=%P": `${BASE_SHA}\n`,
54
+ });
55
+ expect(() => repository.mergeRequest(8009)).to.throw("no merge commit");
56
+ });
57
+ it("falls back to the number while the merge commit carries no title", () => {
58
+ const { repository } = repositoryOf({
59
+ "--format=%H": `${MERGE_SHA}\n`,
60
+ "--format=%P": `${BASE_SHA} 7c1b0d1\n`,
61
+ "--format=%b": "See merge request team-pi/sk-onboarding!8009\n",
62
+ });
63
+ expect(repository.mergeRequest(8009).title).to.equal("!8009");
64
+ });
65
+ it("reads a list of paths as lines", () => {
66
+ const { repository } = repositoryOf({
67
+ "ls-tree": "customers/gohaus/config_SpeedKit.js\ncustomers/gohaus/config_customer.json\n",
68
+ });
69
+ expect(repository.pathsAt(BASE_SHA, "customers/gohaus")).to.deep.equal([
70
+ "customers/gohaus/config_SpeedKit.js",
71
+ "customers/gohaus/config_customer.json",
72
+ ]);
73
+ });
74
+ it("reads the changed paths of the working tree without their status", () => {
75
+ const { repository } = repositoryOf({
76
+ status: " M customers/gohaus/config_SpeedKit.js\n?? customers/gohaus/new.js\n",
77
+ });
78
+ expect(repository.uncommittedChanges("customers/gohaus")).to.deep.equal([
79
+ "customers/gohaus/config_SpeedKit.js",
80
+ "customers/gohaus/new.js",
81
+ ]);
82
+ });
83
+ it("spells a path as git does", () => {
84
+ const { repository } = repositoryOf({});
85
+ expect(repository.relative("/repo/customers/gohaus")).to.equal("customers/gohaus");
86
+ });
87
+ });
@@ -0,0 +1,2 @@
1
+ export * from "./git-repository-model.js";
2
+ export * from "./git-repository.js";
@@ -0,0 +1,2 @@
1
+ export * from "./git-repository-model.js";
2
+ export * from "./git-repository.js";
@@ -1,6 +1,6 @@
1
1
  import { expect } from "chai";
2
2
  import { describe, it } from "mocha";
3
- import { SpeedKitConfigFile, VirtualSpeedKitConfigFile, } from "../index.js";
3
+ import { SpeedKitConfigFile, VirtualSpeedKitConfigFile } from "../index.js";
4
4
  import { INTEGRATION_FILES } from "../../../models/files.js";
5
5
  function configFile(source) {
6
6
  return new SpeedKitConfigFile(INTEGRATION_FILES.CONFIG.SPEED_KIT, "config_SpeedKit.js", source, "config");