From a20bc00b41fd999f40e2c30bc11533ff51c49164 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 8 Sep 2020 19:42:36 +0200 Subject: [PATCH] plugins/circleci: refactor to use DiscoverApi --- docs/api/utility-apis.md | 4 ++-- plugins/circleci/dev/index.tsx | 10 +-------- plugins/circleci/src/api/index.ts | 34 +++++++++++++++++++++++-------- plugins/circleci/src/plugin.ts | 13 ++++++------ 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/docs/api/utility-apis.md b/docs/api/utility-apis.md index d192cbd6ee..599bbbb66c 100644 --- a/docs/api/utility-apis.md +++ b/docs/api/utility-apis.md @@ -97,7 +97,7 @@ app, and the app itself. ### Core APIs -Starting with the Backstage core library, it provides implementation for all of +Starting with the Backstage core library, it provides implementations for all of the core APIs. The core APIs are the ones exported by `@backstage/core`, such as the `errorApiRef` and `configApiRef`. You can find a full list of them [here](../reference/utility-apis/README.md). @@ -113,7 +113,7 @@ While doing so they should usually also provide default implementations of their own APIs, for example, the `catalog` plugin exports `catalogApiRef`, and also supplies a default `ApiFactory` of that API using the `CatalogClient`. There is one restriction to plugin-provided API Factories: plugins may not supply -factories for core APIs, trying to do so will cause the app to crash. +factories for core APIs, trying to do so will cause the app to refuse to start. Plugins supply their APIs through the `apis` option of `createPlugin`, for example: diff --git a/plugins/circleci/dev/index.tsx b/plugins/circleci/dev/index.tsx index 4bf67d5cb2..812a5585d4 100644 --- a/plugins/circleci/dev/index.tsx +++ b/plugins/circleci/dev/index.tsx @@ -16,13 +16,5 @@ import { createDevApp } from '@backstage/dev-utils'; import { plugin } from '../src/plugin'; -import { circleCIApiRef, CircleCIApi } from '../src/api'; -createDevApp() - .registerPlugin(plugin) - .registerApi({ - api: circleCIApiRef, - deps: {}, - factory: () => new CircleCIApi(), - }) - .render(); +createDevApp().registerPlugin(plugin).render(); diff --git a/plugins/circleci/src/api/index.ts b/plugins/circleci/src/api/index.ts index da4c811812..c71be6b781 100644 --- a/plugins/circleci/src/api/index.ts +++ b/plugins/circleci/src/api/index.ts @@ -26,7 +26,7 @@ import { BuildSummary, GitType, } from 'circleci-api'; -import { createApiRef } from '@backstage/core'; +import { createApiRef, DiscoveryApi } from '@backstage/core'; export { GitType }; export type { BuildWithSteps, BuildStepAction, BuildSummary }; @@ -36,15 +36,28 @@ export const circleCIApiRef = createApiRef({ description: 'Used by the CircleCI plugin to make requests', }); +const DEFAULT_PROXY_PATH = '/circleci/api'; + +type Options = { + discoveryApi: DiscoveryApi; + /** + * Path to use for requests via the proxy, defaults to /circleci/api + */ + proxyPath?: string; +}; + export class CircleCIApi { - apiUrl: string; - constructor(apiUrl: string = '/circleci/api') { - this.apiUrl = apiUrl; + private readonly discoveryApi: DiscoveryApi; + private readonly proxyPath: string; + + constructor(options: Options) { + this.discoveryApi = options.discoveryApi; + this.proxyPath = options.proxyPath ?? DEFAULT_PROXY_PATH; } async retry(buildNumber: number, options: Partial) { return postBuildActions('', buildNumber, BuildAction.RETRY, { - circleHost: this.apiUrl, + circleHost: await this.getApiUrl(), ...options.vcs, }); } @@ -59,19 +72,24 @@ export class CircleCIApi { offset, }, vcs: {}, - circleHost: this.apiUrl, + circleHost: await this.getApiUrl(), ...options, }); } async getUser(options: Partial) { - return getMe('', { circleHost: this.apiUrl, ...options }); + return getMe('', { circleHost: await this.getApiUrl(), ...options }); } async getBuild(buildNumber: number, options: Partial) { return getFullBuild('', buildNumber, { - circleHost: this.apiUrl, + circleHost: await this.getApiUrl(), ...options.vcs, }); } + + private async getApiUrl() { + const proxyUrl = await this.discoveryApi.getBaseUrl('proxy'); + return proxyUrl + this.proxyPath; + } } diff --git a/plugins/circleci/src/plugin.ts b/plugins/circleci/src/plugin.ts index f966caa383..c5bb122ed3 100644 --- a/plugins/circleci/src/plugin.ts +++ b/plugins/circleci/src/plugin.ts @@ -14,7 +14,11 @@ * limitations under the License. */ -import { createPlugin, createApiFactory, configApiRef } from '@backstage/core'; +import { + createPlugin, + createApiFactory, + discoveryApiRef, +} from '@backstage/core'; import { circleCIApiRef, CircleCIApi } from './api'; export const plugin = createPlugin({ @@ -22,11 +26,8 @@ export const plugin = createPlugin({ apis: [ createApiFactory({ api: circleCIApiRef, - deps: { configApi: configApiRef }, - factory: ({ configApi }) => - new CircleCIApi( - `${configApi.getString('backend.baseUrl')}/proxy/circleci/api`, - ), + deps: { discoveryApi: discoveryApiRef }, + factory: ({ discoveryApi }) => new CircleCIApi({ discoveryApi }), }), ], });