Merge pull request #6093 from kuangp/refactor/catalogClient

refactor(catalogClient): return fetched entities sorted by ref
This commit is contained in:
Ben Lambert
2021-06-21 15:25:40 +02:00
committed by GitHub
3 changed files with 24 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/catalog-client': patch
---
Return entities sorted alphabetically by ref
@@ -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<Entity> = {
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);
});
+15 -1
View File
@@ -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(