UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

150 lines (149 loc) 5.08 kB
import { isNil } from './utils.js'; export const defaultConfigName = '__default'; export const defaultFallbackBranch = '*'; const diffDefault = { annotations: true, failOnBreaking: true, }; const notificationsDefault = false; function normalizeConfig(config) { if (isLegacyConfig(config)) { console.log('config type - "legacy"'); return { kind: 'legacy', config: { [defaultConfigName]: { name: defaultConfigName, schema: config.schema.path, branch: config.schema.ref, endpoint: config.endpoint, notifications: prioritize(config.notifications, notificationsDefault), diff: prioritize(config.diff, diffDefault), }, }, }; } if (isSingleEnvironmentConfig(config)) { console.log('config type - "single"'); return { kind: 'single', config: { [config.branch]: { name: config.branch, schema: config.schema, branch: config.branch, endpoint: config.endpoint, notifications: prioritize(config.notifications, notificationsDefault), diff: prioritize(config.diff, diffDefault), }, }, }; } if (isMultipleEnvironmentConfig(config)) { console.log('config type - "multiple"'); const normalized = {}; for (const envName in config.env) { if (Object.prototype.hasOwnProperty.call(config.env, envName)) { const env = config.env[envName]; normalized[envName] = { name: envName, schema: config.schema, branch: env.branch, endpoint: env.endpoint, diff: prioritize(env.diff, config.diff, diffDefault), notifications: prioritize(env.notifications, config.notifications, notificationsDefault), }; } } return { kind: 'multiple', config: normalized, }; } throw new Error('Invalid configuration'); } function getGlobalConfig(config, fallbackBranch) { return { name: 'global', schema: config.schema, branch: fallbackBranch, notifications: false, // notifications should be disabled for non-environment commits diff: prioritize(config.others?.diff, config.diff, diffDefault), }; } export function createConfig(rawConfig, setConfigKind, branches = [], fallbackBranch = defaultFallbackBranch) { const { config: normalizedConfig, kind: configKind } = normalizeConfig(rawConfig); let config = null; setConfigKind(configKind); if (isNormalizedLegacyConfig(normalizedConfig)) { config = normalizedConfig[defaultConfigName]; if (branches.includes(config.branch) === false) { config.endpoint = undefined; } return config; } for (const branch of branches) { if (config == null) { config = findConfigByBranch(branch, normalizedConfig, false); } if (config) { break; } } if (config == null) { config = getGlobalConfig(rawConfig, fallbackBranch); } return config; } function isNormalizedLegacyConfig(config) { return typeof config[defaultConfigName] === 'object'; } function isLegacyConfig(config) { return config.schema && typeof config.schema === 'object'; } function isSingleEnvironmentConfig(config) { return !config.env; } function isMultipleEnvironmentConfig(config) { return !isLegacyConfig(config) && !isSingleEnvironmentConfig(config); } function findConfigByBranch(branch, config, throwOnMissing = true) { const branches = []; for (const name in config) { if (Object.prototype.hasOwnProperty.call(config, name)) { const env = config[name]; if (env.branch === branch) { return env; } branches.push(env.branch); } } if (throwOnMissing) { throw new Error(`Couldn't match branch "${branch}" with branches in config. Available branches: ${branches.join(', ')}`); } return null; } // I'm not very proud of it :) function prioritize(child, parent, defaults) { if (child === false) { return false; } if (child === true || isNil(child)) { if (parent === true || isNil(parent)) { return defaults || false; } return typeof parent === 'object' && typeof defaults === 'object' ? { ...defaults, ...parent } : parent; } if (parent && typeof parent === 'object') { return { ...defaults, ...parent, ...child, }; } return typeof child === 'object' && typeof defaults === 'object' ? { ...defaults, ...child } : child; }