UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

125 lines (124 loc) 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleSchemaChangeNotifications = void 0; const tslib_1 = require("tslib"); const graphql_1 = require("graphql"); const core_1 = require("@graphql-inspector/core"); const config_js_1 = require("./helpers/config.js"); const loaders_js_1 = require("./helpers/loaders.js"); const logger_js_1 = require("./helpers/logger.js"); const notifications_js_1 = require("./helpers/notifications.js"); function handleSchemaChangeNotifications({ context, ref, repo, owner, before, loadFile, loadConfig, onError, release, action, }) { var _a, _b; return tslib_1.__awaiter(this, void 0, void 0, function* () { const id = `${owner}/${repo}#${ref}`; const logger = (0, logger_js_1.createLogger)('NOTIFICATIONS', context, release); logger.info(`started - ${id}`); logger.info(`action - ${action}`); const isBranchPush = ref.startsWith('refs/heads/'); if (!isBranchPush) { logger.warn(`Received Push event is not a branch push event (ref "${ref}")`); return; } const rawConfig = yield loadConfig(); if (!rawConfig) { logger.error(`Missing config file`); return; } const branch = ref.replace('refs/heads/', ''); // eslint-disable-next-line @typescript-eslint/no-empty-function const config = (0, config_js_1.createConfig)(rawConfig, () => { }, [branch]); if (!config.notifications) { logger.info(`disabled. Skipping...`); return; } logger.info(`enabled`); if (config.branch !== branch) { logger.info(`Received branch "${branch}" doesn't match expected branch "${config.branch}". Skipping...`); return; } const oldPointer = { path: config.schema, ref: before, }; const newPointer = { path: config.schema, ref, }; const sources = yield (0, loaders_js_1.loadSources)({ config, oldPointer, newPointer, loadFile, }); const schemas = { old: (0, graphql_1.buildSchema)(sources.old, { assumeValid: true, assumeValidSDL: true, }), new: (0, graphql_1.buildSchema)(sources.new, { assumeValid: true, assumeValidSDL: true, }), }; logger.info(`built schemas`); const changes = yield (0, core_1.diff)(schemas.old, schemas.new); if (!changes.length) { logger.info(`schemas are equal. Skipping...`); return; } const notifications = config.notifications; function actionRunner(target, fn) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { yield fn(); } catch (error) { onError(error); logger.error(`Failed to send a notification via ${target}`, error); } }); } if (hasNotificationsEnabled(notifications)) { const actions = []; const commit = (_b = (_a = context.payload.commits) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.id; if (notifications.slack) { actions.push(actionRunner('slack', () => (0, notifications_js_1.notifyWithSlack)({ url: notifications.slack, changes, environment: config.name, repo, owner, commit, }))); } if (notifications.discord) { actions.push(actionRunner('discord', () => (0, notifications_js_1.notifyWithDiscord)({ url: notifications.discord, changes, environment: config.name, repo, owner, commit, }))); } if (notifications.webhook) { actions.push(actionRunner('webhook', () => (0, notifications_js_1.notifyWithWebhook)({ url: notifications.webhook, changes, environment: config.name, repo, owner, commit, }))); } if (actions.length) { yield Promise.all(actions); } } }); } exports.handleSchemaChangeNotifications = handleSchemaChangeNotifications; function hasNotificationsEnabled(notifications) { return notifications && typeof notifications === 'object'; }