@graphql-inspector/cli
Version:
Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.
146 lines (145 loc) • 5.4 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.notifyWithWebhook = notifyWithWebhook;
exports.notifyWithSlack = notifyWithSlack;
exports.notifyWithDiscord = notifyWithDiscord;
const core_1 = require("@graphql-inspector/core");
const fetch_1 = require("@whatwg-node/fetch");
const config_js_1 = require("./config.js");
const utils_js_1 = require("./utils.js");
async function notifyWithWebhook({ url, changes, environment, repo, owner, commit, }) {
const event = {
repo,
owner,
commit,
environment: environment && environment !== config_js_1.defaultConfigName ? environment : 'default',
changes: changes.map(change => ({
message: change.message,
level: change.criticality.level,
})),
};
await (0, fetch_1.fetch)(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(event),
});
}
async function notifyWithSlack({ url, changes, environment, repo, owner, commit, }) {
const totalChanges = changes.length;
const schemaName = environment ? `${environment} schema` : `schema`;
const sourceLink = commit
? ` (<https://github.com/${owner}/${repo}/commit/${commit}|\`${commit.substr(0, 7)}\`>)`
: '';
const event = {
username: 'GraphQL Inspector',
icon_url: 'https://graphql-inspector/img/logo-slack.png',
text: `:male-detective: Hi, I found *${totalChanges} ${pluralize('change', totalChanges)}* in ${schemaName}${sourceLink}:`,
attachments: createAttachments(changes),
};
await (0, fetch_1.fetch)(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(event),
});
}
async function notifyWithDiscord({ url, changes, environment, repo, owner, commit, }) {
const totalChanges = changes.length;
const schemaName = environment ? `${environment} schema` : `schema`;
const sourceLink = commit
? ` ([\`${commit.substr(0, 7)}\`](https://github.com/${owner}/${repo}/commit/${commit}))`
: '';
const event = {
username: 'GraphQL Inspector',
avatar_url: 'https://graphql-inspector/img/logo-slack.png',
content: `:detective: Hi, I found **${totalChanges} ${pluralize('change', totalChanges)}** in ${schemaName}${sourceLink}:`,
embeds: createDiscordEmbeds(changes),
};
await (0, fetch_1.fetch)(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(event),
});
}
function createAttachments(changes) {
const breakingChanges = changes.filter((0, utils_js_1.filterChangesByLevel)(core_1.CriticalityLevel.Breaking));
const dangerousChanges = changes.filter((0, utils_js_1.filterChangesByLevel)(core_1.CriticalityLevel.Dangerous));
const safeChanges = changes.filter((0, utils_js_1.filterChangesByLevel)(core_1.CriticalityLevel.NonBreaking));
const attachments = [];
if (breakingChanges.length) {
attachments.push(renderAttachments({
color: '#E74C3B',
title: 'Breaking changes',
changes: breakingChanges,
}));
}
if (dangerousChanges.length) {
attachments.push(renderAttachments({
color: '#F0C418',
title: 'Dangerous changes',
changes: dangerousChanges,
}));
}
if (safeChanges.length) {
attachments.push(renderAttachments({
color: '#23B99A',
title: 'Safe changes',
changes: safeChanges,
}));
}
return attachments;
}
function renderAttachments({ changes, title, color, }) {
const text = changes.map(change => (0, utils_js_1.slackCoderize)(change.message)).join('\n');
return {
mrkdwn_in: ['text', 'fallback'],
color,
author_name: title,
text,
fallback: text,
};
}
function createDiscordEmbeds(changes) {
const breakingChanges = changes.filter((0, utils_js_1.filterChangesByLevel)(core_1.CriticalityLevel.Breaking));
const dangerousChanges = changes.filter((0, utils_js_1.filterChangesByLevel)(core_1.CriticalityLevel.Dangerous));
const safeChanges = changes.filter((0, utils_js_1.filterChangesByLevel)(core_1.CriticalityLevel.NonBreaking));
const embeds = [];
if (breakingChanges.length) {
embeds.push(renderDiscordEmbed({
color: 15_158_331, // '#E74C3B',
title: 'Breaking changes',
changes: breakingChanges,
}));
}
if (dangerousChanges.length) {
embeds.push(renderDiscordEmbed({
color: 15_778_840, // '#F0C418',
title: 'Dangerous changes',
changes: dangerousChanges,
}));
}
if (safeChanges.length) {
embeds.push(renderDiscordEmbed({
color: 2_341_274, // '#23B99A',
title: 'Safe changes',
changes: safeChanges,
}));
}
return embeds;
}
function renderDiscordEmbed({ changes, title, color, }) {
const description = changes.map(change => (0, utils_js_1.discordCoderize)(change.message)).join('\n');
return {
color,
title,
description,
};
}
function pluralize(word, num) {
return word + (num > 1 ? 's' : '');
}
;