UNPKG

@backstage/integration

Version:

Helpers for managing integrations towards external systems

63 lines (60 loc) 1.94 kB
import { trimEnd } from 'lodash'; import { isValidHost, isValidUrl } from '../helpers.esm.js'; const GITLAB_HOST = "gitlab.com"; const GITLAB_API_BASE_URL = "https://gitlab.com/api/v4"; function readGitLabIntegrationConfig(config) { const host = config.getString("host"); let apiBaseUrl = config.getOptionalString("apiBaseUrl"); const token = config.getOptionalString("token")?.trim(); let baseUrl = config.getOptionalString("baseUrl"); if (apiBaseUrl) { apiBaseUrl = trimEnd(apiBaseUrl, "/"); } else if (host === GITLAB_HOST) { apiBaseUrl = GITLAB_API_BASE_URL; } if (baseUrl) { baseUrl = trimEnd(baseUrl, "/"); } else { baseUrl = `https://${host}`; } if (!isValidHost(host)) { throw new Error( `Invalid GitLab integration config, '${host}' is not a valid host` ); } else if (!apiBaseUrl || !isValidUrl(apiBaseUrl)) { throw new Error( `Invalid GitLab integration config, '${apiBaseUrl}' is not a valid apiBaseUrl` ); } else if (!isValidUrl(baseUrl)) { throw new Error( `Invalid GitLab integration config, '${baseUrl}' is not a valid baseUrl` ); } return { host, token, apiBaseUrl, baseUrl, commitSigningKey: config.getOptionalString("commitSigningKey") }; } function readGitLabIntegrationConfigs(configs) { const result = configs.map(readGitLabIntegrationConfig); if (!result.some((c) => c.host === GITLAB_HOST)) { result.push({ host: GITLAB_HOST, apiBaseUrl: GITLAB_API_BASE_URL, baseUrl: `https://${GITLAB_HOST}` }); } return result; } function getGitLabIntegrationRelativePath(config) { let relativePath = ""; if (config.host !== GITLAB_HOST) { relativePath = new URL(config.baseUrl).pathname; } return trimEnd(relativePath, "/"); } export { getGitLabIntegrationRelativePath, readGitLabIntegrationConfig, readGitLabIntegrationConfigs }; //# sourceMappingURL=config.esm.js.map