Ensure that emitted relations are deduplicated

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2021-06-28 11:49:09 +02:00
parent 1d1b197a65
commit aa2b15d9df
4 changed files with 19 additions and 1 deletions
@@ -74,6 +74,13 @@ describe('Stitcher', () => {
type: 'looksAt',
target_entity_ref: 'k:ns/other',
},
// handles and ignores duplicates
{
originating_entity_id: 'my-id',
source_entity_ref: 'k:ns/n',
type: 'looksAt',
target_entity_ref: 'k:ns/other',
},
]);
await stitcher.stitch(new Set(['k:ns/n']));
@@ -22,6 +22,7 @@ import {
} from '@backstage/catalog-model';
import { SerializedError } from '@backstage/errors';
import { Knex } from 'knex';
import { uniqBy } from 'lodash';
import { v4 as uuid } from 'uuid';
import { Logger } from 'winston';
import {
@@ -168,7 +169,11 @@ export class Stitcher {
// TODO: entityRef is lower case and should be uppercase in the final
// result
entity.relations = result
const uniqueRelationRows = uniqBy(
result,
r => `${r.relationType}:${r.relationTarget}`,
);
entity.relations = uniqueRelationRows
.filter(row => row.relationType /* exclude null row, if relevant */)
.map(row => ({
type: row.relationType!,