@graphql-inspector/action
Version:
GraphQL Inspector functionality for GitHub Actions
141 lines (140 loc) • 5.04 kB
JavaScript
import { CriticalityLevel } from '@graphql-inspector/core';
import { fetch } from '@whatwg-node/fetch';
import { defaultConfigName } from './config.js';
import { discordCoderize, filterChangesByLevel, slackCoderize } from './utils.js';
export async function notifyWithWebhook({ url, changes, environment, repo, owner, commit, }) {
const event = {
repo,
owner,
commit,
environment: environment && environment !== defaultConfigName ? environment : 'default',
changes: changes.map(change => ({
message: change.message,
level: change.criticality.level,
})),
};
await fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(event),
});
}
export 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 fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(event),
});
}
export 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 fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(event),
});
}
function createAttachments(changes) {
const breakingChanges = changes.filter(filterChangesByLevel(CriticalityLevel.Breaking));
const dangerousChanges = changes.filter(filterChangesByLevel(CriticalityLevel.Dangerous));
const safeChanges = changes.filter(filterChangesByLevel(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 => slackCoderize(change.message)).join('\n');
return {
mrkdwn_in: ['text', 'fallback'],
color,
author_name: title,
text,
fallback: text,
};
}
function createDiscordEmbeds(changes) {
const breakingChanges = changes.filter(filterChangesByLevel(CriticalityLevel.Breaking));
const dangerousChanges = changes.filter(filterChangesByLevel(CriticalityLevel.Dangerous));
const safeChanges = changes.filter(filterChangesByLevel(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 => discordCoderize(change.message)).join('\n');
return {
color,
title,
description,
};
}
function pluralize(word, num) {
return word + (num > 1 ? 's' : '');
}