@immobiliarelabs/backstage-plugin-gitlab-backend
Version:
> Backstage plugin to interact with GitLab
138 lines (132 loc) • 4.82 kB
JavaScript
;
var rootHttpRouter = require('@backstage/backend-defaults/rootHttpRouter');
var integration = require('@backstage/integration');
var bodyParser = require('body-parser');
var express = require('express');
var httpProxyMiddleware = require('http-proxy-middleware');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
var bodyParser__default = /*#__PURE__*/_interopDefaultCompat(bodyParser);
function getBasePath(config) {
const baseUrl = config.getOptionalString("backend.baseUrl");
if (!baseUrl) {
return undefined;
}
return new URL(baseUrl).pathname.replace(/\/$/, "");
}
function headersManipulation(headers) {
if (headers["authorization"]) delete headers["authorization"];
if (headers["gitlab-authorization"]) {
headers["authorization"] = headers["gitlab-authorization"];
delete headers["gitlab-authorization"];
}
}
async function createRouter(options) {
const { logger, config } = options;
const secure = config.getOptionalBoolean("gitlab.proxySecure");
const useOAuth = config.getOptionalBoolean("gitlab.useOAuth");
const basePath = getBasePath(config) || "";
const gitlabIntegrations = integration.readGitLabIntegrationConfigs(
config.getConfigArray("integrations.gitlab")
);
const router = express.Router();
router.use(bodyParser__default.default.json());
router.use(bodyParser__default.default.urlencoded({ extended: true }));
router.use(bodyParser__default.default.text());
for (const { host, apiBaseUrl, token } of gitlabIntegrations) {
const apiUrl = new URL(apiBaseUrl);
router.use(
`/graphql/${host}`,
httpProxyMiddleware.createProxyMiddleware(
(_pathname, req) => {
headersManipulation(req.headers);
const isPost = req.method === "POST";
const isNotMutation = !req.body?.query?.includes(
"mutation"
);
return isPost && isNotMutation;
},
{
target: apiUrl.origin,
changeOrigin: true,
headers: {
...token && !useOAuth ? { "PRIVATE-TOKEN": token } : {}
},
secure,
onProxyReq: (proxyReq, req) => {
headersManipulation(req.headers);
if (req.headers.authorization) {
proxyReq.setHeader(
"authorization",
req.headers.authorization
);
}
const body = req.body || {};
const bodyData = JSON.stringify(body);
proxyReq.setHeader("Content-Type", "application/json");
proxyReq.setHeader(
"Content-Length",
Buffer.byteLength(bodyData)
);
proxyReq.write(bodyData);
},
logProvider: () => ({
log: logger.info.bind(logger),
debug: logger.debug.bind(logger),
info: logger.info.bind(logger),
warn: logger.warn.bind(logger),
error: logger.error.bind(logger)
}),
pathRewrite: (path, _req) => {
const from = `${basePath}/api/gitlab/graphql/${host}`;
const to = "/api/graphql";
if (path.startsWith(from))
return path.replace(from, to);
const relativeFrom = `/graphql/${host}`;
if (path.startsWith(relativeFrom))
return path.replace(relativeFrom, to);
return path;
}
}
)
);
router.use(
`/rest/${host}`,
httpProxyMiddleware.createProxyMiddleware(
(_pathname, req) => {
headersManipulation(req.headers);
return req.method === "GET";
},
{
target: apiUrl.origin,
changeOrigin: true,
headers: {
...token && !useOAuth ? { "PRIVATE-TOKEN": token } : {}
},
secure,
logProvider: () => ({
log: logger.info.bind(logger),
debug: logger.debug.bind(logger),
info: logger.info.bind(logger),
warn: logger.warn.bind(logger),
error: logger.error.bind(logger)
}),
pathRewrite: (path, _req) => {
const from = `${basePath}/api/gitlab/rest/${host}`;
const to = apiUrl.pathname;
if (path.startsWith(from))
return path.replace(from, to);
const relativeFrom = `/rest/${host}`;
if (path.startsWith(relativeFrom))
return path.replace(relativeFrom, to);
return path;
}
}
)
);
}
const middleware = rootHttpRouter.MiddlewareFactory.create({ logger, config });
router.use(middleware.error());
return router;
}
exports.createRouter = createRouter;
//# sourceMappingURL=router.cjs.js.map