UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

111 lines 3.9 kB
"use strict"; 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 useGitHubAction() { return { detect() { // eslint-disable-next-line no-process-env return !!process.env.GITHUB_ACTIONS; }, 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 commit = null; const isPr = // eslint-disable-next-line no-process-env process.env.GITHUB_EVENT_NAME === 'pull_request' || // eslint-disable-next-line no-process-env process.env.GITHUB_EVENT_NAME === 'pull_request_target'; if (isPr) { 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 (event === null || event === void 0 ? void 0 : event.pull_request) { commit = event.pull_request.head.sha; pullRequestNumber = String(event.pull_request.number); } } catch (_b) { // Noop } } return { commit, pullRequestNumber, repository }; }, }; } async function gitInfo(noGit) { var _a, _b, _c, _d; let repository = null; let pullRequestNumber = null; let commit = null; let author = null; const env = (0, env_ci_1.default)(); const githubAction = useGitHubAction(); if (githubAction.detect()) { const env = githubAction.env(); repository = (_a = env.repository) !== null && _a !== void 0 ? _a : null; commit = (_b = env.commit) !== null && _b !== void 0 ? _b : null; pullRequestNumber = (_c = env.pullRequestNumber) !== null && _c !== void 0 ? _c : null; } if (!commit) { commit = (_d = env.commit) !== null && _d !== void 0 ? _d : null; } if (!commit || !author) { const git = await getLatestCommitFromGit(); if (git) { if (!commit) { commit = git.hash; } if (!author) { author = git.author; } } else { noGit(); } } return { repository, pullRequestNumber, commit, author, }; } //# sourceMappingURL=git.js.map