Merge pull request #2610 from spotify/rugvip/discovery
plugins/circleci: refactor to use DiscoveryApi
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<CircleCIApi>({
|
||||
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<CircleCIOptions>) {
|
||||
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<CircleCIOptions>) {
|
||||
return getMe('', { circleHost: this.apiUrl, ...options });
|
||||
return getMe('', { circleHost: await this.getApiUrl(), ...options });
|
||||
}
|
||||
|
||||
async getBuild(buildNumber: number, options: Partial<CircleCIOptions>) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 }),
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user