Keep target field in entities output

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2022-03-04 10:49:17 +01:00
committed by Fredrik Adelöw
parent cdb623445e
commit 36c2ada275
2 changed files with 83 additions and 1 deletions
@@ -469,6 +469,62 @@ describe('DefaultEntitiesCatalog', () => {
expect(entities.length).toBe(0);
},
);
it.each(databases.eachSupportedId())(
'should return both target and targetRef for entities',
async databaseId => {
const { knex } = await createDatabase(databaseId);
await addEntity(
knex,
{
apiVersion: 'a',
kind: 'k',
metadata: { name: 'one' },
spec: {},
relations: [{ type: 'r', targetRef: 'x:y/z' } as any],
},
[],
);
await addEntity(
knex,
{
apiVersion: 'a',
kind: 'k',
metadata: { name: 'two' },
spec: {},
relations: [
{
type: 'r',
target: { kind: 'x', namespace: 'y', name: 'z' },
} as any,
],
},
[],
);
const catalog = new DefaultEntitiesCatalog(knex);
const { entities } = await catalog.entities();
expect(
entities.find(e => e.metadata.name === 'one')!.relations,
).toEqual([
{
type: 'r',
targetRef: 'x:y/z',
target: { kind: 'x', namespace: 'y', name: 'z' },
},
]);
expect(
entities.find(e => e.metadata.name === 'two')!.relations,
).toEqual([
{
type: 'r',
targetRef: 'x:y/z',
target: { kind: 'x', namespace: 'y', name: 'z' },
},
]);
},
);
});
describe('removeEntityByUid', () => {
@@ -14,7 +14,11 @@
* limitations under the License.
*/
import { Entity, stringifyEntityRef } from '@backstage/catalog-model';
import {
Entity,
parseEntityRef,
stringifyEntityRef,
} from '@backstage/catalog-model';
import { InputError, NotFoundError } from '@backstage/errors';
import { Knex } from 'knex';
import lodash from 'lodash';
@@ -200,6 +204,28 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog {
entities = entities.map(e => request.fields!(e));
}
// TODO(freben): This is added as a compatibility guarantee, until we can be
// sure that all adopters have re-stitched their entities so that the new
// targetRef field is present on them, and that they have stopped consuming
// the now-removed old field
// TODO(jhaals): Remove this in April 2021
for (const entity of entities) {
if (entity.relations) {
for (const relation of entity.relations as any) {
if (!relation.targetRef && relation.target) {
// This is the case where an old-form entity, not yet stitched with
// the updated code, was in the database
relation.targetRef = stringifyEntityRef(relation.target);
} else if (!relation.target && relation.targetRef) {
// This is the case where a new-form entity, stitched with the
// updated code, was in the database but we still want to produce
// the old data shape as well for compatibility reasons
relation.target = parseEntityRef(relation.targetRef);
}
}
}
}
return {
entities,
pageInfo,