@catladder/cli 0.0.0-refactor-cli-add-improved-git-utilities-fba867df → 0.0.0-refactor-remove-rails-hosts-afe04cf0

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 (119) hide show
  1. package/dist/bundles/catci/index.js +43322 -44345
  2. package/dist/bundles/catenv/index.js +27561 -43984
  3. package/dist/bundles/cli/index.js +81346 -97135
  4. package/dist/cli/src/apps/catenv/catenv.js +1 -1
  5. package/dist/cli/src/apps/catenv/catenv.js.map +1 -1
  6. package/dist/cli/src/apps/cli/commands/project/setup/setupAccessTokens.d.ts +1 -2
  7. package/dist/cli/src/apps/cli/commands/project/setup/setupAccessTokens.js +1 -3
  8. package/dist/cli/src/apps/cli/commands/project/setup/setupAccessTokens.js.map +1 -1
  9. package/dist/cli/src/apps/cli/commands/project/setup/setupCloudRun.js +2 -4
  10. package/dist/cli/src/apps/cli/commands/project/setup/setupCloudRun.js.map +1 -1
  11. package/dist/cli/src/apps/cli/commands/project/setup/setupKubernetes.d.ts +1 -2
  12. package/dist/cli/src/apps/cli/commands/project/setup/setupKubernetes.js +9 -23
  13. package/dist/cli/src/apps/cli/commands/project/setup/setupKubernetes.js.map +1 -1
  14. package/dist/cli/src/apps/cli/commands/project/setup/setupTopic.js +4 -6
  15. package/dist/cli/src/apps/cli/commands/project/setup/setupTopic.js.map +1 -1
  16. package/dist/cli/src/apps/cli/utils/getGoogleAuthUserNumber.d.ts +1 -1
  17. package/dist/cli/src/apps/cli/utils/getGoogleAuthUserNumber.js +4 -3
  18. package/dist/cli/src/apps/cli/utils/getGoogleAuthUserNumber.js.map +1 -1
  19. package/dist/cli/src/packageInfos.js +0 -1
  20. package/dist/cli/src/packageInfos.js.map +1 -1
  21. package/dist/cli/src/utils/gitlab.d.ts +7 -10
  22. package/dist/cli/src/utils/gitlab.js +45 -67
  23. package/dist/cli/src/utils/gitlab.js.map +1 -1
  24. package/dist/cli/src/utils/preferences/index.d.ts +3 -0
  25. package/dist/cli/src/utils/preferences/index.js +35 -0
  26. package/dist/cli/src/utils/preferences/index.js.map +1 -0
  27. package/dist/pipeline/src/build/cache/createJobCache.js +2 -2
  28. package/dist/pipeline/src/build/cache/createJobCache.js.map +1 -1
  29. package/dist/pipeline/src/build/sbom.js +2 -2
  30. package/dist/pipeline/src/build/sbom.js.map +1 -1
  31. package/dist/pipeline/src/build/types.d.ts +3 -3
  32. package/dist/pipeline/src/config/readConfig.js +0 -1
  33. package/dist/pipeline/src/config/readConfig.js.map +1 -1
  34. package/dist/pipeline/src/context/createComponentContext.js +1 -1
  35. package/dist/pipeline/src/context/createComponentContext.js.map +1 -1
  36. package/dist/pipeline/src/context/getEnvironmentVariables.d.ts +1 -1
  37. package/dist/pipeline/src/context/getEnvironmentVariables.js +1 -5
  38. package/dist/pipeline/src/context/getEnvironmentVariables.js.map +1 -1
  39. package/dist/pipeline/src/deploy/base/deploy.js +3 -3
  40. package/dist/pipeline/src/deploy/base/deploy.js.map +1 -1
  41. package/dist/pipeline/src/deploy/cloudRun/createJobs/cloudRunServices.js +1 -1
  42. package/dist/pipeline/src/deploy/cloudRun/createJobs/cloudRunServices.js.map +1 -1
  43. package/dist/pipeline/src/deploy/types/index.d.ts +3 -3
  44. package/dist/pipeline/src/pipeline/createMainPipeline.js +13 -7
  45. package/dist/pipeline/src/pipeline/createMainPipeline.js.map +1 -1
  46. package/dist/pipeline/src/pipeline/generatePipelineFiles.d.ts +7 -40
  47. package/dist/pipeline/src/pipeline/generatePipelineFiles.js +69 -42
  48. package/dist/pipeline/src/pipeline/generatePipelineFiles.js.map +1 -1
  49. package/dist/pipeline/src/pipeline/gitlab/createGitlabJobs.d.ts +6 -4
  50. package/dist/pipeline/src/pipeline/gitlab/gitlabReleaseJobs.d.ts +0 -2
  51. package/dist/pipeline/src/pipeline/gitlab/gitlabReleaseJobs.js +2 -4
  52. package/dist/pipeline/src/pipeline/gitlab/gitlabReleaseJobs.js.map +1 -1
  53. package/dist/pipeline/src/pipeline/index.d.ts +0 -1
  54. package/dist/pipeline/src/pipeline/index.js +0 -1
  55. package/dist/pipeline/src/pipeline/index.js.map +1 -1
  56. package/dist/pipeline/src/types/config.d.ts +3 -3
  57. package/dist/pipeline/src/types/gitlab-types.d.ts +2 -1
  58. package/dist/pipeline/src/types/pipeline.d.ts +0 -1
  59. package/dist/tsconfig.tsbuildinfo +1 -1
  60. package/package.json +14 -13
  61. package/src/apps/catenv/catenv.ts +1 -1
  62. package/src/apps/cli/commands/project/setup/setupAccessTokens.ts +1 -11
  63. package/src/apps/cli/commands/project/setup/setupCloudRun.ts +2 -4
  64. package/src/apps/cli/commands/project/setup/setupKubernetes.ts +27 -30
  65. package/src/apps/cli/commands/project/setup/setupTopic.ts +11 -6
  66. package/src/apps/cli/utils/getGoogleAuthUserNumber.ts +4 -6
  67. package/src/packageInfos.ts +0 -1
  68. package/src/utils/gitlab.ts +101 -99
  69. package/src/utils/preferences/index.ts +36 -0
  70. package/dist/cli/src/git/getGitConfigVars.d.ts +0 -4
  71. package/dist/cli/src/git/getGitConfigVars.js +0 -14
  72. package/dist/cli/src/git/getGitConfigVars.js.map +0 -1
  73. package/dist/cli/src/git/getGitRemoteHostAndPath.d.ts +0 -9
  74. package/dist/cli/src/git/getGitRemoteHostAndPath.js +0 -23
  75. package/dist/cli/src/git/getGitRemoteHostAndPath.js.map +0 -1
  76. package/dist/cli/src/git/getProjectRootPath.d.ts +0 -1
  77. package/dist/cli/src/git/getProjectRootPath.js +0 -10
  78. package/dist/cli/src/git/getProjectRootPath.js.map +0 -1
  79. package/dist/cli/src/git/gitConfig.d.ts +0 -23
  80. package/dist/cli/src/git/gitConfig.js +0 -60
  81. package/dist/cli/src/git/gitConfig.js.map +0 -1
  82. package/dist/cli/src/git/index.d.ts +0 -4
  83. package/dist/cli/src/git/index.js +0 -21
  84. package/dist/cli/src/git/index.js.map +0 -1
  85. package/dist/cli/src/gitlab/apiClient.d.ts +0 -5
  86. package/dist/cli/src/gitlab/apiClient.js +0 -21
  87. package/dist/cli/src/gitlab/apiClient.js.map +0 -1
  88. package/dist/cli/src/gitlab/getGitlabProjectId.d.ts +0 -4
  89. package/dist/cli/src/gitlab/getGitlabProjectId.js +0 -29
  90. package/dist/cli/src/gitlab/getGitlabProjectId.js.map +0 -1
  91. package/dist/cli/src/gitlab/gitlabToken.d.ts +0 -10
  92. package/dist/cli/src/gitlab/gitlabToken.js +0 -42
  93. package/dist/cli/src/gitlab/gitlabToken.js.map +0 -1
  94. package/dist/cli/src/gitlab/index.d.ts +0 -3
  95. package/dist/cli/src/gitlab/index.js +0 -20
  96. package/dist/cli/src/gitlab/index.js.map +0 -1
  97. package/dist/cli/src/utils/preferences.d.ts +0 -15
  98. package/dist/cli/src/utils/preferences.js +0 -61
  99. package/dist/cli/src/utils/preferences.js.map +0 -1
  100. package/dist/cli/src/utils/projectPreferences.d.ts +0 -5
  101. package/dist/cli/src/utils/projectPreferences.js +0 -54
  102. package/dist/cli/src/utils/projectPreferences.js.map +0 -1
  103. package/dist/pipeline/src/pipeline/createChildPipeline.d.ts +0 -3
  104. package/dist/pipeline/src/pipeline/createChildPipeline.js +0 -30
  105. package/dist/pipeline/src/pipeline/createChildPipeline.js.map +0 -1
  106. package/dist/pipeline/src/pipeline/gitlab/getPipelineTriggerForGitlabChildPipeline.d.ts +0 -1
  107. package/dist/pipeline/src/pipeline/gitlab/getPipelineTriggerForGitlabChildPipeline.js +0 -32
  108. package/dist/pipeline/src/pipeline/gitlab/getPipelineTriggerForGitlabChildPipeline.js.map +0 -1
  109. package/src/git/getGitConfigVars.ts +0 -11
  110. package/src/git/getGitRemoteHostAndPath.ts +0 -28
  111. package/src/git/getProjectRootPath.ts +0 -6
  112. package/src/git/gitConfig.ts +0 -86
  113. package/src/git/index.ts +0 -4
  114. package/src/gitlab/apiClient.ts +0 -23
  115. package/src/gitlab/getGitlabProjectId.ts +0 -32
  116. package/src/gitlab/gitlabToken.ts +0 -52
  117. package/src/gitlab/index.ts +0 -3
  118. package/src/utils/preferences.ts +0 -85
  119. package/src/utils/projectPreferences.ts +0 -78
