UNPKG

3.63 kBJavaScriptView Raw
1"use strict";
2var __rest = (this && this.__rest) || function (s, e) {
3 var t = {};
4 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5 t[p] = s[p];
6 if (s != null && typeof Object.getOwnPropertySymbols === "function")
7 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9 t[p[i]] = s[p[i]];
10 }
11 return t;
12};
13var __importDefault = (this && this.__importDefault) || function (mod) {
14 return (mod && mod.__esModule) ? mod : { "default": mod };
15};
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.gitInfo = exports.sanitizeGitRemote = void 0;
18const path_1 = __importDefault(require("path"));
19const fs_1 = __importDefault(require("fs"));
20const env_ci_1 = __importDefault(require("env-ci"));
21const git_parse_1 = require("git-parse");
22const git_rev_sync_1 = __importDefault(require("git-rev-sync"));
23const lodash_pickby_1 = __importDefault(require("lodash.pickby"));
24const lodash_identity_1 = __importDefault(require("lodash.identity"));
25const git_url_parse_1 = __importDefault(require("git-url-parse"));
26const findGitRoot = (start) => {
27 start = start || process.cwd();
28 if (typeof start === "string") {
29 if (start[start.length - 1] !== path_1.default.sep)
30 start += path_1.default.sep;
31 start = start.split(path_1.default.sep);
32 }
33 if (!start.length)
34 return;
35 start.pop();
36 const dir = start.join(path_1.default.sep);
37 if (fs_1.default.existsSync(path_1.default.join(dir, ".git"))) {
38 return path_1.default.normalize(dir);
39 }
40 else {
41 return findGitRoot(start);
42 }
43};
44const sanitizeGitRemote = (remote) => {
45 if (!remote)
46 return null;
47 const info = git_url_parse_1.default(remote);
48 const source = info.source.toLowerCase();
49 if (source !== "github.com" &&
50 source !== "gitlab.com" &&
51 source !== "bitbucket.org")
52 return null;
53 if (info.user !== "" && info.user !== "git") {
54 info.user = "REDACTED";
55 }
56 info.token = "";
57 info.href = null;
58 return git_url_parse_1.default.stringify(info);
59};
60exports.sanitizeGitRemote = sanitizeGitRemote;
61const gitInfo = async (log) => {
62 const { commit, branch: ciBranch, root, prBranch } = env_ci_1.default();
63 const gitLoc = root ? root : findGitRoot();
64 if (!commit)
65 return;
66 let committer, remoteUrl, message;
67 let branch = ciBranch || prBranch;
68 if (gitLoc) {
69 const _a = await git_parse_1.gitToJs(gitLoc)
70 .then((commits) => commits && commits.length > 0
71 ? commits[0]
72 : { authorName: null, authorEmail: null, message: null })
73 .catch(() => ({ authorEmail: null, authorName: null, message: null })), { authorName, authorEmail } = _a, commit = __rest(_a, ["authorName", "authorEmail"]);
74 committer = `${authorName || ""} ${authorEmail ? `<${authorEmail}>` : ""}`.trim();
75 message = commit.message;
76 try {
77 remoteUrl = exports.sanitizeGitRemote(git_rev_sync_1.default.remoteUrl());
78 }
79 catch (e) {
80 log(["Unable to retrieve remote url, failed with:", e].join("\n\n"));
81 }
82 if (!branch) {
83 branch = git_rev_sync_1.default.branch([gitLoc]);
84 }
85 }
86 return lodash_pickby_1.default({
87 committer,
88 commit,
89 remoteUrl,
90 message,
91 branch
92 }, lodash_identity_1.default);
93};
94exports.gitInfo = gitInfo;
95//# sourceMappingURL=git.js.map
\No newline at end of file