From 31d05b1f922ce0b7ea0fe03b40a96a8ca1e8b75b Mon Sep 17 00:00:00 2001 From: Ivan Shmidt Date: Mon, 4 May 2020 20:58:39 +0200 Subject: [PATCH] feat: circleCI backend proxy --- packages/app/package.json | 1 + packages/app/src/plugins.ts | 1 + packages/backend/package.json | 1 + packages/backend/src/index.ts | 2 ++ plugins/circleci-backend/.eslintrc.js | 3 +++ plugins/circleci-backend/README.md | 6 ++++++ plugins/circleci-backend/package.json | 23 ++++++++++++++++++++++ plugins/circleci-backend/src/index.ts | 27 ++++++++++++++++++++++++++ plugins/circleci-backend/tsconfig.json | 15 ++++++++++++++ plugins/circleci/src/api/index.ts | 11 +++++------ yarn.lock | 15 ++++++++++++-- 11 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 plugins/circleci-backend/.eslintrc.js create mode 100644 plugins/circleci-backend/README.md create mode 100644 plugins/circleci-backend/package.json create mode 100644 plugins/circleci-backend/src/index.ts create mode 100644 plugins/circleci-backend/tsconfig.json diff --git a/packages/app/package.json b/packages/app/package.json index da1d02a5be..e88bd46a54 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -21,6 +21,7 @@ "@backstage/cli": "^0.1.1-alpha.4", "@backstage/core": "^0.1.1-alpha.4", "@backstage/plugin-circleci": "^0.1.1-alpha.4", + "@backstage/plugin-circleci-backend": "^0.1.1-alpha.4", "@backstage/plugin-home-page": "^0.1.1-alpha.4", "@backstage/plugin-inventory": "^0.1.1-alpha.4", "@backstage/plugin-lighthouse": "^0.1.1-alpha.4", diff --git a/packages/app/src/plugins.ts b/packages/app/src/plugins.ts index 31546c6ba6..b46bf2ace8 100644 --- a/packages/app/src/plugins.ts +++ b/packages/app/src/plugins.ts @@ -19,3 +19,4 @@ export { plugin as LighthousePlugin } from '@backstage/plugin-lighthouse'; export { plugin as InventoryPlugin } from '@backstage/plugin-inventory'; export { plugin as TechRadar } from '@backstage/plugin-tech-radar'; export { plugin as Circleci } from '@backstage/plugin-circleci'; +// export { plugin as CircleciBackend } from '@backstage/plugin-circleci-backend'; diff --git a/packages/backend/package.json b/packages/backend/package.json index 0530f04452..13d64a0a13 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -16,6 +16,7 @@ }, "dependencies": { "@backstage/plugin-inventory-backend": "0.1.1-alpha.4", + "@backstage/plugin-circleci-backend": "0.1.1-alpha.4", "compression": "^1.7.4", "cors": "^2.8.5", "express": "^4.17.1", diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index fd8015c328..da1a4f3dc3 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -28,6 +28,7 @@ import helmet from 'helmet'; import compression from 'compression'; import { testRouter } from './test'; import { router as inventoryRouter } from '@backstage/plugin-inventory-backend'; +import { router as circleCIRouter } from '@backstage/plugin-circleci-backend'; const DEFAULT_PORT = 7000; @@ -40,6 +41,7 @@ app.use(compression()); app.use(express.json()); app.use('/test', testRouter); app.use('/inventory', inventoryRouter); +app.use('/circleci', circleCIRouter); app.listen(PORT, () => { console.log(`Listening on port ${PORT}`); diff --git a/plugins/circleci-backend/.eslintrc.js b/plugins/circleci-backend/.eslintrc.js new file mode 100644 index 0000000000..13573efa9c --- /dev/null +++ b/plugins/circleci-backend/.eslintrc.js @@ -0,0 +1,3 @@ +module.exports = { + extends: [require.resolve('@backstage/cli/config/eslint')], +}; diff --git a/plugins/circleci-backend/README.md b/plugins/circleci-backend/README.md new file mode 100644 index 0000000000..91e60e3e6b --- /dev/null +++ b/plugins/circleci-backend/README.md @@ -0,0 +1,6 @@ +# Title +Welcome to the circleci-backend plugin! + +## Sub-section 1 + +## Sub-section 2 diff --git a/plugins/circleci-backend/package.json b/plugins/circleci-backend/package.json new file mode 100644 index 0000000000..6535deba10 --- /dev/null +++ b/plugins/circleci-backend/package.json @@ -0,0 +1,23 @@ +{ + "name": "@backstage/plugin-circleci-backend", + "version": "0.1.1-alpha.4", + "main": "dist", + "license": "Apache-2.0", + "private": true, + "scripts": { + "build": "tsc", + "lint": "backstage-cli lint", + "test": "backstage-cli test", + "clean": "backstage-cli clean" + }, + "devDependencies": { + "@backstage/cli": "^0.1.1-alpha.4" + }, + "dependencies": { + "express": "^4.17.1", + "http-proxy-middleware": "^1.0.3" + }, + "files": [ + "dist" + ] +} diff --git a/plugins/circleci-backend/src/index.ts b/plugins/circleci-backend/src/index.ts new file mode 100644 index 0000000000..45bf47b884 --- /dev/null +++ b/plugins/circleci-backend/src/index.ts @@ -0,0 +1,27 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import express from 'express'; +import httpProxy from 'http-proxy'; + +// Simple proxy for handling CORS for now +const proxy = httpProxy.createServer({ + target: 'https://circleci.com/api/v1.1', + changeOrigin: true, +}); +proxy.on('error', e => console.error(e)); +export const router = express(); +router.use('/api', (req, res) => proxy.web(req, res)); diff --git a/plugins/circleci-backend/tsconfig.json b/plugins/circleci-backend/tsconfig.json new file mode 100644 index 0000000000..b463ac102f --- /dev/null +++ b/plugins/circleci-backend/tsconfig.json @@ -0,0 +1,15 @@ +{ + "include": ["src"], + "compilerOptions": { + "baseUrl": "src", + "outDir": "dist", + "incremental": true, + "sourceMap": true, + "declaration": true, + "strict": true, + "target": "es5", + "module": "commonjs", + "esModuleInterop": true, + "types": ["node", "jest"] + } +} diff --git a/plugins/circleci/src/api/index.ts b/plugins/circleci/src/api/index.ts index 70068f29ba..d42023c657 100644 --- a/plugins/circleci/src/api/index.ts +++ b/plugins/circleci/src/api/index.ts @@ -18,6 +18,7 @@ import { CircleCI, GitType, CircleCIOptions } from 'circleci-api'; import { ApiRef } from '@backstage/core'; const defaultOptions: Partial = { + circleHost: 'http://backstage.localhost:7000/circleci/api', vcs: { type: GitType.GITHUB, owner: 'CircleCITest3', @@ -59,10 +60,10 @@ export class CircleCIApi { const persistedToken = sessionStorage.getItem(key); let persistedVCSOptions: {} | undefined; try { - persistedVCSOptions = JSON.parse(sessionStorage.getItem(key + '_options') as string); - } catch(e) { - - } + persistedVCSOptions = JSON.parse( + sessionStorage.getItem(key + '_options') as string, + ); + } catch (e) {} if (persistedToken && persistedVCSOptions) { this.token = persistedToken; this.options.vcs = persistedVCSOptions; @@ -71,8 +72,6 @@ export class CircleCIApi { return Promise.reject(); } - - async persistToken() { if (this.authed) return; const key = circleCIApiRef.id; diff --git a/yarn.lock b/yarn.lock index cd2016d0d0..b3b8467ce5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4003,7 +4003,7 @@ "@types/http-proxy" "*" "@types/node" "*" -"@types/http-proxy@*": +"@types/http-proxy@*", "@types/http-proxy@^1.17.3": version "1.17.4" resolved "https://registry.npmjs.org/@types/http-proxy/-/http-proxy-1.17.4.tgz#e7c92e3dbe3e13aa799440ff42e6d3a17a9d045b" integrity sha512-IrSHl2u6AWXduUaDLqYpt45tLVCtYv7o4Z0s1KghBCDgIIS9oW5K1H8mZG/A2CfeLdEa7rTd1ACOiHBc1EMT2Q== @@ -10759,7 +10759,18 @@ http-proxy-middleware@0.19.1: lodash "^4.17.11" micromatch "^3.1.10" -http-proxy@^1.17.0: +http-proxy-middleware@^1.0.3: + version "1.0.3" + resolved "https://registry.npmjs.org/http-proxy-middleware/-/http-proxy-middleware-1.0.3.tgz#f73daad8dac622d51fe1769960c914b9b1f75a72" + integrity sha512-GHvPeBD+A357zS5tHjzj6ISrVOjjCiy0I92bdyTJz0pNmIjFxO0NX/bX+xkGgnclKQE/5hHAB9JEQ7u9Pw4olg== + dependencies: + "@types/http-proxy" "^1.17.3" + http-proxy "^1.18.0" + is-glob "^4.0.1" + lodash "^4.17.15" + micromatch "^4.0.2" + +http-proxy@^1.17.0, http-proxy@^1.18.0: version "1.18.0" resolved "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.0.tgz#dbe55f63e75a347db7f3d99974f2692a314a6a3a" integrity sha512-84I2iJM/n1d4Hdgc6y2+qY5mDaz2PUVjlg9znE9byl+q0uC3DeByqBGReQu5tpLK0TAqTIXScRUV+dg7+bUPpQ==