@backstage/integration
Version:
Helpers for managing integrations towards external systems
67 lines (63 loc) • 2.09 kB
JavaScript
;
var lodash = require('lodash');
var helpers = require('../helpers.cjs.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 = lodash.trimEnd(apiBaseUrl, "/");
} else if (host === GITLAB_HOST) {
apiBaseUrl = GITLAB_API_BASE_URL;
}
if (baseUrl) {
baseUrl = lodash.trimEnd(baseUrl, "/");
} else {
baseUrl = `https://${host}`;
}
if (!helpers.isValidHost(host)) {
throw new Error(
`Invalid GitLab integration config, '${host}' is not a valid host`
);
} else if (!apiBaseUrl || !helpers.isValidUrl(apiBaseUrl)) {
throw new Error(
`Invalid GitLab integration config, '${apiBaseUrl}' is not a valid apiBaseUrl`
);
} else if (!helpers.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 lodash.trimEnd(relativePath, "/");
}
exports.getGitLabIntegrationRelativePath = getGitLabIntegrationRelativePath;
exports.readGitLabIntegrationConfig = readGitLabIntegrationConfig;
exports.readGitLabIntegrationConfigs = readGitLabIntegrationConfigs;
//# sourceMappingURL=config.cjs.js.map