diff --git a/.changeset/cuddly-flowers-provide.md b/.changeset/cuddly-flowers-provide.md new file mode 100644 index 0000000000..729ac96cda --- /dev/null +++ b/.changeset/cuddly-flowers-provide.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-fossa': patch +--- + +Allow configuration of base URL for Fossa links diff --git a/plugins/fossa/README.md b/plugins/fossa/README.md index 898c9d0475..10ccba5ba7 100644 --- a/plugins/fossa/README.md +++ b/plugins/fossa/README.md @@ -43,9 +43,12 @@ proxy: headers: Authorization: token ${FOSSA_API_TOKEN} -# if you have a fossa organization, configure your id here fossa: + # if you have a fossa organization, configure your id here organizationId: + # if you have a self-managed fossa instance, + # configure the baseUrl to use for links to the fossa page here + externalLinkBaseUrl: ``` 4. Get an api-token and provide `FOSSA_AUTH_HEADER` as env variable (https://app.fossa.com/account/settings/integrations/api_tokens) diff --git a/plugins/fossa/config.d.ts b/plugins/fossa/config.d.ts index 72d844c4e9..26a2823097 100644 --- a/plugins/fossa/config.d.ts +++ b/plugins/fossa/config.d.ts @@ -20,6 +20,12 @@ export interface Config { * The organization id in fossa. * @visibility frontend */ - organizationId: string; + organizationId?: string; + + /** + * The base url to use for external links (from Backstage to Fossa). + * @visibility frontend + */ + externalLinkBaseUrl?: string; }; } diff --git a/plugins/fossa/src/api/FossaClient.test.ts b/plugins/fossa/src/api/FossaClient.test.ts index 65b0fc1d53..0e2dc81e1f 100644 --- a/plugins/fossa/src/api/FossaClient.test.ts +++ b/plugins/fossa/src/api/FossaClient.test.ts @@ -175,6 +175,58 @@ describe('FossaClient', () => { expect(summary).toBeUndefined(); }); + it('should allow custom external link base url', async () => { + client = new FossaClient({ + discoveryApi, + identityApi, + organizationId: '8736', + externalLinkBaseUrl: 'https://custom.fossa.com', // overrides the default app.fossa.com + }); + server.use( + rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => { + const expectedQuery = + 'count=1000&page=0&sort=title%2B&organizationId=8736&title=our-service'; + if (req.url.searchParams.toString() !== expectedQuery) { + return res( + ctx.status(500), + ctx.body( + `${req.url.searchParams.toString()} !== ${expectedQuery}`, + ), + ); + } + + return res( + ctx.json([ + { + locator: 'custom+8736/our-service', + title: 'our-service', + default_branch: 'develop', + revisions: [ + { + updatedAt: '2020-01-01T00:00:00Z', + dependency_count: 160, + unresolved_licensing_issue_count: 5, + unresolved_issue_count: 100, + }, + ], + }, + ]), + ); + }), + ); + + const summary = await client.getFindingSummary('our-service'); + + expect(summary).toEqual({ + timestamp: '2020-01-01T00:00:00Z', + issueCount: 5, + dependencyCount: 160, + projectDefaultBranch: 'develop', + projectUrl: + 'https://custom.fossa.com/projects/custom%2B8736%2Four-service', + } as FindingSummary); + }); + it('should handle 404 status', async () => { server.use( rest.get(`${mockBaseUrl}/fossa/projects`, (_req, res, ctx) => { diff --git a/plugins/fossa/src/api/FossaClient.ts b/plugins/fossa/src/api/FossaClient.ts index 66b44de21e..8b4b4b02e5 100644 --- a/plugins/fossa/src/api/FossaClient.ts +++ b/plugins/fossa/src/api/FossaClient.ts @@ -36,20 +36,24 @@ export class FossaClient implements FossaApi { discoveryApi: DiscoveryApi; identityApi: IdentityApi; organizationId?: string; + externalLinkBaseUrl: string; private readonly limit = pLimit(5); constructor({ discoveryApi, identityApi, organizationId, + externalLinkBaseUrl = 'https://app.fossa.com', }: { discoveryApi: DiscoveryApi; identityApi: IdentityApi; organizationId?: string; + externalLinkBaseUrl?: string; }) { this.discoveryApi = discoveryApi; this.identityApi = identityApi; this.organizationId = organizationId; + this.externalLinkBaseUrl = externalLinkBaseUrl; } private async callApi( @@ -104,9 +108,9 @@ export class FossaClient implements FossaApi { revision.unresolved_issue_count, dependencyCount: revision.dependency_count, projectDefaultBranch: project.default_branch, - projectUrl: `https://app.fossa.com/projects/${encodeURIComponent( - project.locator, - )}`, + projectUrl: `${ + this.externalLinkBaseUrl + }/projects/${encodeURIComponent(project.locator)}`, }, }; } diff --git a/plugins/fossa/src/plugin.ts b/plugins/fossa/src/plugin.ts index a586d6d8a6..954ecedc81 100644 --- a/plugins/fossa/src/plugin.ts +++ b/plugins/fossa/src/plugin.ts @@ -39,6 +39,9 @@ export const fossaPlugin = createPlugin({ discoveryApi, identityApi, organizationId: configApi.getOptionalString('fossa.organizationId'), + externalLinkBaseUrl: configApi.getOptionalString( + 'fossa.externalLinkBaseUrl', + ), }), }), ],