package/package.json CHANGED
@@ -53,7 +53,7 @@
53
53
  }
54
54
  ],
55
55
  "license": "MIT",
56
- "version": "0.0.0-refactor-cli-add-improved-git-utilities-fba867df",
56
+ "version": "0.0.0-refactor-remove-rails-hosts-afe04cf0",
57
57
  "scripts": {
58
58
  "lint": "eslint \"src/**/*.ts\"",
59
59
  "lint:fix": "eslint \"src/**/*.ts\" --fix",
@@ -74,41 +74,42 @@
74
74
  "node": ">=18.0.0"
75
75
  },
76
76
  "devDependencies": {
77
- "@gitbeaker/rest": "^40.1.3",
77
+ "@gitbeaker/rest": "^39.28.0",
78
78
  "@kubernetes/client-node": "^0.16.2",
79
79
  "@tsconfig/node14": "^1.0.1",
80
- "@types/common-tags": "^1.8.0",
80
+ "@types/common-tags": "^1.8.4",
81
81
  "@types/fs-extra": "^5.0.4",
82
- "@types/lodash": "^4.14.185",
82
+ "@types/lodash": "^4.17.7",
83
83
  "@types/memoizee": "^0.4.2",
84
- "@types/node-fetch": "^2.1.4",
85
- "@types/request": "^2.48.1",
84
+ "@types/node-fetch": "^2.6.11",
85
+ "@types/request": "^2.48.12",
86
86
  "@types/tmp": "^0.2.3",
87
87
  "@types/update-notifier": "^2.5.0",
88
88
  "@types/vorpal": "^1.12.0",
89
89
  "@types/websocket": "^0.0.40",
90
- "@types/yargs": "^13.0.0",
90
+ "@types/yargs": "^17.0.33",
91
91
  "child-process-promise": "^2.2.1",
92
92
  "clipboardy": "^2.0.0",
93
93
  "command-exists-promise": "^2.0.2",
94
- "common-tags": "^1.8.0",
94
+ "common-tags": "^1.8.2",
95
95
  "connection-string-parser": "^1.0.4",
96
96
  "date-fns": "^3.6.0",
97
97
  "fs-extra": "^7.0.1",
98
+ "gitlab": "^4.4.1",
98
99
  "lodash": "^4.17.21",
99
- "memoizee": "^0.4.14",
100
+ "memoizee": "^0.4.17",
100
101
  "node-fetch": "^2.3.0",
101
102
  "open": "^8.4.0",
102
- "prettier": "^3.3.2",
103
+ "prettier": "^3.3.3",
103
104
  "tmp-promise": "^2.0.2",
104
- "ts-results-es": "^4.1.0-alpha.1",
105
+ "ts-results-es": "^4.2.0",
105
106
  "tsc-alias": "^1.8.10",
106
107
  "typescript": "^5.4.5",
107
108
  "vorpal": "^1.12.0",
108
- "yaml": "^2.4.5"
109
+ "yaml": "^2.5.0"
109
110
  },
