@graphql-hive/cli
Version:
A CLI util to manage and control your GraphQL Hive
135 lines • 5.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.gitInfo = gitInfo;
const tslib_1 = require("tslib");
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const env_ci_1 = tslib_1.__importDefault(require("env-ci"));
const splitBy = '<##>';
const gitLogFormat = [
/* full hash */ '%H',
/* Author's name */ '%an',
/* Author's email */ '%ae',
].join(splitBy);
const latestCommitCommand = `git log -1 --pretty=format:"${gitLogFormat}"`;
function getLatestCommitFromGit() {
return new Promise(resolve => {
(0, child_process_1.exec)(latestCommitCommand, { cwd: process.cwd() }, (_, stdout) => {
if (stdout.includes(splitBy)) {
const [hash, authorName, authorEmail] = stdout.split(splitBy);
if (hash && authorName) {
let author = authorName;
if (authorEmail) {
author += ` <${authorEmail}>`;
}
resolve({
hash,
author,
});
return;
}
}
resolve(null);
});
});
}
function getMergeGroupCommits(baseSha, headSha) {
return new Promise(resolve => {
// We use --no-merges to ignore the temporary merge commit generated by the queue
(0, child_process_1.exec)(`git rev-list --no-merges ${baseSha}..${headSha}`, { cwd: process.cwd() }, (error, stdout) => {
if (error) {
resolve([]);
return;
}
const commits = stdout
.split('\n')
.map(c => c.trim())
.filter(Boolean);
resolve(commits);
});
});
}
function useGitHubAction() {
return {
detect() {
// eslint-disable-next-line no-process-env
return !!process.env.GITHUB_ACTIONS;
},
async env() {
var _a;
// eslint-disable-next-line no-process-env
const repository = (_a = process.env['GITHUB_REPOSITORY']) !== null && _a !== void 0 ? _a : null;
let pullRequestNumber = null;
let commits = [];
// eslint-disable-next-line no-process-env
const eventName = process.env.GITHUB_EVENT_NAME;
const isPr = eventName === 'pull_request' || eventName === 'pull_request_target';
const isMergeGroup = eventName === 'merge_group';
if (isPr || isMergeGroup) {
try {
// eslint-disable-next-line no-process-env
const event = process.env.GITHUB_EVENT_PATH
? // eslint-disable-next-line no-process-env
JSON.parse((0, fs_1.readFileSync)(process.env.GITHUB_EVENT_PATH, 'utf-8'))
: undefined;
if (isPr && (event === null || event === void 0 ? void 0 : event.pull_request)) {
commits = [event.pull_request.head.sha];
pullRequestNumber = String(event.pull_request.number);
}
else if (isMergeGroup && (event === null || event === void 0 ? void 0 : event.merge_group)) {
const baseSha = event.merge_group.base_sha;
const headSha = event.merge_group.head_sha;
if (baseSha && headSha) {
commits = await getMergeGroupCommits(baseSha, headSha);
}
}
}
catch (_b) {
// Noop
}
}
return { commits, pullRequestNumber, repository };
},
};
}
async function gitInfo(noGit) {
var _a, _b, _c;
let repository = null;
let pullRequestNumber = null;
let commits = [];
let author = null;
const env = (0, env_ci_1.default)();
const githubAction = useGitHubAction();
if (githubAction.detect()) {
const ghEnv = await githubAction.env();
repository = (_a = ghEnv.repository) !== null && _a !== void 0 ? _a : null;
commits = (_b = ghEnv.commits) !== null && _b !== void 0 ? _b : [];
pullRequestNumber = (_c = ghEnv.pullRequestNumber) !== null && _c !== void 0 ? _c : null;
}
// Fallback to env-ci commit if empty
if (commits.length === 0 && env.commit) {
commits = [env.commit];
}
// Fallback to latest local commit if still empty, and fetch author
if (commits.length === 0 || !author) {
const git = await getLatestCommitFromGit();
if (git) {
if (commits.length === 0) {
commits = [git.hash];
}
if (!author) {
author = git.author;
}
}
else {
noGit();
}
}
return {
repository,
pullRequestNumber,
commits,
author,
};
}
//# sourceMappingURL=git.js.map