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,