110
111
  "dependencies": {
111
- "tsx": "^4.16.2"
112
+ "tsx": "^4.19.0"
112
113
  },
113
114
  "stableVersion": "0.0.0"
114
115
  }
@@ -12,7 +12,7 @@ export default async (choice?: Choice) => {
12
12
  }
13
13
 
14
14
  await Promise.all([
15
- generatePipelineFiles(config, config.pipelineType ?? "gitlab", "local"),
15
+ generatePipelineFiles(config, config.pipelineType ?? "gitlab"),
16
16
  writeDotEnvFiles(config, choice),
17
17
 
18
18
  writeDTsFiles(config, choice),
@@ -1,20 +1,10 @@
1
1
  import type { CommandInstance } from "vorpal";
2
2
  import { add, format } from "date-fns";
3
3
  import { doGitlabRequest, getProjectInfo } from "../../../../../utils/gitlab";
4
- import {
5
- getGitlabProjectId,
6
- gitlabApiClient,
7
- type GitlabRestClient,
8
- } from "../../../../../gitlab";
9
4
 
10
5
  const TOKEN_NAME = "semantic-release";
11
6
 
12
- export const setupAccessTokens = async (
13
- instance: CommandInstance,
14
- preinit?: [number, GitlabRestClient],
15
- ) => {
16
- const [id, gitlabApi] =
17
- preinit ?? (await Promise.all([getGitlabProjectId(), gitlabApiClient()]));
7
+ export const setupAccessTokens = async (instance: CommandInstance) => {
18
8
  const { id: projectId } = await getProjectInfo(instance);
19
9
 
20
10
  const projectTokens = await doGitlabRequest(
@@ -78,9 +78,7 @@ export const setupCloudRun = async (
78
78
  },
79
79
  context.env,
80
80
  context.name,
81
- {
82
- backup: false,
83
- masked: false,
84
- },
81
+ false, // backup
82
+ false, // masked
85
83
  );
86
84
  };
@@ -6,30 +6,27 @@ import {
6
6
  import type { CommandInstance } from "vorpal";
7
7
  import { exec } from "child-process-promise";
8
8
  import { connectToCluster } from "../../../../../utils/cluster";
9
- import { upsertAllVariables } from "../../../../../utils/gitlab";
9
+ import {
10
+ doGitlabRequest,
11
+ getProjectInfo,
12
+ upsertAllVariables,
13
+ } from "../../../../../utils/gitlab";
10
14
  import ensureNamespace from "../utils/ensureNamespace";
11
- import type { GitlabRestClient } from "../../../../../gitlab";
12
- import { getGitlabProjectId, gitlabApiClient } from "../../../../../gitlab";
13
15
 
14
16
  export const setupKubernetes = async (
15
17
  instance: CommandInstance,
16
18
  context: ComponentContext,
17
- preinit?: [number, GitlabRestClient],
18
19
  ) => {
19
20
  const deployConfig = context.deploy?.config;
20
21
  if (!isOfDeployType(deployConfig, "kubernetes")) {
21
22
  throw new Error("cannot run setupKubernetes on non-kubernetes deployments");
22
23
  }
23
- const [id, gitlabApi] =
24
- preinit ?? (await Promise.all([getGitlabProjectId(), gitlabApiClient()]));
25
-
26
- const deploy_tokens = await gitlabApi.DeployTokens.all({ projectId: id });
27
24
 
28
- // const { id: projectId } = await getProjectInfo(instance);
29
- // const deploy_tokens = await doGitlabRequest(
30
- // instance,
31
- // `projects/${projectId}/deploy_tokens`,
32
- // );
25
+ const { id: projectId } = await getProjectInfo(instance);
26
+ const deploy_tokens = await doGitlabRequest(
27
+ instance,
28
+ `projects/${projectId}/deploy_tokens`,
29
+ );
33
30
 
34
31
  if (
35
32
  !deploy_tokens.find(
@@ -39,21 +36,17 @@ export const setupKubernetes = async (
39
36
  instance.log(
40
37
  "I will setup the 'GitLab Deploy Token', so Kubernetes can pull images from this project.",
41
38
  );
42
- await gitlabApi.DeployTokens.create(
43
- "gitlab-deploy-token",
44
- ["read_registry"],
45
- { projectId: id },
39
+
40
+ await doGitlabRequest(
41
+ instance,
42
+ `projects/${projectId}/deploy_tokens`,
43
+ {
44
+ id: projectId,
45
+ name: "gitlab-deploy-token",
46
+ scopes: ["read_registry"],
47
+ },
48
+ "POST",
46
49
  );
47
- // await doGitlabRequest(
48
- // instance,
49
- // `projects/${projectId}/deploy_tokens`,
50
- // {
51
- // id: projectId,
52
- // name: "gitlab-deploy-token",
53
- // scopes: ["read_registry"],
54
- // },
55
- // "POST",
56
- // );
57
50
  }
58
51
 
59
52
  const fullName = getFullKubernetesClusterName(deployConfig.cluster);
@@ -167,8 +160,12 @@ EOF
167
160
  instance.log("");
168
161
  instance.log("pusing secrets to gitlab...");
169
162
 
170
- await upsertAllVariables(instance, vars, context.env, context.name, {
171
- backup: false,
172
- });
163
+ await upsertAllVariables(
164
+ instance,
165
+ vars,
166
+ context.env,
167
+ context.name,
168
+ false, // no backup
169
+ );
173
170
  instance.log("done!");
174
171
  };
@@ -1,12 +1,17 @@
1
1
  import type { CommandInstance } from "vorpal";
2
- import { getGitlabProjectInfo, gitlabApiClient } from "../../../../../gitlab";
2
+ import { doGitlabRequest, getProjectInfo } from "../../../../../utils/gitlab";
3
3
 
4
4
  const catladderTopic = "catladder";
5
5
 
6
6
  export const setupTopic = async (instance: CommandInstance) => {
7
- const api = await gitlabApiClient();
8
- const info = await getGitlabProjectInfo(api);
9
- await api.Projects.edit(info.id, {
10
- topics: [catladderTopic, ...info.topics],
11
- });
7
+ const { id: projectId } = await getProjectInfo(instance);
8
+
9
+ const { topics } = await doGitlabRequest(instance, `projects/${projectId}`);
10
+
11
+ await doGitlabRequest(
12
+ instance,
13
+ `projects/${projectId}`,
14
+ { topics: [catladderTopic, ...topics] },
15
+ "PUT",
16
+ );
12
17
  };
@@ -4,8 +4,9 @@ import {
4
4
  setPreference,
5
5
  } from "../../../utils/preferences";
6
6
 
7
+ const KEY = "googleAuthUserNumber";
7
8
  export const getGoogleAuthUserNumber = async function () {
8
- if (!(await hasPreference("googleAuthUserNumber"))) {
9
+ if (!(await hasPreference(KEY))) {
9
10
  this.log(
10
11
  "Please type in your google auth user number (0 if you have only one google account or maybe 1 if you have multiple)",
11
12
  );
@@ -16,10 +17,7 @@ export const getGoogleAuthUserNumber = async function () {
16
17
  default: "0",
17
18
  message: "Auth user ",
18
19
  });
19
- await setPreference(
20
- "googleAuthUserNumber",
21
- Number.parseInt(authUserNumber, 10),
22
- );
20
+ await setPreference(KEY, authUserNumber);
23
21
  }
24
- return getPreference("googleAuthUserNumber");
22
+ return getPreference(KEY);
25
23
  };
@@ -4,7 +4,6 @@
4
4
  // this will point to the root package.json
5
5
 
6
6
  const path = require.main?.path + "/../package.json";
7
- // eslint-disable-next-line @typescript-eslint/no-var-requires
8
7
  const ownPkg = require(path);
9
8
 
10
9
  export default ownPkg;
@@ -5,20 +5,11 @@ import memoizee from "memoizee";
5
5
  import fetch from "node-fetch";
6
6
  import open from "open";
7
7
  import type { CommandInstance } from "vorpal";
8
- import { hasPreference } from "./preferences";
9
- import {
10
- setGitlabToken,
11
- getGitlabToken as getGitlabTokenFromPerferences,
12
- getGitlabProjectId,
13
- getGitlabProjectInfo,
14
- gitlabApiClient,
15
- type GitlabRestClient,
16
- } from "../gitlab";
17
- import type { Camelize, ProjectVariableSchema } from "@gitbeaker/rest";
18
-
19
- export const hasGitlabToken = async () =>
20
- await hasPreference("gitlab-personal-access-token");
8
+ import { getPreference, hasPreference, setPreference } from "./preferences";
21
9
 
10
+ const TOKEN_KEY = "gitlab-personal-access-token";
11
+
12
+ export const hasGitlabToken = async () => await hasPreference(TOKEN_KEY);
22
13
  export const setupGitlabToken = async (vorpal: CommandInstance) => {
23
14
  vorpal.log("");
24
15
  vorpal.log("☝ in order to access the api, we need a personal access token");
@@ -32,10 +23,6 @@ export const setupGitlabToken = async (vorpal: CommandInstance) => {
32
23
  name: "shouldContinue",
33
24
  type: "prompt",
34
25
  });
35
- if (!shouldContinue) {
36
- console.info("Aborted by user");
37
- process.exit(0);
38
- }
39
26
 
40
27
  open("https://git.panter.ch/-/profile/personal_access_tokens");
41
28
 
@@ -47,15 +34,12 @@ export const setupGitlabToken = async (vorpal: CommandInstance) => {
47
34
  default: "",
48
35
  message: "Your personal access token ",
49
36
  });
50
- if (typeof personalToken === "string" && personalToken.length) {
51
- await setGitlabToken(personalToken);
52
- } else {
53
- console.error("⚠️ no gitlab personal access token set");
37
+ if (personalToken) {
38
+ await setPreference(TOKEN_KEY, personalToken);
54
39
  }
55
40
  };
56
41
  export const getGitlabToken = async (vorpal: CommandInstance | null) => {
57
- const { token } = await getGitlabTokenFromPerferences();
58
- if (!token?.length) {
42
+ if (!(await hasGitlabToken())) {
59
43
  if (!vorpal) {
60
44
  console.error(
61
45
  "⚠️ gitlab token missing, please run catladder to set it up",
@@ -64,7 +48,7 @@ export const getGitlabToken = async (vorpal: CommandInstance | null) => {
64
48
  }
65
49
  await setupGitlabToken(vorpal);
66
50
  }
67
- return token;
51
+ return getPreference(TOKEN_KEY);
68
52
  };
69
53
 
70
54
  type Method = "GET" | "PUT" | "POST" | "DELETE";
@@ -75,9 +59,6 @@ export const doGitlabRequest = async <T = any>(
75
59
  method: Method = "GET",
76
60
  ): Promise<T> => {
77
61
  const rootToken = await getGitlabToken(vorpal);
78
- if (typeof rootToken !== "string") {
79
- throw new Error("Missing gitlab personal access token!");
80
- }
81
62
 
82
63
  //const method = data ? (update ? "PUT" : "POST") : "GET";
83
64
 
@@ -107,10 +88,24 @@ export const doGitlabRequest = async <T = any>(
107
88
  );
108
89
  };
109
90
 
110
- export const getProjectInfo = async (vorpal: CommandInstance | null) =>
111
- getGitlabProjectInfo();
91
+ export const getProjectInfo = async (
92
+ vorpal: CommandInstance | null,
93
+ ): Promise<{ id: string; web_url: string }> => {
94
+ const gitRemoteOriginUrl = (
95
+ await exec("git config --get remote.origin.url")
96
+ ).stdout.trim();
97
+ const projectPath =
98
+ /(https:\/\/|git@)git\.panter\.ch[:/]([^.]*)(\.git)?/g.exec(
99
+ gitRemoteOriginUrl,
100
+ );
101
+ const project = await doGitlabRequest(
102
+ vorpal,
103
+ `projects/${encodeURIComponent(projectPath[2])}`,
104
+ );
105
+ return project;
106
+ };
112
107
 
113
- export type GitlabVariable = {
108
+ type GitlabVariable = {
114
109
  variable_type: string;
115
110
  key: string;
116
111
  value: string;
@@ -121,36 +116,25 @@ export type GitlabVariable = {
121
116
  export const getAllVariables = memoizee(
122
117
  async (
123
118
  vorpal: CommandInstance | null,
124
- preinit?: [number, GitlabRestClient],
125
- ): Promise<(ProjectVariableSchema | Camelize<ProjectVariableSchema>)[]> => {
126
- const [id, gitlabApi] =
127
- preinit ?? (await Promise.all([getGitlabProjectId(), gitlabApiClient()]));
128
- // how many requests to do in parallel, 5 seems a good value for many projects, ideally we would do one request in parallel per component
129
- const n = 5;
130
- let all: (ProjectVariableSchema | Camelize<ProjectVariableSchema>)[] = [];
131
- let result: (ProjectVariableSchema | Camelize<ProjectVariableSchema>)[][] =
132
- [];
119
+ n = 5, // how many requests to do in parallel, 5 seems a good value for many projects, ideally we would do one request in parallel per component
120
+ ): Promise<Array<GitlabVariable>> => {
121
+ const { id } = await getProjectInfo(vorpal);
122
+
123
+ let all: Array<GitlabVariable> = [];
124
+ let result: Array<Array<GitlabVariable>> = [];
133
125
  let page = 1;
134
126
 
135
127
  do {
136
128
  // Create an array of promises for N pages
137
129
  const promises = Array.from({ length: n }, (_, i) => {
138
- return gitlabApi.ProjectVariables.all(id, {
139
- page: page + i,
140
- perPage: 100,
141
- });
130
+ return doGitlabRequest(
131
+ vorpal,
132
+ `projects/${id}/variables?per_page=100&page=${page + i}`,
133
+ );
142
134
  });
143
135
 
144
136
  // Wait for all promises to resolve
145
- result = await Promise.all(
146
- Array.from({ length: n }, async (_, i) => {
147
- const res = await gitlabApi.ProjectVariables.all(id, {
148
- page: page + i,
149
- perPage: 100,
150
- });
151
- return res;
152
- }),
153
- );
137
+ result = await Promise.all(promises);
154
138
 
155
139
  // Increment the page by N
156
140
  page += n;
@@ -176,11 +160,60 @@ export const getVariableValueByRawName = async (
176
160
  const maskableRegex = new RegExp("^[a-zA-Z0-9_+=/@:.~-]{8,}$"); // SEE https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/spec/frontend/ci_variable_list/components/ci_variable_modal_spec.js#L20
177
161
  const isMaskable = (value: string): boolean => maskableRegex.test(value);
178
162
 
179
- const getAllCatladderEnvVarsInGitlab = async (
163
+ const createVariable = async (
180
164
  vorpal: CommandInstance,
181
- preinit?: [number, GitlabRestClient],
165
+ projectId: string,
166
+ key: string,
167
+ value: string,
168
+ masked = true,
169
+ environment_scope = "*",
182
170
  ) => {
183
- const allVariables = await getAllVariables(vorpal, preinit).then((v) =>
171
+ return await doGitlabRequest(
172
+ vorpal,
173
+ `projects/${projectId}/variables`,
174
+ {
175
+ key,
176
+ value,
177
+ masked: masked && isMaskable(value),
178
+ environment_scope,
179
+ },
180
+ "POST",
181
+ );
182
+ };
183
+
184
+ const updateVariable = async (
185
+ vorpal: CommandInstance,
186
+ projectId: string,
187
+ key: string,
188
+ value: string,
189
+ masked = true,
190
+ ) => {
191
+ return await doGitlabRequest(
192
+ vorpal,
193
+ `projects/${projectId}/variables/${key}`,
194
+ {
195
+ value,
196
+ masked: masked && isMaskable(value),
197
+ },
198
+ "PUT",
199
+ );
200
+ };
201
+
202
+ const deleteVariable = async (
203
+ vorpal: CommandInstance,
204
+ projectId: string,
205
+ key: string,
206
+ ) => {
207
+ return await doGitlabRequest(
208
+ vorpal,
209
+ `projects/${projectId}/variables/${key}`,
210
+ undefined,
211
+ "DELETE",
212
+ );
213
+ };
214
+
215
+ const getAllCatladderEnvVarsInGitlab = async (vorpal: CommandInstance) => {
216
+ const allVariables = await getAllVariables(vorpal).then((v) =>
184
217
  v.reduce<{
185
218
  [key: string]: {
186
219
  value?: string;
@@ -223,25 +256,16 @@ const getAllCatladderEnvVarsInGitlab = async (
223
256
 
224
257
  const getBackupKey = (fullKey: string, timestamp: number) =>
225
258
  `${fullKey}_backup_${timestamp}`;
226
-
227
259
  export const clearBackups = async (vorpal: CommandInstance, keep: number) => {
228
- const [id, gitlabApi] = await Promise.all([
229
- getGitlabProjectId(),
230
- gitlabApiClient(),
231
- ]);
232
- const existingVariables = await getAllCatladderEnvVarsInGitlab(vorpal, [
233
- id,
234
- gitlabApi,
235
- ]);
236
- // const { id } = await getProjectInfo(vorpal);
260
+ const existingVariables = await getAllCatladderEnvVarsInGitlab(vorpal);
261
+ const { id } = await getProjectInfo(vorpal);
237
262
  for (const [key, { backups }] of Object.entries(existingVariables)) {
238
263
  const backupsSorted = backups.sort((a, b) => b - a);
239
264
  //const toKeep = backupsSorted.slice(0, keep);
240
265
  const toDelete = backupsSorted.slice(keep);
241
266
 
242
267
  for (const timestamp of toDelete) {
243
- await gitlabApi.ProjectVariables.remove(id, getBackupKey(key, timestamp));
244
- // await deleteVariable(vorpal, id, getBackupKey(key, timestamp));
268
+ await deleteVariable(vorpal, id, getBackupKey(key, timestamp));
245
269
  }
246
270
  }
247
271
  };
@@ -251,26 +275,13 @@ export const upsertAllVariables = async (
251
275
  variables: Record<string, any>,
252
276
  env: string,
253
277
  componentName: string,
254
- {
255
- backup = true,
256
- masked = true,
257
- preinit,
258
- }: {
259
- backup?: boolean;
260
- // FIXME: would be better to have this per variable
261
- masked?: boolean;
262
- preinit?: [number, GitlabRestClient];
263
- } = {},
278
+ backup = true,
279
+ masked = true, // FIXME: would be better to have this per variable
264
280
  ): Promise<void> => {
265
- const [id, gitlabApi] =
266
- preinit ?? (await Promise.all([getGitlabProjectId(), gitlabApiClient()]));
267
- // const { id } = await getProjectInfo(vorpal);
281
+ const { id } = await getProjectInfo(vorpal);
268
282
 
269
283
  // we list all existing variables. We also gather backup versions, but its currently unused
270
- const existingVariables = await getAllCatladderEnvVarsInGitlab(vorpal, [
271
- id,
272
- gitlabApi,
273
- ]);
284
+ const existingVariables = await getAllCatladderEnvVarsInGitlab(vorpal);
274
285
  for (const [key, value] of Object.entries(variables ?? {})) {
275
286
  const fullKey = getSecretVarName(env, componentName, key);
276
287
  const valueSanitized = isObject(value) ? JSON.stringify(value) : `${value}`;
@@ -282,30 +293,21 @@ export const upsertAllVariables = async (
282
293
  if (exists) {
283
294
  vorpal.log(`changed: ${key}`);
284
295
 
285
- await gitlabApi.ProjectVariables.edit(id, fullKey, valueSanitized, {
286
- filter: { environment_scope: "All (default)" },
287
- masked,
288
- });
289
- // await updateVariable(vorpal, id, fullKey, valueSanitized, masked);
296
+ await updateVariable(vorpal, id, fullKey, valueSanitized, masked);
290
297
  // write backup
291
298
  if (backup) {
292
- await gitlabApi.ProjectVariables.create(
299
+ await createVariable(
300
+ vorpal,
293
301
  id,
294
302
  getBackupKey(fullKey, new Date().getTime()),
295
303
  oldValue,
296
- {
297
- environmentScope: "_backup",
298
- masked,
299
- },
304
+ masked,
305
+ "_backup",
300
306
  );
301
307
  }
302
308
  } else {
303
309
  vorpal.log(`new : ${key}`);
304
- await gitlabApi.ProjectVariables.create(id, fullKey, valueSanitized, {
305
- environmentScope: "All (default)",
306
- masked,
307
- });
308
- // await createVariable(vorpal, id, fullKey, valueSanitized, masked);
310
+ await createVariable(vorpal, id, fullKey, valueSanitized, masked);
309
311
  }
310
312
  } else {
311
313
  vorpal.log(`skip : ${key}`);
@@ -0,0 +1,36 @@
1
+ import { createFile, pathExists, readFile, writeFile } from "fs-extra";
2
+ import { homedir } from "os";
3
+
4
+ import { parse, stringify } from "yaml";
5
+ const directory = `${homedir()}/.catladder`;
6
+ const file = `${directory}/preferences.yml`;
7
+
8
+ const getPreferences = async () => {
9
+ if (!(await pathExists(file))) {
10
+ await createFile(file);
11
+ }
12
+ return (parse(await readFile(file, { encoding: "utf-8" })) ?? {}) as Record<
13
+ string,
14
+ string
15
+ >;
16
+ };
17
+
18
+ export const hasPreference = async (key: string) => {
19
+ const preferences = await getPreferences();
20
+ return key in preferences;
21
+ };
22
+ export const getPreference = async (key: string) => {
23
+ const preferences = await getPreferences();
24
+ return preferences[key];
25
+ };
26
+
27
+ export const setPreference = async (key: string, value: string | number) => {
28
+ const preferences = await getPreferences();
29
+
30
+ const newPreferences = {
31
+ ...preferences,
32
+ [key]: value,
33
+ };
34
+
35
+ await writeFile(file, stringify(newPreferences));
36
+ };
@@ -1,4 +0,0 @@
1
- export declare const getGitConfigVars: () => Promise<{
2
- PROJECT_ROOT_PATH: string;
3
- GIT_REMOTE_HOST: string;
4
- }>;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getGitConfigVars = void 0;
4
- const getGitRemoteHostAndPath_1 = require("./getGitRemoteHostAndPath");
5
- const getProjectRootPath_1 = require("./getProjectRootPath");
6
- const getGitConfigVars = async () => {
7
- const [{ gitRemoteHost: GIT_REMOTE_HOST }, PROJECT_ROOT_PATH] = await Promise.all([(0, getGitRemoteHostAndPath_1.getGitRemoteHostAndPath)(), (0, getProjectRootPath_1.getProjectRootPath)()]);
8
- return {
9
- PROJECT_ROOT_PATH,
10
- GIT_REMOTE_HOST,
11
- };
12
- };
13
- exports.getGitConfigVars = getGitConfigVars;
14
- //# sourceMappingURL=getGitConfigVars.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getGitConfigVars.js","sourceRoot":"","sources":["../../../../src/git/getGitConfigVars.ts"],"names":[],"mappings":";;;AAAA,uEAAoE;AACpE,6DAA0D;AAEnD,MAAM,gBAAgB,GAAG,KAAK,IAAI,EAAE;IACzC,MAAM,CAAC,EAAE,aAAa,EAAE,eAAe,EAAE,EAAE,iBAAiB,CAAC,GAC3D,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,IAAA,iDAAuB,GAAE,EAAE,IAAA,uCAAkB,GAAE,CAAC,CAAC,CAAC;IACvE,OAAO;QACL,iBAAiB;QACjB,eAAe;KAChB,CAAC;AACJ,CAAC,CAAC;AAPW,QAAA,gBAAgB,oBAO3B"}
@@ -1,9 +0,0 @@
1
- /**
2
- * Gets the git remote host and path from cat-cli config or else from the git config.
3
- *
4
- * Writes the values to the cat-cli config file.
5
- */
6
- export declare function getGitRemoteHostAndPath(): Promise<{
7
- gitRemoteHost: string;
8
- gitRemotePath: string;
9
- }>;
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getGitRemoteHostAndPath = void 0;
4
- const gitConfig_1 = require("./gitConfig");
5
- /**
6
- * Gets the git remote host and path from cat-cli config or else from the git config.
7
- *
8
- * Writes the values to the cat-cli config file.
9
- */
10
- async function getGitRemoteHostAndPath() {
11
- const gitRemoteOriginUrl = await (0, gitConfig_1.gitConfigGet)("remote.origin.url");
12
- const match = gitRemoteOriginUrl.match(/(https:\/\/|git@)([^:/]+)[:/]([^.]*)(\.git)?/);
13
- if (!match || !match.length) {
14
- throw new Error(`Could not find gitlab instance url from git! ${gitRemoteOriginUrl}`);
15
- }
16
- const [, , gitRemoteHost, gitRemotePath] = match;
17
- if (!(gitRemoteHost === null || gitRemoteHost === void 0 ? void 0 : gitRemoteHost.length) || !(gitRemotePath === null || gitRemotePath === void 0 ? void 0 : gitRemotePath.length)) {
18
- throw new Error(`Could not get gitlab hostname and project path from git config! ${gitRemoteOriginUrl}`);
19
- }
20
- return { gitRemoteHost, gitRemotePath };
21
- }
22
- exports.getGitRemoteHostAndPath = getGitRemoteHostAndPath;
23
- //# sourceMappingURL=getGitRemoteHostAndPath.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getGitRemoteHostAndPath.js","sourceRoot":"","sources":["../../../../src/git/getGitRemoteHostAndPath.ts"],"names":[],"mappings":";;;AAAA,2CAA2C;AAE3C;;;;GAIG;AACI,KAAK,UAAU,uBAAuB;IAI3C,MAAM,kBAAkB,GAAG,MAAM,IAAA,wBAAY,EAAC,mBAAmB,CAAC,CAAC;IACnE,MAAM,KAAK,GAAG,kBAAkB,CAAC,KAAK,CACpC,8CAA8C,CAC/C,CAAC;IACF,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CACb,gDAAgD,kBAAkB,EAAE,CACrE,CAAC;IACJ,CAAC;IACD,MAAM,CAAC,EAAE,AAAD,EAAG,aAAa,EAAE,aAAa,CAAC,GAAG,KAAK,CAAC;IACjD,IAAI,CAAC,CAAA,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,MAAM,CAAA,IAAI,CAAC,CAAA,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,MAAM,CAAA,EAAE,CAAC;QACrD,MAAM,IAAI,KAAK,CACb,mEAAmE,kBAAkB,EAAE,CACxF,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AAC1C,CAAC;AApBD,0DAoBC"}
@@ -1 +0,0 @@
1
- export declare function getProjectRootPath(): Promise<string>;