Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions workspaces/bulk-import/.changeset/humble-moons-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@red-hat-developer-hub/backstage-plugin-bulk-import-backend': patch
---

Correctly identify already-imported repositories, ensuring repos with pending import PRs are visible for import.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ import {
BackendFeature,
createServiceFactory,
} from '@backstage/backend-plugin-api';
import { mockServices, startTestBackend } from '@backstage/backend-test-utils';
import type { CatalogClient } from '@backstage/catalog-client';
import {
mockServices,
startTestBackend,
type ServiceMock,
} from '@backstage/backend-test-utils';
import { catalogServiceRef } from '@backstage/plugin-catalog-node/alpha';
import {
catalogServiceMock,
type CatalogServiceMock,
} from '@backstage/plugin-catalog-node/testUtils';
import { AuthorizeResult } from '@backstage/plugin-permission-common';

import { rest } from 'msw';
Expand Down Expand Up @@ -170,7 +177,7 @@ export function addHandlersForGHTokenAppErrors(server: SetupServer) {
}

export async function startBackendServer(
mockCatalogClient: CatalogClient,
mockCatalogClient: ServiceMock<CatalogServiceMock>,
authorizeResult?: AuthorizeResult.DENY | AuthorizeResult.ALLOW,
config?: any,
db?: any,
Expand Down Expand Up @@ -201,7 +208,7 @@ export async function startBackendServer(

export function setupTest() {
let server: SetupServer;
let mockCatalogClient: CatalogClient;
let mockCatalogClient: ServiceMock<CatalogServiceMock>;

beforeAll(() => {
server = setupServer(...DEFAULT_TEST_HANDLERS);
Expand All @@ -223,24 +230,7 @@ export function setupTest() {
afterAll(() => server.close());

beforeEach(() => {
// TODO(rm3l): Use 'catalogServiceMock' from '@backstage/plugin-catalog-node/testUtils'
// once '@backstage/plugin-catalog-node' is upgraded
mockCatalogClient = {
getEntities: jest.fn(),
getEntitiesByRefs: jest.fn(),
queryEntities: jest.fn(),
getEntityAncestors: jest.fn(),
getEntityByRef: jest.fn(),
removeEntityByUid: jest.fn(),
refreshEntity: jest.fn(),
getEntityFacets: jest.fn(),
getLocationById: jest.fn(),
getLocationByRef: jest.fn(),
addLocation: jest.fn(),
removeLocationById: jest.fn(),
getLocationByEntity: jest.fn(),
validateEntity: jest.fn(),
} as unknown as CatalogClient;
mockCatalogClient = catalogServiceMock.mock();
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,42 @@ export class CatalogHttpClient {
};
}

async listCatalogComponentManagedByUrlLocations(): Promise<Set<string>> {
const result = await this.catalogApi.queryEntities(
{
fields: ['metadata.annotations'],
query: {
$all: [
{ kind: 'Component' },
{
'metadata.annotations.backstage.io/managed-by-location': {
$hasPrefix: 'url:',
},
},
],
},
// We need all imported component targets to reliably exclude already-imported repositories.
// That's why we are retrieving this hard-coded high number of Components.
limit: 9999,
},
{
token: await getTokenForPlugin(this.auth, 'catalog'),
},
);

const entities = result?.items ?? [];
return new Set(
entities
.map(
entity =>
entity.metadata.annotations?.[
'backstage.io/managed-by-location'
]?.slice(4), // remove 'url:' prefix
)
.filter((repoUrl): repoUrl is string => !!repoUrl),
);
}

async listCatalogUrlLocationsById(
search?: string,
pageNumber?: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import { AuthorizeResult } from '@backstage/plugin-permission-common';
import { rest } from 'msw';
import request from 'supertest';

import {
CATALOG_API_LOCATIONS_LOCAL_ADDR,
LOCAL_ADDR,
} from '../../../../__fixtures__/handlers';
import { LOCAL_ADDR } from '../../../../__fixtures__/handlers';
import {
setupTest,
startBackendServer,
Expand Down Expand Up @@ -210,25 +207,24 @@ describe('repositories', () => {
});

it('returns filtered (not yet imported) repos when some repos are already imported', async () => {
const { server, mockCatalogClient } = useTestData();

server.use(
rest.get(CATALOG_API_LOCATIONS_LOCAL_ADDR, (_, res, ctx) =>
res(
ctx.status(200),
ctx.json([
{
data: {
id: 'imported-funtimes',
target:
'http://localhost:8765/saltypig1/funtimes/blob/main/catalog-info.yaml',
type: 'url',
},
const { mockCatalogClient } = useTestData();
mockCatalogClient.queryEntities.mockResolvedValue({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'funtimes',
annotations: {
'backstage.io/managed-by-location':
'url:http://localhost:8765/saltypig1/funtimes/blob/main/catalog-info.yaml',
},
]),
),
),
);
},
},
],
totalItems: 1,
pageInfo: {},
});

const backendServer = await startBackendServer(
mockCatalogClient,
Expand Down Expand Up @@ -269,41 +265,46 @@ describe('repositories', () => {
});

it('returns empty array when all repos are already imported', async () => {
const { server, mockCatalogClient } = useTestData();

server.use(
rest.get(CATALOG_API_LOCATIONS_LOCAL_ADDR, (_, res, ctx) =>
res(
ctx.status(200),
ctx.json([
{
data: {
id: 'imported-dolbear',
target:
'http://localhost:8765/saltypig1/dolbear/blob/main/catalog-info.yaml',
type: 'url',
},
const { mockCatalogClient } = useTestData();
mockCatalogClient.queryEntities.mockResolvedValue({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'dolbear',
annotations: {
'backstage.io/managed-by-location':
'url:http://localhost:8765/saltypig1/dolbear/blob/main/catalog-info.yaml',
},
{
data: {
id: 'imported-funtimes',
target:
'http://localhost:8765/saltypig1/funtimes/blob/main/catalog-info.yaml',
type: 'url',
},
},
},
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'funtimes',
annotations: {
'backstage.io/managed-by-location':
'url:http://localhost:8765/saltypig1/funtimes/blob/main/catalog-info.yaml',
},
{
data: {
id: 'imported-swapi-node',
target:
'http://localhost:8765/saltypig1/swapi-node/blob/main/catalog-info.yaml',
type: 'url',
},
},
},
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'swapi-node',
annotations: {
'backstage.io/managed-by-location':
'url:http://localhost:8765/saltypig1/swapi-node/blob/main/catalog-info.yaml',
},
]),
),
),
);
},
},
],
totalItems: 3,
pageInfo: {},
});

const backendServer = await startBackendServer(
mockCatalogClient,
Expand All @@ -325,25 +326,24 @@ describe('repositories', () => {
});

it('returns all repos even though a non-root catalog location exists', async () => {
const { server, mockCatalogClient } = useTestData();

server.use(
rest.get(CATALOG_API_LOCATIONS_LOCAL_ADDR, (_, res, ctx) =>
res(
ctx.status(200),
ctx.json([
{
data: {
id: 'imported-funtimes-nested',
target:
'http://localhost:8765/saltypig1/funtimes/blob/main/packages/backend/catalog-info.yaml',
type: 'url',
},
const { mockCatalogClient } = useTestData();
mockCatalogClient.queryEntities.mockResolvedValue({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'funtimes-nested',
annotations: {
'backstage.io/managed-by-location':
'url:http://localhost:8765/saltypig1/funtimes/blob/main/packages/backend/catalog-info.yaml',
},
]),
),
),
);
},
},
],
totalItems: 1,
pageInfo: {},
});

const backendServer = await startBackendServer(
mockCatalogClient,
Expand Down
Loading
Loading