diff --git a/plugins/catalog/src/components/EntityOrphanWarning/EntityOrphanWarning.test.tsx b/plugins/catalog/src/components/EntityOrphanWarning/EntityOrphanWarning.test.tsx new file mode 100644 index 0000000000..5681a0e289 --- /dev/null +++ b/plugins/catalog/src/components/EntityOrphanWarning/EntityOrphanWarning.test.tsx @@ -0,0 +1,104 @@ +/* + * 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 { ApiProvider, ApiRegistry, ConfigReader } from '@backstage/core'; +import { + ScmIntegrationsApi, + scmIntegrationsApiRef, +} from '@backstage/integration-react'; +import { EntityProvider } from '@backstage/plugin-catalog-react'; +import { renderInTestApp } from '@backstage/test-utils'; +import React from 'react'; +import { EntityOrphanWarning } from './EntityOrphanWarning'; + +describe('', () => { + it('renders EntityOrphanWarning if the entity is orphan', async () => { + const entity = { + apiVersion: 'v1', + kind: 'Component', + metadata: { + name: 'software', + description: 'This is the description', + annotations: { 'backstage.io/orphan': 'true' }, + }, + + spec: { + owner: 'guest', + type: 'service', + lifecycle: 'production', + }, + }; + const apis = ApiRegistry.with( + scmIntegrationsApiRef, + ScmIntegrationsApi.fromConfig( + new ConfigReader({ + integrations: {}, + }), + ), + ); + + const { getByText } = await renderInTestApp( + + + + + , + ); + expect( + getByText( + 'This entity is not referenced by any location and is therefore not receiving updates. Click here to unregister.', + ), + ).toBeInTheDocument(); + }); + + it('does not render EntityOrphanWarning if the entity is not orphan', async () => { + const entity = { + apiVersion: 'v1', + kind: 'Component', + metadata: { + name: 'software', + description: 'This is the description', + }, + + spec: { + owner: 'guest', + type: 'service', + lifecycle: 'production', + }, + }; + const apis = ApiRegistry.with( + scmIntegrationsApiRef, + ScmIntegrationsApi.fromConfig( + new ConfigReader({ + integrations: {}, + }), + ), + ); + + const { queryByText } = await renderInTestApp( + + + + + , + ); + expect( + queryByText( + 'This entity is not referenced by any location and is therefore not receiving updates. Click here to unregister.', + ), + ).not.toBeInTheDocument(); + }); +}); diff --git a/plugins/catalog/src/components/EntityOrphanWarning/EntityOrphanWarning.tsx b/plugins/catalog/src/components/EntityOrphanWarning/EntityOrphanWarning.tsx new file mode 100644 index 0000000000..1215daeed1 --- /dev/null +++ b/plugins/catalog/src/components/EntityOrphanWarning/EntityOrphanWarning.tsx @@ -0,0 +1,57 @@ +/* + * 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 { useEntity } from '@backstage/plugin-catalog-react'; +import { Alert } from '@material-ui/lab'; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router'; +import { UnregisterEntityDialog } from '../UnregisterEntityDialog/UnregisterEntityDialog'; + +/** + * Displays a warning alert if the entity is marked as orphan with the ability to delete said entity. + */ +export const EntityOrphanWarning = () => { + const navigate = useNavigate(); + const [confirmationDialogOpen, setConfirmationDialogOpen] = useState(false); + + const { entity } = useEntity(); + if ( + !entity.metadata?.annotations || + entity.metadata?.annotations['backstage.io/orphan'] !== 'true' + ) { + return null; + } + + const cleanUpAfterRemoval = async () => { + setConfirmationDialogOpen(false); + navigate('/'); + }; + + return ( + <> + setConfirmationDialogOpen(true)}> + This entity is not referenced by any location and is therefore not + receiving updates. Click here to unregister. + + setConfirmationDialogOpen(false)} + /> + + ); +}; diff --git a/plugins/catalog/src/components/EntityOrphanWarning/index.ts b/plugins/catalog/src/components/EntityOrphanWarning/index.ts new file mode 100644 index 0000000000..b632e6ada9 --- /dev/null +++ b/plugins/catalog/src/components/EntityOrphanWarning/index.ts @@ -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 { EntityOrphanWarning } from './EntityOrphanWarning'; diff --git a/plugins/catalog/src/index.ts b/plugins/catalog/src/index.ts index 2e74219718..f7506b534a 100644 --- a/plugins/catalog/src/index.ts +++ b/plugins/catalog/src/index.ts @@ -35,3 +35,4 @@ export { EntityLinksCard, EntitySystemDiagramCard, } from './plugin'; +export * from './components/EntityOrphanWarning';