@graphql-inspector/action
Version:
GraphQL Inspector functionality for GitHub Actions
69 lines (68 loc) • 2.56 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.diff = diff;
const core_1 = require("@graphql-inspector/core");
const fetch_1 = require("@whatwg-node/fetch");
const location_js_1 = require("./location.js");
const types_js_1 = require("./types.js");
const utils_js_1 = require("./utils.js");
async function diff({ path, schemas, sources, interceptor, pullRequests, ref, rules, config, }) {
let changes = await (0, core_1.diff)(schemas.old, schemas.new, rules, config);
let forcedConclusion = null;
if (!changes?.length) {
return {
conclusion: types_js_1.CheckConclusion.Success,
};
}
if (!(0, utils_js_1.isNil)(interceptor)) {
const interceptionResult = await interceptChanges(interceptor, {
pullRequests,
ref,
changes,
});
changes = interceptionResult.changes || [];
forcedConclusion = interceptionResult.conclusion || null;
}
const annotations = await Promise.all(changes.map(change => annotate({ path, change, source: sources.new })));
let conclusion = types_js_1.CheckConclusion.Success;
if (changes.some(change => change.criticality.level === core_1.CriticalityLevel.Breaking)) {
conclusion = types_js_1.CheckConclusion.Failure;
}
if (forcedConclusion) {
conclusion = forcedConclusion;
}
return {
conclusion,
annotations,
changes,
};
}
const levelMap = {
[core_1.CriticalityLevel.Breaking]: types_js_1.AnnotationLevel.Failure,
[core_1.CriticalityLevel.Dangerous]: types_js_1.AnnotationLevel.Warning,
[core_1.CriticalityLevel.NonBreaking]: types_js_1.AnnotationLevel.Notice,
};
function annotate({ path, change, source, }) {
const level = change.criticality.level;
const loc = change.path
? (0, location_js_1.getLocationByPath)({ path: change.path, source })
: { line: 1, column: 1 };
return {
title: change.message,
annotation_level: levelMap[level],
path,
message: change.criticality.reason || change.message,
start_line: loc.line,
end_line: loc.line,
};
}
async function interceptChanges(interceptor, payload) {
const endpoint = (0, utils_js_1.parseEndpoint)(interceptor);
const response = await (0, fetch_1.fetch)(endpoint.url, {
method: endpoint.method,
body: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' },
});
const data = await response.json();
return data;
}
;