UNPKG

wgc

Version:

The official CLI tool to manage the GraphQL Federation Platform Cosmo

62 lines 2.31 kB
import { readFileSync } from 'node:fs'; import envCi from 'env-ci'; import { getBaseHeaders } from './core/config.js'; // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables function getLatestPRCommit() { try { const event = process.env.GITHUB_EVENT_PATH ? JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')) : undefined; if (event && event.pull_request) { return event.pull_request.head.sha; } } catch { return undefined; } } export function useGitHub() { const isPr = process.env.GITHUB_EVENT_NAME === 'pull_request' || process.env.GITHUB_EVENT_NAME === 'pull_request_target'; let commit = getLatestPRCommit(); if (!commit) { const env = envCi(); commit = env.commit; } return { isPr, commit, build: process.env.GITHUB_RUN_ID, branch: process.env.GITHUB_HEAD_REF, repository: process.env.GITHUB_REPOSITORY, accountId: process.env.GITHUB_REPOSITORY_OWNER_ID, repositoryId: process.env.GITHUB_REPOSITORY_ID, root: process.env.GITHUB_WORKSPACE, }; } export const verifyGitHubIntegration = async (client) => { let gitInfo; const { isPr, commit: commitSha, repository, accountId } = useGitHub(); if (isPr && commitSha && repository && accountId) { const [ownerSlug, repositorySlug] = repository === null || repository === void 0 ? void 0 : repository.split('/'); gitInfo = { commitSha, accountId, ownerSlug, repositorySlug, }; } let ignoreErrorsDueToGitHubIntegration = false; if (gitInfo) { const integrationCheckResponse = await client.platform.isGitHubAppInstalled({ gitInfo, }, { headers: getBaseHeaders(), }); ignoreErrorsDueToGitHubIntegration = integrationCheckResponse.isInstalled; if (ignoreErrorsDueToGitHubIntegration) { console.log('GitHub integration detected. The command will succeed and any errors detected will be reflected on commit status instead.'); } } return { gitInfo, ignoreErrorsDueToGitHubIntegration }; }; //# sourceMappingURL=github.js.map