Add a FossaPage that shows the license compliance status of all components in the catalog

Signed-off-by: Dominik Henneke <dominik.henneke@sda-se.com>
This commit is contained in:
Dominik Henneke
2021-04-19 19:58:47 +02:00
parent 94da209767
commit 40d1e11cf3
20 changed files with 1350 additions and 250 deletions
+21
View File
@@ -0,0 +1,21 @@
---
'@backstage/plugin-fossa': patch
---
Add a `FossaPage` that shows the license compliance status of all components in the catalog.
This is an optional component that can be registered as a route:
```diff
// packages/app/src/App.tsx
+ import { FossaPage } from '@backstage/plugin-fossa';
// ...
const routes = (
<FlatRoutes>
// ...
+ <Route path="/fossa" element={<FossaPage />} />
</FlatRoutes>
);
```
+48
View File
@@ -75,3 +75,51 @@ spec:
owner: CNCF
lifecycle: experimental
```
## Other Components
### FOSSA Overview Page
The plugin provides an optional page that can be used to check the license compliance of all components.
![FOSSA Overview](./docs/fossa-overview.png)
Add it to your Backstage application:
1. Install the FOSSA Plugin (see [Getting Started](#getting-started)).
2. Register the page:
```tsx
// packages/app/src/App.tsx
import { FossaPage } from '@backstage/plugin-fossa';
// ...
const routes = (
<FlatRoutes>
// ...
<Route path="/fossa" element={<FossaPage />} />
</FlatRoutes>
);
```
3. (Optional) Add a Sidebar Icon:
```tsx
// packages/app/src/components/Root/Root.tsx
// ...
export const Root = ({ children }: PropsWithChildren<{}>) => (
<SidebarPage>
<Sidebar>
// ...
<SidebarItem icon={CheckCircleIcon} to="fossa" text="FOSSA" />
// ...
</Sidebar>
{children}
</SidebarPage>
);
```
+70 -4
View File
@@ -14,14 +14,19 @@
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { Entity, RELATION_OWNED_BY } from '@backstage/catalog-model';
import { Content, Header, Page } from '@backstage/core';
import { createDevApp } from '@backstage/dev-utils';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import {
CatalogApi,
catalogApiRef,
EntityProvider,
} from '@backstage/plugin-catalog-react';
import { Grid } from '@material-ui/core';
import React from 'react';
import { EntityFossaCard } from '../src';
import { FossaApi, fossaApiRef } from '../src/api';
import { EntityFossaCard, fossaPlugin } from '../src';
import { FindingSummary, FossaApi, fossaApiRef } from '../src/api';
import { FossaPage } from '../src/components/FossaPage';
import { FOSSA_PROJECT_NAME_ANNOTATION } from '../src/components/useProjectName';
const entity = (name?: string) =>
@@ -34,9 +39,16 @@ const entity = (name?: string) =>
},
name: name,
},
relations: [
{
type: RELATION_OWNED_BY,
target: { kind: 'Group', namespace: 'default', name },
},
],
} as Entity);
createDevApp()
.registerPlugin(fossaPlugin)
.registerApi({
api: fossaApiRef,
deps: {},
@@ -81,8 +93,58 @@ createDevApp()
return undefined;
}
},
getFindingSummaries: async () => {
await new Promise(r => setTimeout(r, 1000));
return new Map<string, FindingSummary>([
[
'zero-deps',
{
timestamp: '2000-01-01T00:00:00Z',
projectUrl: '',
projectDefaultBranch: 'master',
issueCount: 0,
dependencyCount: 0,
},
],
[
'issues',
{
timestamp: '2001-01-01T00:00:00Z',
projectUrl: '',
projectDefaultBranch: 'develop',
issueCount: 10,
dependencyCount: 15,
},
],
[
'no-issues',
{
timestamp: '2002-01-01T00:00:00Z',
projectUrl: '',
projectDefaultBranch: 'master',
issueCount: 0,
dependencyCount: 5,
},
],
]);
},
} as FossaApi),
})
.registerApi({
api: catalogApiRef,
deps: {},
factory: () =>
(({
getEntities: async () => {
await new Promise(r => setTimeout(r, 1000));
return {
items: [entity('zero-deps'), entity('issues'), entity('no-issues')],
};
},
} as Partial<CatalogApi>) as any),
})
.addPage({
title: 'Entity Content',
element: (
@@ -130,4 +192,8 @@ createDevApp()
</Page>
),
})
.addPage({
title: 'Catalog Overview',
element: <FossaPage />,
})
.render();
Binary file not shown.

After

Width:  |  Height:  |  Size: 366 KiB

+4
View File
@@ -33,12 +33,15 @@
"dependencies": {
"@backstage/catalog-model": "^0.7.3",
"@backstage/core": "^0.7.5",
"@backstage/errors": "^0.1.1",
"@backstage/plugin-catalog-react": "^0.1.1",
"@backstage/theme": "^0.2.5",
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "4.0.0-alpha.45",
"cross-fetch": "^3.0.6",
"luxon": "^1.26.0",
"p-limit": "^3.0.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-use": "^15.3.3"
@@ -49,6 +52,7 @@
"@backstage/test-utils": "^0.1.10",
"@testing-library/jest-dom": "^5.10.1",
"@testing-library/react": "^11.2.5",
"@testing-library/react-hooks": "^3.4.2",
"@testing-library/user-event": "^12.0.7",
"@types/jest": "^26.0.7",
"@types/node": "^14.14.32",
+14
View File
@@ -30,5 +30,19 @@ export const fossaApiRef = createApiRef<FossaApi>({
});
export type FossaApi = {
/**
* Get the finding summary for a list of projects
*
* @param projectTitles a list of project titles in FOSSA
*/
getFindingSummaries(
projectTitles: Array<string>,
): Promise<Map<string, FindingSummary>>;
/**
* Get the finding summary of a single project.
*
* @param projectTitle the project title in FOSSA
*/
getFindingSummary(projectTitle: string): Promise<FindingSummary | undefined>;
};
+253 -125
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { UrlPatternDiscovery, IdentityApi } from '@backstage/core';
import { IdentityApi, UrlPatternDiscovery } from '@backstage/core';
import { msw } from '@backstage/test-utils';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
@@ -52,138 +52,266 @@ describe('FossaClient', () => {
});
});
it('should report finding summary', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => {
expect(req.url.searchParams.toString()).toBe(
'count=1&sort=title+&title=our-service&organizationId=8736',
);
return res(
ctx.json([
describe('getFindingSummary', () => {
it('should report finding summary', async () => {
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://app.fossa.com/projects/custom%2B8736%2Four-service',
} as FindingSummary);
});
it('should report finding summary without licensing_issue_count', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (_req, res, ctx) => {
return res(
ctx.json([
{
locator: 'custom+8736/our-service',
title: 'our-service',
default_branch: 'refs/master',
revisions: [
{
updatedAt: '2020-01-01T00:00:00Z',
dependency_count: 160,
unresolved_issue_count: 100,
},
],
},
]),
);
}),
);
const summary = await client.getFindingSummary('our-service');
expect(summary).toEqual({
timestamp: '2020-01-01T00:00:00Z',
issueCount: 100,
dependencyCount: 160,
projectDefaultBranch: 'refs/master',
projectUrl:
'https://app.fossa.com/projects/custom%2B8736%2Four-service',
} as FindingSummary);
});
it('should handle empty result', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (_req, res, ctx) => {
return res(ctx.json([]));
}),
);
const summary = await client.getFindingSummary('our-service');
expect(summary).toBeUndefined();
});
it('should ignore result with invalid title', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (_req, res, ctx) => {
return res(ctx.json([{ title: 'our-service-2' }]));
}),
);
const summary = await client.getFindingSummary('our-service');
expect(summary).toBeUndefined();
});
it('should skip organizationId', async () => {
client = new FossaClient({ discoveryApi, identityApi });
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => {
const expectedQuery =
'count=1000&page=0&sort=title%2B&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([]));
}),
);
const summary = await client.getFindingSummary('our-service');
expect(summary).toBeUndefined();
});
it('should handle 404 status', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (_req, res, ctx) => {
return res(ctx.status(404));
}),
);
await expect(client.getFindingSummary('our-service')).rejects.toThrow();
});
});
describe('getFindingSummaries', () => {
it('should report finding summary', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => {
const expectedQuery =
'count=1000&page=0&sort=title%2B&organizationId=8736';
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,
},
],
},
{
locator: 'custom+8736/our-service-2',
title: 'our-service-2',
default_branch: 'develop',
revisions: [
{
updatedAt: '2020-01-01T00:00:00Z',
dependency_count: 160,
unresolved_licensing_issue_count: 5,
unresolved_issue_count: 100,
},
],
},
{
locator: 'custom+8736/our-service-3',
title: 'our-service-3',
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.getFindingSummaries([
'our-service',
'our-service-2',
]);
expect(summary).toEqual(
new Map<string, FindingSummary>([
[
'our-service',
{
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,
},
],
timestamp: '2020-01-01T00:00:00Z',
issueCount: 5,
dependencyCount: 160,
projectDefaultBranch: 'develop',
projectUrl:
'https://app.fossa.com/projects/custom%2B8736%2Four-service',
},
]),
);
}),
);
const summary = await client.getFindingSummary('our-service');
expect(summary).toEqual({
timestamp: '2020-01-01T00:00:00Z',
issueCount: 5,
dependencyCount: 160,
projectDefaultBranch: 'develop',
projectUrl: 'https://app.fossa.com/projects/custom%2B8736%2Four-service',
} as FindingSummary);
});
it('should report finding summary without licensing_issue_count', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => {
expect(req.url.searchParams.toString()).toBe(
'count=1&sort=title+&title=our-service&organizationId=8736',
);
return res(
ctx.json([
],
[
'our-service-2',
{
locator: 'custom+8736/our-service',
title: 'our-service',
default_branch: 'refs/master',
revisions: [
{
updatedAt: '2020-01-01T00:00:00Z',
dependency_count: 160,
unresolved_issue_count: 100,
},
],
timestamp: '2020-01-01T00:00:00Z',
issueCount: 5,
dependencyCount: 160,
projectDefaultBranch: 'develop',
projectUrl:
'https://app.fossa.com/projects/custom%2B8736%2Four-service-2',
},
]),
);
}),
);
],
]),
);
});
const summary = await client.getFindingSummary('our-service');
it('should handle empty result', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (_req, res, ctx) => {
return res(ctx.json([]));
}),
);
expect(summary).toEqual({
timestamp: '2020-01-01T00:00:00Z',
issueCount: 100,
dependencyCount: 160,
projectDefaultBranch: 'refs/master',
projectUrl: 'https://app.fossa.com/projects/custom%2B8736%2Four-service',
} as FindingSummary);
});
const summary = await client.getFindingSummaries([
'our-service',
'our-service-2',
]);
it('should handle empty result', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => {
expect(req.url.searchParams.toString()).toBe(
'count=1&sort=title+&title=our-service&organizationId=8736',
);
return res(ctx.json([]));
}),
);
expect(summary).toEqual(new Map());
});
const summary = await client.getFindingSummary('our-service');
it('should handle 404 status', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (_req, res, ctx) => {
return res(ctx.status(404));
}),
);
expect(summary).toBeUndefined();
});
it('should ignore result with invalid title', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => {
expect(req.url.searchParams.toString()).toBe(
'count=1&sort=title+&title=our-service&organizationId=8736',
);
return res(ctx.json([{ title: 'our-service-2' }]));
}),
);
const summary = await client.getFindingSummary('our-service');
expect(summary).toBeUndefined();
});
it('should skip organizationId', async () => {
client = new FossaClient({ discoveryApi, identityApi });
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => {
expect(req.url.searchParams.toString()).toBe(
'count=1&sort=title+&title=our-service',
);
return res(ctx.status(404));
}),
);
const summary = await client.getFindingSummary('our-service');
expect(summary).toBeUndefined();
});
it('should handle 404 status', async () => {
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => {
expect(req.url.searchParams.toString()).toBe(
'count=1&sort=title+&title=our-service&organizationId=8736',
);
return res(ctx.status(404));
}),
);
const summary = await client.getFindingSummary('our-service');
expect(summary).toBeUndefined();
await expect(
client.getFindingSummaries(['our-service', 'our-service-2']),
).rejects.toThrow();
});
});
});
+103 -29
View File
@@ -15,13 +15,28 @@
*/
import { DiscoveryApi, IdentityApi } from '@backstage/core';
import { ResponseError } from '@backstage/errors';
import fetch from 'cross-fetch';
import pLimit from 'p-limit';
import { FindingSummary, FossaApi } from './FossaApi';
type FossaProjectsResponse = {
title: string;
default_branch: string;
locator: string;
revisions: Array<{
updatedAt: string;
unresolved_licensing_issue_count: number;
unresolved_issue_count: number;
dependency_count: number;
}>;
};
export class FossaClient implements FossaApi {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
organizationId?: string;
private readonly limit = pLimit(5);
constructor({
discoveryApi,
@@ -37,42 +52,101 @@ export class FossaClient implements FossaApi {
this.organizationId = organizationId;
}
private async callApi(path: string): Promise<any> {
private async callApi<T>(
path: string,
query: Record<string, any>,
): Promise<T | undefined> {
const apiUrl = `${await this.discoveryApi.getBaseUrl('proxy')}/fossa`;
const idToken = await this.identityApi.getIdToken();
const response = await fetch(`${apiUrl}/${path}`, {
headers: idToken ? { Authorization: `Bearer ${idToken}` } : {},
});
if (response.status === 200) {
return await response.json();
const response = await fetch(
`${apiUrl}/${path}?${new URLSearchParams(query).toString()}`,
{
headers: idToken ? { Authorization: `Bearer ${idToken}` } : {},
},
);
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
return undefined;
return (await response.json()) as T;
}
async *getProject(
projectTitles: Set<string>,
): AsyncIterable<{ title: string; summary: FindingSummary }> {
let nextPage = 0;
const pageSize = 1000;
for (;;) {
const projects = await this.limit(
page =>
this.callApi<FossaProjectsResponse[]>('projects', {
count: pageSize,
page,
sort: 'title+',
...(this.organizationId && {
organizationId: this.organizationId,
}),
...(projectTitles.size === 1 && {
title: projectTitles.values().next().value,
}),
}),
nextPage++,
);
if (!projects) {
break;
}
for (const project of projects) {
if (projectTitles.has(project.title) && project.revisions.length > 0) {
const revision = project.revisions[0];
yield {
title: project.title,
summary: {
timestamp: revision.updatedAt,
issueCount:
revision.unresolved_licensing_issue_count ||
revision.unresolved_issue_count,
dependencyCount: revision.dependency_count,
projectDefaultBranch: project.default_branch,
projectUrl: `https://app.fossa.com/projects/${encodeURIComponent(
project.locator,
)}`,
},
};
}
}
if (projects.length < pageSize) {
break;
}
}
}
async getFindingSummaries(
projectTitles: string[],
): Promise<Map<string, FindingSummary>> {
const map = new Map<string, FindingSummary>();
if (projectTitles.length === 0) {
return map;
}
for await (const { title, summary } of this.getProject(
new Set(projectTitles),
)) {
map.set(title, summary);
}
return map;
}
async getFindingSummary(
projectTitle: string,
): Promise<FindingSummary | undefined> {
const project = await this.callApi(
`projects?count=1&sort=title+&title=${projectTitle}${
this.organizationId ? `&organizationId=${this.organizationId}` : ''
}`,
);
if (!project || project.length === 0 || project[0].title !== projectTitle) {
return undefined;
}
const revision = project[0].revisions[0];
return {
timestamp: revision.updatedAt,
issueCount:
revision.unresolved_licensing_issue_count ||
revision.unresolved_issue_count,
dependencyCount: revision.dependency_count,
projectDefaultBranch: project[0].default_branch,
projectUrl: `https://app.fossa.com/projects/${encodeURIComponent(
project[0].locator,
)}`,
};
const summaries = await this.getFindingSummaries([projectTitle]);
return summaries.get(projectTitle);
}
}
@@ -0,0 +1,186 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { ApiProvider, ApiRegistry } from '@backstage/core';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import { renderInTestApp } from '@backstage/test-utils';
import React from 'react';
import { FossaApi, fossaApiRef } from '../../api';
import { FossaCard } from './FossaCard';
describe('<FossaCard />', () => {
const fossaApi: jest.Mocked<FossaApi> = {
getFindingSummary: jest.fn(),
getFindingSummaries: jest.fn(),
};
let Wrapper: React.ComponentType;
beforeEach(() => {
const apis = ApiRegistry.with(fossaApiRef, fossaApi);
Wrapper = ({ children }: { children?: React.ReactNode }) => (
<ApiProvider apis={apis}>{children}</ApiProvider>
);
});
afterEach(() => jest.resetAllMocks());
it('shows missing annotation', async () => {
const entity: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'my-name',
},
};
const { getByText } = await renderInTestApp(
<Wrapper>
<EntityProvider entity={entity}>
<FossaCard />
</EntityProvider>
</Wrapper>,
);
expect(getByText(/Missing Annotation/i)).toBeInTheDocument();
expect(getByText('fossa.io/project-name')).toBeInTheDocument();
});
it('shows error', async () => {
const entity: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'my-name',
annotations: {
'fossa.io/project-name': 'my-name',
},
},
};
fossaApi.getFindingSummary.mockRejectedValue(new Error('My Error'));
const { getByText } = await renderInTestApp(
<Wrapper>
<EntityProvider entity={entity}>
<FossaCard />
</EntityProvider>
</Wrapper>,
);
expect(getByText(/Warning: My Error/i)).toBeInTheDocument();
});
it('shows empty', async () => {
const entity: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'my-name',
annotations: {
'fossa.io/project-name': 'my-name',
},
},
};
fossaApi.getFindingSummary.mockResolvedValue(undefined);
const { getByText } = await renderInTestApp(
<Wrapper>
<EntityProvider entity={entity}>
<FossaCard />
</EntityProvider>
</Wrapper>,
);
expect(
getByText(/There is no Fossa project with title 'my-name'./i),
).toBeInTheDocument();
});
it('shows fossa issues', async () => {
const entity: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'my-name',
annotations: {
'fossa.io/project-name': 'my-name',
},
},
};
fossaApi.getFindingSummary.mockResolvedValue({
timestamp: '2000-01-01T00:00:00Z',
projectDefaultBranch: 'branch/default-branch',
projectUrl: 'http://…',
issueCount: 0,
dependencyCount: 10,
});
const { getByText } = await renderInTestApp(
<Wrapper>
<EntityProvider entity={entity}>
<FossaCard />
</EntityProvider>
</Wrapper>,
);
expect(getByText(/Number of issues/i)).toBeInTheDocument();
expect(
getByText(
(_, node) =>
node?.textContent ===
'Based on 10 Dependencies on branch branch/default-branch.',
),
).toBeInTheDocument();
});
it('warns about zero dependencies', async () => {
const entity: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'my-name',
annotations: {
'fossa.io/project-name': 'my-name',
},
},
};
fossaApi.getFindingSummary.mockResolvedValue({
timestamp: '2000-01-01T00:00:00Z',
projectDefaultBranch: 'branch/default-branch',
projectUrl: 'http://…',
issueCount: 0,
dependencyCount: 0,
});
const { getByText } = await renderInTestApp(
<Wrapper>
<EntityProvider entity={entity}>
<FossaCard />
</EntityProvider>
</Wrapper>,
);
expect(getByText(/No Dependencies/i)).toBeInTheDocument();
expect(
getByText(/Please check your FOSSA project settings/i),
).toBeInTheDocument();
});
});
@@ -17,14 +17,17 @@
import {
EmptyState,
InfoCard,
InfoCardVariants,
MissingAnnotationEmptyState,
Progress,
ResponseErrorPanel,
useApi,
} from '@backstage/core';
import { useEntity } from '@backstage/plugin-catalog-react';
import { Grid, Tooltip } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import React from 'react';
import { DateTime } from 'luxon';
import React, { PropsWithChildren } from 'react';
import { useAsync } from 'react-use';
import { fossaApiRef } from '../../api';
import {
@@ -65,109 +68,134 @@ const useStyles = makeStyles(theme => ({
},
}));
export const FossaCard = () => {
const Card = ({
children,
disabled,
projectUrl,
variant = 'gridItem',
}: PropsWithChildren<{
disabled?: boolean;
projectUrl?: string;
variant?: InfoCardVariants;
}>) => {
const classes = useStyles();
return (
<InfoCard
title="License Findings"
deepLink={
projectUrl
? {
title: 'View more',
link: projectUrl,
}
: undefined
}
variant={variant}
className={disabled ? classes.disabled : undefined}
>
{children}
</InfoCard>
);
};
export const FossaCard = ({ variant }: { variant?: InfoCardVariants }) => {
const { entity } = useEntity();
const fossaApi = useApi(fossaApiRef);
const projectTitle = useProjectName(entity);
const { value, loading } = useAsync(
const { value, loading, error } = useAsync(
async () =>
projectTitle ? fossaApi.getFindingSummary(projectTitle) : undefined,
[fossaApi, projectTitle],
);
const deepLink = value
? {
title: 'View more',
link: value.projectUrl,
}
: undefined;
const classes = useStyles();
if (error) {
return (
<Card disabled variant={variant}>
<ResponseErrorPanel error={error} />
</Card>
);
}
if (loading) {
return (
<Card disabled variant={variant}>
<Progress />
</Card>
);
}
if (!projectTitle) {
return (
<Card disabled variant={variant}>
<MissingAnnotationEmptyState
annotation={FOSSA_PROJECT_NAME_ANNOTATION}
/>
</Card>
);
}
if (!value) {
return (
<Card disabled variant={variant}>
<EmptyState
missing="info"
title="No information to display"
description={`There is no Fossa project with title '${projectTitle}'.`}
/>
</Card>
);
}
return (
<>
<InfoCard
title="License Findings"
deepLink={deepLink}
variant="gridItem"
className={
!loading && (!projectTitle || !value) ? classes.disabled : undefined
}
<Card projectUrl={value.projectUrl} variant={variant}>
<Grid
item
container
direction="column"
justify="space-between"
alignItems="center"
style={{ height: '100%' }}
spacing={0}
>
{loading && <Progress />}
{!loading && !projectTitle && (
<MissingAnnotationEmptyState
annotation={FOSSA_PROJECT_NAME_ANNOTATION}
/>
)}
{!loading && projectTitle && !value && (
<EmptyState
missing="info"
title="No information to display"
description={`There is no Fossa project with title '${projectTitle}'.`}
/>
)}
{value && (
<Grid
item
container
direction="column"
justify="space-between"
alignItems="center"
style={{ height: '100%' }}
spacing={0}
<Grid item>
<p
className={
value.issueCount > 0 || value.dependencyCount === 0
? classes.numberError
: classes.numberSuccess
}
>
<Grid item>
<p
className={
value.issueCount > 0 || value.dependencyCount === 0
? classes.numberError
: classes.numberSuccess
}
>
{value.issueCount}
</p>
{value.dependencyCount > 0 && (
<p className={classes.description}>Number of issues</p>
)}
{value.dependencyCount === 0 && (
<p className={classes.description}>
No Dependencies.
<br />
Please check your FOSSA project settings.
</p>
)}
</Grid>
{value.issueCount}
</p>
{value.dependencyCount > 0 && (
<p className={classes.description}>Number of issues</p>
)}
{value.dependencyCount === 0 && (
<p className={classes.description}>
No Dependencies.
<br />
Please check your FOSSA project settings.
</p>
)}
</Grid>
<Grid item className={classes.lastAnalyzed}>
Last analyzed on{' '}
{new Date(value.timestamp).toLocaleString('en-US', {
timeZone: 'UTC',
day: 'numeric',
month: 'short',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false,
})}
</Grid>
<Grid item className={classes.lastAnalyzed}>
Based on {value.dependencyCount} Dependencies on branch{' '}
<Tooltip title="The default branch can be changed by a FOSSA admin.">
<span className={classes.branch}>
{value.projectDefaultBranch}
</span>
</Tooltip>
.
</Grid>
</Grid>
)}
</InfoCard>
</>
<Grid item className={classes.lastAnalyzed}>
Last analyzed on{' '}
{DateTime.fromISO(value.timestamp).toLocaleString(
DateTime.DATETIME_MED,
)}
</Grid>
<Grid item className={classes.lastAnalyzed}>
Based on {value.dependencyCount} Dependencies on branch{' '}
<Tooltip title="The default branch can be changed by a FOSSA admin.">
<span className={classes.branch}>{value.projectDefaultBranch}</span>
</Tooltip>
.
</Grid>
</Grid>
</Card>
);
};
@@ -0,0 +1,134 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { ApiProvider, ApiRegistry } from '@backstage/core';
import { CatalogApi, catalogApiRef } from '@backstage/plugin-catalog-react';
import { renderInTestApp } from '@backstage/test-utils';
import React from 'react';
import { FossaApi, fossaApiRef } from '../../api';
import { FossaPage } from './FossaPage';
describe('<FossaPage />', () => {
const catalogApi: jest.Mocked<CatalogApi> = {
addLocation: jest.fn(),
getEntities: jest.fn(),
getEntityByName: jest.fn(),
getLocationByEntity: jest.fn(),
getLocationById: jest.fn(),
getOriginLocationByEntity: jest.fn(),
removeEntityByUid: jest.fn(),
removeLocationById: jest.fn(),
};
const fossaApi: jest.Mocked<FossaApi> = {
getFindingSummary: jest.fn(),
getFindingSummaries: jest.fn(),
};
let Wrapper: React.ComponentType;
beforeEach(() => {
const apis = ApiRegistry.with(fossaApiRef, fossaApi).with(
catalogApiRef,
catalogApi,
);
Wrapper = ({ children }: { children?: React.ReactNode }) => (
<ApiProvider apis={apis}>{children}</ApiProvider>
);
});
afterEach(() => jest.resetAllMocks());
it('shows fossa issues', async () => {
const entity0: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'my-name-0',
annotations: {
'fossa.io/project-name': 'my-name-0',
},
},
};
const entity1: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'my-name-1',
annotations: {
'fossa.io/project-name': 'my-name-1',
},
},
};
const entity2: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'my-name-2',
annotations: {
'fossa.io/project-name': 'my-name-2',
},
},
};
const entity3: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'my-name-3',
},
};
catalogApi.getEntities.mockResolvedValue({
items: [entity0, entity1, entity2, entity3],
});
fossaApi.getFindingSummaries.mockResolvedValue(
new Map([
[
'my-name-1',
{
timestamp: '2000-01-01T00:00:00Z',
projectDefaultBranch: 'branch/default-branch',
projectUrl: 'http://…',
issueCount: 0,
dependencyCount: 0,
},
],
[
'my-name-2',
{
timestamp: '2000-01-01T00:00:00Z',
projectDefaultBranch: 'branch/default-branch',
projectUrl: 'http://…',
issueCount: 10,
dependencyCount: 10,
},
],
]),
);
const { getByText, getAllByText } = await renderInTestApp(
<Wrapper>
<FossaPage />
</Wrapper>,
);
expect(getAllByText(/Not configured/i)).toHaveLength(2);
expect(getByText(/No dependencies/i)).toBeInTheDocument();
expect(getByText(/0 Issues/i)).toBeInTheDocument();
expect(getByText(/10 Issues/i)).toBeInTheDocument();
});
});
@@ -0,0 +1,228 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
Entity,
EntityName,
RELATION_OWNED_BY,
} from '@backstage/catalog-model';
import {
Content,
Header,
Page,
StatusError,
StatusOK,
StatusWarning,
Table,
TableColumn,
TableFilter,
useApi,
} from '@backstage/core';
import {
catalogApiRef,
EntityRefLink,
EntityRefLinks,
formatEntityRefTitle,
getEntityRelations,
} from '@backstage/plugin-catalog-react';
import { Tooltip } from '@material-ui/core';
import { Skeleton } from '@material-ui/lab';
import { DateTime } from 'luxon';
import * as React from 'react';
import { useMemo } from 'react';
import { useAsync } from 'react-use';
import { FindingSummary, fossaApiRef } from '../../api';
import { useProjectNames } from '../useProjectNames';
type FossaRow = {
entity: Entity;
resolved: {
name: string;
ownedByRelationsTitle?: string;
ownedByRelations: EntityName[];
loading: boolean;
details?: FindingSummary;
};
};
const columns: TableColumn<FossaRow>[] = [
{
title: 'Name',
field: 'resolved.name',
highlight: true,
render: ({ entity }) => <EntityRefLink entityRef={entity} />,
},
{
title: 'Owner',
field: 'resolved.ownedByRelationsTitle',
highlight: true,
render: ({ resolved }) => (
<EntityRefLinks
entityRefs={resolved.ownedByRelations}
defaultKind="group"
/>
),
},
{
title: 'Status',
field: 'resolved.details.issueCount',
// We interpret missing values as '0.5' to be sorted at the end of the projects with issues.
customSort: (a, b) =>
(b?.resolved?.details?.issueCount ?? 0.5) -
(a?.resolved?.details?.issueCount ?? 0.5),
render: ({ resolved }) => {
if (resolved.loading) {
return <Skeleton animation="pulse" />;
} else if (!resolved.details) {
return <StatusWarning>Not configured</StatusWarning>;
} else if (resolved.details.dependencyCount === 0) {
return <StatusWarning>No dependencies</StatusWarning>;
} else if (resolved.details.issueCount > 0) {
return <StatusError>{resolved.details.issueCount} Issues</StatusError>;
}
return <StatusOK>{resolved.details.issueCount} Issues</StatusOK>;
},
},
{
title: 'Dependencies',
field: 'resolved.details.dependencyCount',
render: ({ resolved: { loading, details } }) => {
if (loading) {
return <Skeleton animation="pulse" />;
}
return details?.dependencyCount;
},
},
{
title: 'Branch',
field: 'resolved.details.projectDefaultBranch',
render: ({ resolved: { loading, details } }) => {
if (loading) {
return <Skeleton animation="pulse" />;
}
return details?.projectDefaultBranch;
},
},
{
title: 'Last Updated',
field: 'resolved.details.timestamp',
render: ({ resolved: { loading, details } }) => {
if (loading) {
return <Skeleton animation="pulse" />;
}
return (
details?.timestamp && (
<Tooltip
title={DateTime.fromISO(details.timestamp).toLocaleString(
DateTime.DATETIME_MED,
)}
>
<span>
{DateTime.fromISO(details.timestamp).toRelative({
locale: 'en',
})}
</span>
</Tooltip>
)
);
},
},
];
const filters: TableFilter[] = [
{ column: 'Owner', type: 'multiple-select' },
{ column: 'Branch', type: 'select' },
];
export const FossaPage = () => {
const catalogApi = useApi(catalogApiRef);
const fossaApi = useApi(fossaApiRef);
// Get a list of all relevant entities
const { value: entities, loading: entitiesLoading } = useAsync(() => {
return catalogApi.getEntities({
filter: { kind: 'Component' },
fields: [
'kind',
'metadata.namespace',
'metadata.name',
'metadata.annotations',
'relations',
],
});
});
// get the project names of all entities. the idx of both lists match.
const projectNames = useProjectNames(entities?.items);
// get the summary list
const { value: summaries, loading: summariesLoading } = useAsync(
async () =>
await fossaApi.getFindingSummaries(
projectNames.filter(n => n !== undefined) as string[],
),
[projectNames],
);
// compose the rows
const rows: FossaRow[] = useMemo(
() =>
entities?.items?.map((entity, idx) => {
const projectName = projectNames[idx];
const summary = projectName ? summaries?.get(projectName) : undefined;
const ownedByRelations = getEntityRelations(entity, RELATION_OWNED_BY);
return {
entity,
resolved: {
name: formatEntityRefTitle(entity),
ownedByRelations,
ownedByRelationsTitle: ownedByRelations
.map(r => formatEntityRefTitle(r, { defaultKind: 'group' }))
.join(', '),
loading: summariesLoading,
details: summary,
},
};
}) ?? [],
[projectNames, entities?.items, summaries, summariesLoading],
);
return (
<Page themeId="home">
<Header title="FOSSA Component Overview" />
<Content>
<Table<FossaRow>
columns={columns}
data={rows}
filters={filters}
isLoading={entitiesLoading}
options={{
pageSize: 20,
actionsColumnIndex: -1,
loadingType: 'linear',
padding: 'dense',
showEmptyDataSourceMessage: !entitiesLoading,
}}
/>
</Content>
</Page>
);
};
@@ -0,0 +1,17 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { FossaPage } from './FossaPage';
@@ -0,0 +1,38 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { renderHook } from '@testing-library/react-hooks';
import { useProjectName } from './useProjectName';
describe('useProjectName', () => {
const entity: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'test',
annotations: {
'fossa.io/project-name': 'test',
},
},
};
it('should extract the fossa project name', async () => {
const { result } = renderHook(() => useProjectName(entity));
expect(result.current).toBe('test');
});
});
@@ -0,0 +1,45 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { renderHook } from '@testing-library/react-hooks';
import { useProjectNames } from './useProjectNames';
describe('useProjectNames', () => {
const entity0: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'test',
annotations: {
'fossa.io/project-name': 'test',
},
},
};
const entity1: Entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'test',
},
};
it('should extract the fossa project name', async () => {
const { result } = renderHook(() => useProjectNames([entity0, entity1]));
expect(result.current).toEqual(['test', undefined]);
});
});
@@ -0,0 +1,33 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { useMemo } from 'react';
import { FOSSA_PROJECT_NAME_ANNOTATION } from './useProjectName';
export const useProjectNames = (
entities: Entity[] | undefined,
): Array<string | undefined> => {
return useMemo(
() =>
entities?.map(
entity =>
entity.metadata.annotations?.[FOSSA_PROJECT_NAME_ANNOTATION] ??
undefined,
) ?? [],
[entities],
);
};
+12 -1
View File
@@ -14,8 +14,12 @@
* limitations under the License.
*/
import { createComponentExtension } from '@backstage/core';
import {
createComponentExtension,
createRoutableExtension,
} from '@backstage/core';
import { fossaPlugin } from './plugin';
import { rootRoute } from './routes';
export const EntityFossaCard = fossaPlugin.provide(
createComponentExtension({
@@ -24,3 +28,10 @@ export const EntityFossaCard = fossaPlugin.provide(
},
}),
);
export const FossaPage = fossaPlugin.provide(
createRoutableExtension({
component: () => import('./components/FossaPage').then(m => m.FossaPage),
mountPoint: rootRoute,
}),
);
+1 -1
View File
@@ -15,4 +15,4 @@
*/
export { fossaPlugin } from './plugin';
export { EntityFossaCard } from './extensions';
export { EntityFossaCard, FossaPage } from './extensions';
+4
View File
@@ -22,6 +22,7 @@ import {
identityApiRef,
} from '@backstage/core';
import { fossaApiRef, FossaClient } from './api';
import { rootRoute } from './routes';
export const fossaPlugin = createPlugin({
id: 'fossa',
@@ -41,4 +42,7 @@ export const fossaPlugin = createPlugin({
}),
}),
],
routes: {
fossaOverview: rootRoute,
},
});
+21
View File
@@ -0,0 +1,21 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createRouteRef } from '@backstage/core';
export const rootRoute = createRouteRef({
id: 'fossa-overview',
});