Merge pull request #19447 from ganhammar/feat/include-auth-token-in-code-coverage-plugin
Include auth token when fetching data in code coverage plugin
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-code-coverage': patch
|
||||
---
|
||||
|
||||
Use fetchApi to ensure authorization is used when fetching code-coverage data
|
||||
@@ -20,10 +20,13 @@ import {
|
||||
} from '@backstage/catalog-model';
|
||||
import { ResponseError } from '@backstage/errors';
|
||||
import { JsonCodeCoverage, JsonCoverageHistory } from './types';
|
||||
import { createApiRef, DiscoveryApi } from '@backstage/core-plugin-api';
|
||||
import {
|
||||
createApiRef,
|
||||
DiscoveryApi,
|
||||
FetchApi,
|
||||
} from '@backstage/core-plugin-api';
|
||||
|
||||
export type CodeCoverageApi = {
|
||||
discovery: DiscoveryApi;
|
||||
getCoverageForEntity: (
|
||||
entity: CompoundEntityRef,
|
||||
) => Promise<JsonCodeCoverage>;
|
||||
@@ -42,18 +45,22 @@ export const codeCoverageApiRef = createApiRef<CodeCoverageApi>({
|
||||
});
|
||||
|
||||
export class CodeCoverageRestApi implements CodeCoverageApi {
|
||||
url: string = '';
|
||||
private readonly discoveryApi: DiscoveryApi;
|
||||
private readonly fetchApi: FetchApi;
|
||||
|
||||
constructor(public discovery: DiscoveryApi) {}
|
||||
public constructor(options: {
|
||||
discoveryApi: DiscoveryApi;
|
||||
fetchApi: FetchApi;
|
||||
}) {
|
||||
this.discoveryApi = options.discoveryApi;
|
||||
this.fetchApi = options.fetchApi;
|
||||
}
|
||||
|
||||
private async fetch<T = unknown | string | JsonCoverageHistory>(
|
||||
path: string,
|
||||
init?: RequestInit,
|
||||
): Promise<T | string> {
|
||||
if (!this.url) {
|
||||
this.url = await this.discovery.getBaseUrl('code-coverage');
|
||||
}
|
||||
const resp = await fetch(`${this.url}${path}`, init);
|
||||
const url = await this.discoveryApi.getBaseUrl('code-coverage');
|
||||
const resp = await this.fetchApi.fetch(`${url}${path}`);
|
||||
if (!resp.ok) {
|
||||
throw await ResponseError.fromResponse(resp);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
createPlugin,
|
||||
createRoutableExtension,
|
||||
discoveryApiRef,
|
||||
fetchApiRef,
|
||||
} from '@backstage/core-plugin-api';
|
||||
|
||||
/**
|
||||
@@ -34,8 +35,9 @@ export const codeCoveragePlugin = createPlugin({
|
||||
apis: [
|
||||
createApiFactory({
|
||||
api: codeCoverageApiRef,
|
||||
deps: { discoveryApi: discoveryApiRef },
|
||||
factory: ({ discoveryApi }) => new CodeCoverageRestApi(discoveryApi),
|
||||
deps: { discoveryApi: discoveryApiRef, fetchApi: fetchApiRef },
|
||||
factory: ({ discoveryApi, fetchApi }) =>
|
||||
new CodeCoverageRestApi({ discoveryApi, fetchApi }),
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user