@graphql-inspector/action
Version:
GraphQL Inspector functionality for GitHub Actions
184 lines (183 loc) • 7.36 kB
JavaScript
import { __awaiter } from "tslib";
import { annotate, complete, start } from './helpers/check-runs.js';
import { createConfig, defaultFallbackBranch } from './helpers/config.js';
import { diff } from './helpers/diff.js';
import { MissingConfigError } from './helpers/errors.js';
import { loadSources } from './helpers/loaders.js';
import { createLogger } from './helpers/logger.js';
import { produceSchema } from './helpers/schema.js';
import { CheckConclusion } from './helpers/types.js';
import { createSummary } from './helpers/utils.js';
export function handleSchemaDiff({ release, action, context, ref, pullRequestNumber, repo, owner, before, pullRequests = [], loadFile, loadConfig, onError, }) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const id = `${owner}/${repo}#${ref}`;
const logger = createLogger('DIFF', context, release);
logger.info(`Started - ${id}`);
logger.info(`Action: "${action}"`);
const checkRunId = yield 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 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 = createConfig(rawConfig, configKind => {
isLegacyConfig = configKind === 'legacy';
}, branches, fallbackBranch);
if (!config.diff) {
logger.info(`disabled. Skipping...`);
yield complete({
owner,
repo,
checkRunId,
context,
conclusion: CheckConclusion.Success,
logger,
});
return;
}
logger.info(`enabled`);
if (!config.branch || /^[0]+$/.test(config.branch)) {
logger.info(`Nothing to compare with. Skipping...`);
yield complete({
owner,
repo,
checkRunId,
context,
conclusion: 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 === defaultFallbackBranch) {
logger.error('used default ref to get old schema');
}
if (newPointer.ref === defaultFallbackBranch) {
logger.error('used default ref to get new schema');
}
const sources = yield loadSources({
config,
oldPointer,
newPointer,
loadFile,
});
const schemas = {
old: produceSchema(sources.old),
new: produceSchema(sources.new),
};
logger.info(`built schemas`);
const action = yield 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 = 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 = CheckConclusion.Success;
}
const title = conclusion === 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 annotate({
owner,
repo,
checkRunId,
context,
title,
summary,
annotations,
logger,
});
logger.info(`Finishing check (${conclusion})`);
yield complete({
owner,
repo,
checkRunId,
context,
conclusion,
logger,
});
logger.info(`done`);
}
catch (error) {
logger.error(error);
if (!(error instanceof MissingConfigError)) {
onError(error);
}
yield annotate({
owner,
repo,
checkRunId,
context,
title: `Failed to complete schema check`,
summary: `ERROR: ${error.message || error}`,
annotations: [],
logger,
});
yield complete({
owner,
repo,
checkRunId,
context,
conclusion: CheckConclusion.Failure,
logger,
});
}
});
}