js-code-detector 0.0.33 → 0.0.35

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.
@@ -1,3 +1,4 @@
1
+ import { CloneType } from "./util/shared/gitDiffTool";
1
2
  export declare const gitDiffFileName = "git_diff.txt";
2
3
  export declare function getGitRepositoryAndBranch(): Promise<{
3
4
  gitUrl: string;
@@ -48,6 +49,6 @@ export declare function gitDiffDetectByUrl(inputUrl: string): Promise<{
48
49
  filePath: string;
49
50
  }[];
50
51
  } | undefined>;
51
- export declare function getUpstreamDependenceJson(inputUrl: string, cloneType?: 'ssh'): Promise<{
52
+ export declare function getUpstreamDependenceJson(inputUrl: string, cloneType?: CloneType): Promise<{
52
53
  [k: string]: string[];
53
54
  } | undefined>;
@@ -11,3 +11,4 @@ export declare function parseGitLabCompareUrl(compareUrl: string): {
11
11
  targetBranch: string;
12
12
  };
13
13
  export declare function getSshGitRepoUrl(gitRepoUrl: string): string;
14
+ export declare function getGitRepoUrlByToken(gitRepoUrl: string): string;
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  // src/util/parseGitLabDiffUril.ts
20
20
  var parseGitLabDiffUril_exports = {};
21
21
  __export(parseGitLabDiffUril_exports, {
22
+ getGitRepoUrlByToken: () => getGitRepoUrlByToken,
22
23
  getSshGitRepoUrl: () => getSshGitRepoUrl,
23
24
  parseGitLabCompareUrl: () => parseGitLabCompareUrl
24
25
  });
@@ -61,8 +62,22 @@ function parseGitLabCompareUrl(compareUrl) {
61
62
  function getSshGitRepoUrl(gitRepoUrl) {
62
63
  return gitRepoUrl.replace(/https?:\/\/[^/]+/, "git@");
63
64
  }
65
+ function getGitRepoUrlByToken(gitRepoUrl) {
66
+ const urlObj = new URL(gitRepoUrl);
67
+ const gitlabToken = process.env.GITLAB_ACCESS_TOKEN;
68
+ const gitlabDomain = process.env.GITLAB_DOMAIN || urlObj.hostname;
69
+ const projectPath = urlObj.pathname.replace(/\.git/, "");
70
+ if (!gitlabToken) {
71
+ throw new Error("未配置 GitLab accessToken,请通过 GITLAB_ACCESS_TOKEN 环境变量传入");
72
+ }
73
+ if (!projectPath) {
74
+ throw new Error("请传入 GitLab 项目路径(如 dev-team/my-project)");
75
+ }
76
+ return `http://${gitlabToken}@${gitlabDomain}/${projectPath}.git`;
77
+ }
64
78
  // Annotate the CommonJS export names for ESM import in node:
65
79
  0 && (module.exports = {
80
+ getGitRepoUrlByToken,
66
81
  getSshGitRepoUrl,
67
82
  parseGitLabCompareUrl
68
83
  });
@@ -1,9 +1,10 @@
1
1
  import { generateGitDiffReport } from "../report_util/generateGitDiffReport";
2
- export declare function cloneGitRepo(gitUrl: string, branchName: string, tempDirPath: string, folder: string, cloneType?: 'ssh'): Promise<{
3
- failed: boolean;
4
- stderr: string;
2
+ export type CloneType = 'ssh' | 'token';
3
+ export declare function cloneGitRepo(gitUrl: string, branchName: string, tempDirPath: string, folder: string, cloneType?: CloneType): Promise<{
4
+ failed: boolean | undefined;
5
+ stderr: string | undefined;
5
6
  }>;
6
- export declare function cloneGitRepoAndGetDiff(gitRepoUrl: string, branchName: string, cloneType?: 'ssh'): Promise<{
7
+ export declare function cloneGitRepoAndGetDiff(gitRepoUrl: string, branchName: string, cloneType?: CloneType): Promise<{
7
8
  [k: string]: string[];
8
9
  } | undefined>;
9
10
  export declare function gitDiffTool(arg: {
@@ -48,14 +48,18 @@ var import__ = require("../../index");
48
48
  var import_dayjs = __toESM(require("dayjs"));
49
49
  var import_fs = require("fs");
50
50
  var import_format_git_diff_content = require("../format_git_diff_content");
51
+ var import_gitUtil = require("./gitUtil");
51
52
  async function cloneGitRepo(gitUrl, branchName, tempDirPath, folder, cloneType) {
52
53
  import_utils.logger.ready(`准备clone 源代码到临时目录下的 ${tempDirPath}/${folder} 文件夹`);
53
54
  let stderr, failed;
54
55
  if (cloneType === "ssh") {
55
56
  const sshGitRepoUrl = (0, import_parseGitLabDiffUril.getSshGitRepoUrl)(gitUrl);
56
57
  ({ stderr, failed } = await import_utils.execa.execa(`git clone ${sshGitRepoUrl} ${tempDirPath}/${folder}`, { shell: true }));
58
+ } else if (cloneType === "token") {
59
+ const repoUrl = (0, import_parseGitLabDiffUril.getGitRepoUrlByToken)(gitUrl);
60
+ ({ stderr, failed } = await import_utils.execa.execa(`git clone ${repoUrl} ${tempDirPath}/${folder}`, { shell: true }));
57
61
  } else {
58
- ({ stderr, failed } = await import_utils.execa.execa(`git clone ${gitUrl} ${tempDirPath}/${folder}`, { shell: true }));
62
+ await (0, import_gitUtil.cloneRepoWithBranchAndDefault)(gitUrl, `${tempDirPath}/${folder}`, branchName);
59
63
  }
60
64
  (0, import_handleExecaError.handleExecaError)({ failed, stderr });
61
65
  import_utils.logger.info("源代码clone完成");
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 克隆 Git 仓库,同时获取目标分支和默认分支的代码
3
+ * @param {string} repoUrl - 仓库地址(带认证信息的 HTTPS/SSH 地址)
4
+ * @param {string} targetBranch - 主要目标分支(如 dev)
5
+ * @param {string} targetDir - 本地目标目录
6
+ * @returns {Promise<string>} 克隆目录路径
7
+ */
8
+ export declare function cloneRepoWithBranchAndDefault(repoUrl: string, targetBranch: string, targetDir: string): Promise<string>;
@@ -0,0 +1,51 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/util/shared/gitUtil.ts
20
+ var gitUtil_exports = {};
21
+ __export(gitUtil_exports, {
22
+ cloneRepoWithBranchAndDefault: () => cloneRepoWithBranchAndDefault
23
+ });
24
+ module.exports = __toCommonJS(gitUtil_exports);
25
+ var simpleGit = require("simple-git");
26
+ async function cloneRepoWithBranchAndDefault(repoUrl, targetBranch, targetDir) {
27
+ try {
28
+ const git = simpleGit();
29
+ await git.clone(repoUrl, targetDir, [
30
+ `--branch=${targetBranch}`
31
+ // 初始检出目标分支
32
+ // 不添加 --single-branch,默认会拉取所有分支的引用
33
+ ]);
34
+ const repoGit = simpleGit(targetDir);
35
+ const remoteInfo = await repoGit.remote(["show", "origin"]);
36
+ const defaultBranchMatch = remoteInfo.match(/HEAD branch: (\S+)/);
37
+ const defaultBranch = defaultBranchMatch ? defaultBranchMatch[1] : "main";
38
+ await repoGit.checkout(defaultBranch);
39
+ await repoGit.pull("origin", defaultBranch);
40
+ await repoGit.checkout(targetBranch);
41
+ console.log(`克隆完成:目标分支 ${targetBranch} + 默认分支 ${defaultBranch},路径:${targetDir}`);
42
+ return targetDir;
43
+ } catch (error) {
44
+ console.error(`克隆失败:${error.message}`);
45
+ throw error;
46
+ }
47
+ }
48
+ // Annotate the CommonJS export names for ESM import in node:
49
+ 0 && (module.exports = {
50
+ cloneRepoWithBranchAndDefault
51
+ });
@@ -1,4 +1,4 @@
1
1
  export declare function handleExecaError({ failed, stderr }: {
2
- failed: boolean;
3
- stderr: string;
2
+ failed: boolean | undefined;
3
+ stderr: string | undefined;
4
4
  }): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-code-detector",
3
- "version": "0.0.33",
3
+ "version": "0.0.35",
4
4
  "description": "",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/cjs/index.d.ts",
@@ -42,6 +42,7 @@
42
42
  "dayjs": "^1.11.13",
43
43
  "lodash": "^4.17.21",
44
44
  "madge": "^8.0.0",
45
+ "simple-git": "^3.28.0",
45
46
  "vue-eslint-parser": "^10.2.0"
46
47
  }
47
48
  }