From a8953a9c92e0bcf7cf7ce14fe86cf90bf4fe9fc4 Mon Sep 17 00:00:00 2001
From: Andrew Thauer <6507159+andrewthauer@users.noreply.github.com>
Date: Thu, 18 Feb 2021 22:58:57 -0500
Subject: [PATCH 1/2] feat: omit default namespace in catalog-import
---
.changeset/dirty-buckets-flow.md | 5 ++
.../StepPrepareCreatePullRequest.test.tsx | 46 ++++++++++++++++++-
.../StepPrepareCreatePullRequest.tsx | 3 +-
3 files changed, 51 insertions(+), 3 deletions(-)
create mode 100644 .changeset/dirty-buckets-flow.md
diff --git a/.changeset/dirty-buckets-flow.md b/.changeset/dirty-buckets-flow.md
new file mode 100644
index 0000000000..c0f0f030e2
--- /dev/null
+++ b/.changeset/dirty-buckets-flow.md
@@ -0,0 +1,5 @@
+---
+'@backstage/plugin-catalog-import': patch
+---
+
+Omits the entity namespace from a generated entity when it has not be explicitly defined in the analyze-location result.
diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.test.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.test.tsx
index 0b38fdaf58..0f8df04968 100644
--- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.test.tsx
+++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.test.tsx
@@ -21,7 +21,10 @@ import { act, render, RenderResult } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { AnalyzeResult, catalogImportApiRef } from '../../api';
-import { StepPrepareCreatePullRequest } from './StepPrepareCreatePullRequest';
+import {
+ generateEntities,
+ StepPrepareCreatePullRequest,
+} from './StepPrepareCreatePullRequest';
describe('', () => {
const catalogImportApi: jest.Mocked = {
@@ -60,6 +63,7 @@ describe('', () => {
kind: 'Component',
metadata: {
name: 'my-component',
+ namespace: 'default',
},
spec: {
owner: 'my-owner',
@@ -284,4 +288,44 @@ spec:
][0],
).toMatchObject({ groups: ['Group:my-group'], groupsLoading: false });
});
+
+ describe('generateEntities', () => {
+ it.each([[undefined], [null]])(
+ 'should not include blank namespace for %s',
+ namespace => {
+ expect(
+ generateEntities(
+ [{ metadata: { namespace: namespace as any } }],
+ 'my-component',
+ 'group-1',
+ ),
+ ).toEqual([
+ expect.objectContaining({
+ metadata: expect.not.objectContaining({
+ namespace: 'default',
+ }),
+ }),
+ ]);
+ },
+ );
+
+ it.each([['default'], ['my-namespace']])(
+ 'should include explicit namespace %s',
+ namespace => {
+ expect(
+ generateEntities(
+ [{ metadata: { namespace } }],
+ 'my-component',
+ 'group-1',
+ ),
+ ).toEqual([
+ expect.objectContaining({
+ metadata: expect.objectContaining({
+ namespace,
+ }),
+ }),
+ ]);
+ },
+ );
+ });
});
diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx
index d9e7b962ab..0d08e27681 100644
--- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx
+++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx
@@ -66,7 +66,7 @@ type Props = {
) => React.ReactNode;
};
-function generateEntities(
+export function generateEntities(
entities: PartialEntity[],
componentName: string,
owner: string,
@@ -78,7 +78,6 @@ function generateEntities(
metadata: {
...e.metadata,
name: componentName,
- namespace: e.metadata?.namespace ?? 'default',
},
spec: {
...e.spec,
From 8b5f8f84cbc9e8e0c34e5d0bb1b26040e22ea4f5 Mon Sep 17 00:00:00 2001
From: Andrew Thauer <6507159+andrewthauer@users.noreply.github.com>
Date: Sat, 20 Feb 2021 11:18:32 -0500
Subject: [PATCH 2/2] update to use short form group entity references
---
.changeset/dirty-buckets-flow.md | 3 ++-
.../src/components/ImportStepper/defaults.tsx | 2 +-
.../StepPrepareCreatePullRequest.test.tsx | 2 +-
.../StepPrepareCreatePullRequest.tsx | 12 ++++++++----
4 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/.changeset/dirty-buckets-flow.md b/.changeset/dirty-buckets-flow.md
index c0f0f030e2..b76ab5d35c 100644
--- a/.changeset/dirty-buckets-flow.md
+++ b/.changeset/dirty-buckets-flow.md
@@ -2,4 +2,5 @@
'@backstage/plugin-catalog-import': patch
---
-Omits the entity namespace from a generated entity when it has not be explicitly defined in the analyze-location result.
+This updates the `catalog-import` plugin to omit the default metadata namespace
+field and also use the short form entity reference format for selected group owners.
diff --git a/plugins/catalog-import/src/components/ImportStepper/defaults.tsx b/plugins/catalog-import/src/components/ImportStepper/defaults.tsx
index f2dd1d62f4..5252b0aeae 100644
--- a/plugins/catalog-import/src/components/ImportStepper/defaults.tsx
+++ b/plugins/catalog-import/src/components/ImportStepper/defaults.tsx
@@ -230,7 +230,7 @@ export function defaultGenerateStepper(
errorHelperText="required value"
textFieldProps={{
label: 'Entity Owner',
- placeholder: 'Group:default/my-group',
+ placeholder: 'my-group',
}}
rules={{ required: true }}
required
diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.test.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.test.tsx
index 0f8df04968..606f9ff88e 100644
--- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.test.tsx
+++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.test.tsx
@@ -286,7 +286,7 @@ spec:
renderFormFieldsFn.mock.calls[
renderFormFieldsFn.mock.calls.length - 1
][0],
- ).toMatchObject({ groups: ['Group:my-group'], groupsLoading: false });
+ ).toMatchObject({ groups: ['my-group'], groupsLoading: false });
});
describe('generateEntities', () => {
diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx
index 0d08e27681..333df76026 100644
--- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx
+++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx
@@ -14,9 +14,12 @@
* limitations under the License.
*/
-import { Entity, serializeEntityRef } from '@backstage/catalog-model';
+import { Entity } from '@backstage/catalog-model';
import { useApi } from '@backstage/core';
-import { catalogApiRef } from '@backstage/plugin-catalog-react';
+import {
+ catalogApiRef,
+ formatEntityRefTitle,
+} from '@backstage/plugin-catalog-react';
import { Box, FormHelperText, Grid, Typography } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import React, { useCallback, useState } from 'react';
@@ -106,8 +109,9 @@ export const StepPrepareCreatePullRequest = ({
filter: { kind: 'group' },
});
- // TODO: defaultKind (=group), defaultNamespace (=same as entity)
- return groupEntities.items.map(e => serializeEntityRef(e) as string).sort();
+ return groupEntities.items
+ .map(e => formatEntityRefTitle(e, { defaultKind: 'group' }))
+ .sort();
});
const handleResult = useCallback(