UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

118 lines (117 loc) 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bolderize = bolderize; exports.quotesTransformer = quotesTransformer; exports.slackCoderize = slackCoderize; exports.discordCoderize = discordCoderize; exports.filterChangesByLevel = filterChangesByLevel; exports.createSummary = createSummary; exports.isNil = isNil; exports.parseEndpoint = parseEndpoint; exports.batch = batch; exports.objectFromEntries = objectFromEntries; const core_1 = require("@graphql-inspector/core"); function bolderize(msg) { return quotesTransformer(msg, '**'); } function quotesTransformer(msg, symbols = '**') { const findSingleQuotes = /'([^']+)'/gim; const findDoubleQuotes = /"([^"]+)"/gim; function transformm(_, value) { return `${symbols}${value}${symbols}`; } return msg.replace(findSingleQuotes, transformm).replace(findDoubleQuotes, transformm); } function slackCoderize(msg) { return quotesTransformer(msg, '`'); } function discordCoderize(msg) { return quotesTransformer(msg, '`'); } function filterChangesByLevel(level) { return (change) => change.criticality.level === level; } function createSummary(changes, summaryLimit, isLegacyConfig = false) { const breakingChanges = changes.filter(filterChangesByLevel(core_1.CriticalityLevel.Breaking)); const dangerousChanges = changes.filter(filterChangesByLevel(core_1.CriticalityLevel.Dangerous)); const safeChanges = changes.filter(filterChangesByLevel(core_1.CriticalityLevel.NonBreaking)); const summary = [ `# Found ${changes.length} change${changes.length > 1 ? 's' : ''}`, '', `Breaking: ${breakingChanges.length}`, `Dangerous: ${dangerousChanges.length}`, `Safe: ${safeChanges.length}`, ]; if (isLegacyConfig) { summary.push([ '', '> Legacy config detected, [please migrate to a new syntax](https://graphql-inspector.com/docs/products/github#full-configuration)', '', ].join('\n')); } if (changes.length > summaryLimit) { summary.push([ '', `Total amount of changes (${changes.length}) is over the limit (${summaryLimit})`, 'Adjust it using "summaryLimit" option', '', ].join('\n')); } function addChangesToSummary(type, changes) { if (changes.length <= summaryLimit) { summary.push(...['', `## ${type} changes`].concat(changes.map(change => ` - ${bolderize(change.message)}`))); } summaryLimit -= changes.length; } if (breakingChanges.length) { addChangesToSummary('Breaking', breakingChanges); } if (dangerousChanges.length) { addChangesToSummary('Dangerous', dangerousChanges); } if (safeChanges.length) { addChangesToSummary('Safe', safeChanges); } summary.push([ '', '___', `Looking for more advanced tool? Try [GraphQL Hive](https://graphql-hive.com)!`, ].join('\n')); return summary.join('\n'); } function isNil(val) { return !val && typeof val !== 'boolean'; } function parseEndpoint(endpoint) { if (typeof endpoint === 'string') { return { url: endpoint, method: 'POST', }; } return { url: endpoint.url, method: endpoint.method || 'POST', headers: endpoint.headers, }; } function batch(items, limit) { const batches = []; const batchesNum = Math.ceil(items.length / limit); // We still want to update check-run and send empty annotations if (batchesNum === 0) { return [[]]; } for (let i = 0; i < batchesNum; i++) { const start = i * limit; const end = start + limit; batches.push(items.slice(start, end)); } return batches; } function objectFromEntries(iterable) { return [...iterable].reduce((obj, [key, val]) => { obj[key] = val; return obj; }, {}); }