From 79efb2be1d82218f343ed4dd484b4139956979f1 Mon Sep 17 00:00:00 2001 From: Anton Ganhammar Date: Thu, 17 Aug 2023 21:31:54 +0200 Subject: [PATCH 1/4] feat: include authorization token if set in code-coverage plugin Signed-off-by: Anton Ganhammar --- plugins/code-coverage/src/api.ts | 28 +++++++++++++++++++++------- plugins/code-coverage/src/plugin.ts | 6 ++++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/plugins/code-coverage/src/api.ts b/plugins/code-coverage/src/api.ts index 081f1c7999..34e55b5cba 100644 --- a/plugins/code-coverage/src/api.ts +++ b/plugins/code-coverage/src/api.ts @@ -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, + IdentityApi, +} from '@backstage/core-plugin-api'; export type CodeCoverageApi = { - discovery: DiscoveryApi; getCoverageForEntity: ( entity: CompoundEntityRef, ) => Promise; @@ -42,18 +45,29 @@ export const codeCoverageApiRef = createApiRef({ }); export class CodeCoverageRestApi implements CodeCoverageApi { - url: string = ''; + private readonly discoveryApi: DiscoveryApi; + private readonly identityApi: IdentityApi; - constructor(public discovery: DiscoveryApi) {} + private url: string = ''; + + public constructor(options: { + discoveryApi: DiscoveryApi; + identityApi: IdentityApi; + }) { + this.discoveryApi = options.discoveryApi; + this.identityApi = options.identityApi; + } private async fetch( path: string, - init?: RequestInit, ): Promise { if (!this.url) { - this.url = await this.discovery.getBaseUrl('code-coverage'); + this.url = await this.discoveryApi.getBaseUrl('code-coverage'); } - const resp = await fetch(`${this.url}${path}`, init); + const { token } = await this.identityApi.getCredentials(); + const resp = await fetch(`${this.url}${path}`, { + headers: token ? { Authorization: `Bearer ${token}` } : {}, + }); if (!resp.ok) { throw await ResponseError.fromResponse(resp); } diff --git a/plugins/code-coverage/src/plugin.ts b/plugins/code-coverage/src/plugin.ts index f137e9d488..22b59fd50a 100644 --- a/plugins/code-coverage/src/plugin.ts +++ b/plugins/code-coverage/src/plugin.ts @@ -21,6 +21,7 @@ import { createPlugin, createRoutableExtension, discoveryApiRef, + identityApiRef, } 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, identityApi: identityApiRef }, + factory: ({ discoveryApi, identityApi }) => + new CodeCoverageRestApi({ discoveryApi, identityApi }), }), ], }); From 1d8f4f0a7486872ec56d2e40a12631b1943417f4 Mon Sep 17 00:00:00 2001 From: Anton Ganhammar Date: Thu, 17 Aug 2023 21:45:58 +0200 Subject: [PATCH 2/4] feat: add changeset Signed-off-by: Anton Ganhammar --- .changeset/nice-forks-remain.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/nice-forks-remain.md diff --git a/.changeset/nice-forks-remain.md b/.changeset/nice-forks-remain.md new file mode 100644 index 0000000000..1545c88745 --- /dev/null +++ b/.changeset/nice-forks-remain.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-code-coverage': patch +--- + +Include authorization token (if one exists) when fetching code-coverage data From 850ca8eb106b24a834f73fab5073ab3d65506395 Mon Sep 17 00:00:00 2001 From: Anton Ganhammar Date: Fri, 18 Aug 2023 07:07:39 +0200 Subject: [PATCH 3/4] fix: use fetchapi to automatically handle authentication Signed-off-by: Anton Ganhammar --- .changeset/nice-forks-remain.md | 2 +- plugins/code-coverage/src/api.ts | 13 +++++-------- plugins/code-coverage/src/plugin.ts | 8 ++++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/.changeset/nice-forks-remain.md b/.changeset/nice-forks-remain.md index 1545c88745..6d833ff283 100644 --- a/.changeset/nice-forks-remain.md +++ b/.changeset/nice-forks-remain.md @@ -2,4 +2,4 @@ '@backstage/plugin-code-coverage': patch --- -Include authorization token (if one exists) when fetching code-coverage data +Use fetchApi to ensure authorization is used when fetching code-coverage data diff --git a/plugins/code-coverage/src/api.ts b/plugins/code-coverage/src/api.ts index 34e55b5cba..9947891006 100644 --- a/plugins/code-coverage/src/api.ts +++ b/plugins/code-coverage/src/api.ts @@ -23,7 +23,7 @@ import { JsonCodeCoverage, JsonCoverageHistory } from './types'; import { createApiRef, DiscoveryApi, - IdentityApi, + FetchApi, } from '@backstage/core-plugin-api'; export type CodeCoverageApi = { @@ -46,16 +46,16 @@ export const codeCoverageApiRef = createApiRef({ export class CodeCoverageRestApi implements CodeCoverageApi { private readonly discoveryApi: DiscoveryApi; - private readonly identityApi: IdentityApi; + private readonly fetchApi: FetchApi; private url: string = ''; public constructor(options: { discoveryApi: DiscoveryApi; - identityApi: IdentityApi; + fetchApi: FetchApi; }) { this.discoveryApi = options.discoveryApi; - this.identityApi = options.identityApi; + this.fetchApi = options.fetchApi; } private async fetch( @@ -64,10 +64,7 @@ export class CodeCoverageRestApi implements CodeCoverageApi { if (!this.url) { this.url = await this.discoveryApi.getBaseUrl('code-coverage'); } - const { token } = await this.identityApi.getCredentials(); - const resp = await fetch(`${this.url}${path}`, { - headers: token ? { Authorization: `Bearer ${token}` } : {}, - }); + const resp = await this.fetchApi.fetch(`${this.url}${path}`); if (!resp.ok) { throw await ResponseError.fromResponse(resp); } diff --git a/plugins/code-coverage/src/plugin.ts b/plugins/code-coverage/src/plugin.ts index 22b59fd50a..391595f142 100644 --- a/plugins/code-coverage/src/plugin.ts +++ b/plugins/code-coverage/src/plugin.ts @@ -21,7 +21,7 @@ import { createPlugin, createRoutableExtension, discoveryApiRef, - identityApiRef, + fetchApiRef, } from '@backstage/core-plugin-api'; /** @@ -35,9 +35,9 @@ export const codeCoveragePlugin = createPlugin({ apis: [ createApiFactory({ api: codeCoverageApiRef, - deps: { discoveryApi: discoveryApiRef, identityApi: identityApiRef }, - factory: ({ discoveryApi, identityApi }) => - new CodeCoverageRestApi({ discoveryApi, identityApi }), + deps: { discoveryApi: discoveryApiRef, fetchApi: fetchApiRef }, + factory: ({ discoveryApi, fetchApi }) => + new CodeCoverageRestApi({ discoveryApi, fetchApi }), }), ], }); From a80f321353a5590d337a84d2f038ad76aaeb7dda Mon Sep 17 00:00:00 2001 From: Anton Ganhammar Date: Fri, 18 Aug 2023 15:07:14 +0200 Subject: [PATCH 4/4] chore: re-ask for the code-coverage url before fetching Signed-off-by: Anton Ganhammar --- plugins/code-coverage/src/api.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/plugins/code-coverage/src/api.ts b/plugins/code-coverage/src/api.ts index 9947891006..942e5a8a47 100644 --- a/plugins/code-coverage/src/api.ts +++ b/plugins/code-coverage/src/api.ts @@ -48,8 +48,6 @@ export class CodeCoverageRestApi implements CodeCoverageApi { private readonly discoveryApi: DiscoveryApi; private readonly fetchApi: FetchApi; - private url: string = ''; - public constructor(options: { discoveryApi: DiscoveryApi; fetchApi: FetchApi; @@ -61,10 +59,8 @@ export class CodeCoverageRestApi implements CodeCoverageApi { private async fetch( path: string, ): Promise { - if (!this.url) { - this.url = await this.discoveryApi.getBaseUrl('code-coverage'); - } - const resp = await this.fetchApi.fetch(`${this.url}${path}`); + const url = await this.discoveryApi.getBaseUrl('code-coverage'); + const resp = await this.fetchApi.fetch(`${url}${path}`); if (!resp.ok) { throw await ResponseError.fromResponse(resp); }