UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

188 lines (187 loc) • 7.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleSchemaDiff = void 0; const tslib_1 = require("tslib"); const check_runs_js_1 = require("./helpers/check-runs.js"); const config_js_1 = require("./helpers/config.js"); const diff_js_1 = require("./helpers/diff.js"); const errors_js_1 = require("./helpers/errors.js"); const loaders_js_1 = require("./helpers/loaders.js"); const logger_js_1 = require("./helpers/logger.js"); const schema_js_1 = require("./helpers/schema.js"); const types_js_1 = require("./helpers/types.js"); const utils_js_1 = require("./helpers/utils.js"); function handleSchemaDiff({ release, action, context, ref, pullRequestNumber, repo, owner, before, pullRequests = [], loadFile, loadConfig, onError, }) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const id = `${owner}/${repo}#${ref}`; const logger = (0, logger_js_1.createLogger)('DIFF', context, release); logger.info(`Started - ${id}`); logger.info(`Action: "${action}"`); const checkRunId = yield (0, check_runs_js_1.start)({ context, owner, repo, sha: ref, logger, }); try { logger.info(`Looking for config`); const rawConfig = yield loadConfig(); if (!rawConfig) { logger.error(`Config file missing`); throw new errors_js_1.MissingConfigError(); } const branches = pullRequests.map(pr => pr.base.ref); const firstBranch = branches[0]; const fallbackBranch = firstBranch || before; let isLegacyConfig = false; logger.info(`fallback branch from Pull Requests: ${firstBranch}`); logger.info(`SHA before push: ${before}`); // on non-environment related PRs, use a branch from first associated pull request const config = (0, config_js_1.createConfig)(rawConfig, configKind => { isLegacyConfig = configKind === 'legacy'; }, branches, fallbackBranch); if (!config.diff) { logger.info(`disabled. Skipping...`); yield (0, check_runs_js_1.complete)({ owner, repo, checkRunId, context, conclusion: types_js_1.CheckConclusion.Success, logger, }); return; } logger.info(`enabled`); if (!config.branch || /^[0]+$/.test(config.branch)) { logger.info(`Nothing to compare with. Skipping...`); yield (0, check_runs_js_1.complete)({ owner, repo, checkRunId, context, conclusion: types_js_1.CheckConclusion.Success, logger, }); return; } if (config.diff.experimental_merge !== false) { if (!pullRequestNumber && (pullRequests === null || pullRequests === void 0 ? void 0 : pullRequests.length)) { pullRequestNumber = pullRequests[0].number; } if (pullRequestNumber) { ref = `refs/pull/${pullRequestNumber}/merge`; logger.info(`Using Pull Request: ${ref}`); } } const oldPointer = { path: config.schema, ref: config.branch, }; const newPointer = { path: oldPointer.path, ref, }; if (oldPointer.ref === config_js_1.defaultFallbackBranch) { logger.error('used default ref to get old schema'); } if (newPointer.ref === config_js_1.defaultFallbackBranch) { logger.error('used default ref to get new schema'); } const sources = yield (0, loaders_js_1.loadSources)({ config, oldPointer, newPointer, loadFile, }); const schemas = { old: (0, schema_js_1.produceSchema)(sources.old), new: (0, schema_js_1.produceSchema)(sources.new), }; logger.info(`built schemas`); const action = yield (0, diff_js_1.diff)({ path: config.schema, schemas, sources, }); logger.info(`schema diff result is ready`); let conclusion = action.conclusion; let annotations = action.annotations || []; const changes = action.changes || []; logger.info(`changes - ${changes.length}`); logger.info(`annotations - ${changes.length}`); const summaryLimit = config.diff.summaryLimit || 100; const summary = (0, utils_js_1.createSummary)(changes, summaryLimit, isLegacyConfig); const approveLabelName = config.diff.approveLabel || 'approved-breaking-change'; const hasApprovedBreakingChangeLabel = pullRequestNumber ? (_a = pullRequests[0].labels) === null || _a === void 0 ? void 0 : _a.find(label => label.name === approveLabelName) : false; // Force Success when failOnBreaking is disabled if (config.diff.failOnBreaking === false || hasApprovedBreakingChangeLabel) { logger.info('FailOnBreaking disabled. Forcing SUCCESS'); conclusion = types_js_1.CheckConclusion.Success; } const title = conclusion === types_js_1.CheckConclusion.Failure ? 'Something is wrong with your schema' : 'Everything looks good'; if (config.diff.annotations === false) { logger.info(`Anotations are disabled. Skipping annotations...`); annotations = []; } else if (annotations.length > summaryLimit) { logger.info(`Total amount of annotations is over the limit (${annotations.length} > ${summaryLimit}). Skipping annotations...`); annotations = []; } else { logger.info(`Sending annotations (${annotations.length})`); } yield (0, check_runs_js_1.annotate)({ owner, repo, checkRunId, context, title, summary, annotations, logger, }); logger.info(`Finishing check (${conclusion})`); yield (0, check_runs_js_1.complete)({ owner, repo, checkRunId, context, conclusion, logger, }); logger.info(`done`); } catch (error) { logger.error(error); if (!(error instanceof errors_js_1.MissingConfigError)) { onError(error); } yield (0, check_runs_js_1.annotate)({ owner, repo, checkRunId, context, title: `Failed to complete schema check`, summary: `ERROR: ${error.message || error}`, annotations: [], logger, }); yield (0, check_runs_js_1.complete)({ owner, repo, checkRunId, context, conclusion: types_js_1.CheckConclusion.Failure, logger, }); } }); } exports.handleSchemaDiff = handleSchemaDiff;