diff --git a/.changeset/forty-dodos-own.md b/.changeset/forty-dodos-own.md new file mode 100644 index 0000000000..6b60872f9d --- /dev/null +++ b/.changeset/forty-dodos-own.md @@ -0,0 +1,5 @@ +--- +'@backstage/catalog-client': patch +--- + +Return entities sorted alphabetically by ref diff --git a/packages/catalog-client/src/CatalogClient.test.ts b/packages/catalog-client/src/CatalogClient.test.ts index 98b378f5b0..1fcc242fa7 100644 --- a/packages/catalog-client/src/CatalogClient.test.ts +++ b/packages/catalog-client/src/CatalogClient.test.ts @@ -47,7 +47,7 @@ describe('CatalogClient', () => { apiVersion: '1', kind: 'Component', metadata: { - name: 'Test1', + name: 'Test2', namespace: 'test1', }, }, @@ -55,13 +55,13 @@ describe('CatalogClient', () => { apiVersion: '1', kind: 'Component', metadata: { - name: 'Test2', + name: 'Test1', namespace: 'test1', }, }, ]; const defaultResponse: CatalogListResponse = { - items: defaultServiceResponse, + items: defaultServiceResponse.reverse(), }; beforeEach(() => { @@ -72,7 +72,7 @@ describe('CatalogClient', () => { ); }); - it('should entities from correct endpoint', async () => { + it('should fetch entities from correct endpoint', async () => { const response = await client.getEntities({}, { token }); expect(response).toEqual(defaultResponse); }); diff --git a/packages/catalog-client/src/CatalogClient.ts b/packages/catalog-client/src/CatalogClient.ts index 141929f3da..00e417a886 100644 --- a/packages/catalog-client/src/CatalogClient.ts +++ b/packages/catalog-client/src/CatalogClient.ts @@ -20,6 +20,7 @@ import { Location, LOCATION_ANNOTATION, ORIGIN_LOCATION_ANNOTATION, + stringifyEntityRef, stringifyLocationReference, } from '@backstage/catalog-model'; import { ResponseError } from '@backstage/errors'; @@ -89,7 +90,20 @@ export class CatalogClient implements CatalogApi { `/entities${query}`, options, ); - return { items: entities }; + + const refCompare = (a: Entity, b: Entity) => { + const aRef = stringifyEntityRef(a); + const bRef = stringifyEntityRef(b); + if (aRef < bRef) { + return -1; + } + if (aRef > bRef) { + return 1; + } + return 0; + }; + + return { items: entities.sort(refCompare) }; } async getEntityByName(