From 29e1789e1d9e9c0ef8dadd38b06120d54bed42c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 30 Mar 2021 09:31:25 +0200 Subject: [PATCH] Make sure that Group is taken into account when filling out an org hierarchy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/old-hounds-doubt.md | 5 +++++ .../src/ingestion/processors/util/org.test.ts | 18 ++++++++++++++++++ .../src/ingestion/processors/util/org.ts | 8 +++++++- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .changeset/old-hounds-doubt.md diff --git a/.changeset/old-hounds-doubt.md b/.changeset/old-hounds-doubt.md new file mode 100644 index 0000000000..5076c1f1b6 --- /dev/null +++ b/.changeset/old-hounds-doubt.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Make sure that Group `spec.members` is taken into account when filling out an org hierarchy diff --git a/plugins/catalog-backend/src/ingestion/processors/util/org.test.ts b/plugins/catalog-backend/src/ingestion/processors/util/org.test.ts index c9056eebb4..e264847478 100644 --- a/plugins/catalog-backend/src/ingestion/processors/util/org.test.ts +++ b/plugins/catalog-backend/src/ingestion/processors/util/org.test.ts @@ -73,4 +73,22 @@ describe('buildMemberOf', () => { buildMemberOf(groups, [u]); expect(u.spec.memberOf).toEqual(expect.arrayContaining(['a', 'b', 'c'])); }); + + it('takes group spec.members into account', () => { + const a = g('a', undefined, []); + const b = g('b', 'a', []); + const c = g('c', 'b', []); + c.spec.members = ['n']; + const u: UserEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'User', + metadata: { name: 'n' }, + spec: { profile: {}, memberOf: [] }, + }; + + const groups = [a, b, c]; + buildOrgHierarchy(groups); + buildMemberOf(groups, [u]); + expect(u.spec.memberOf).toEqual(expect.arrayContaining(['a', 'b', 'c'])); + }); }); diff --git a/plugins/catalog-backend/src/ingestion/processors/util/org.ts b/plugins/catalog-backend/src/ingestion/processors/util/org.ts index 787e408e60..660c5cd7d3 100644 --- a/plugins/catalog-backend/src/ingestion/processors/util/org.ts +++ b/plugins/catalog-backend/src/ingestion/processors/util/org.ts @@ -57,7 +57,13 @@ export function buildMemberOf(groups: GroupEntity[], users: UserEntity[]) { users.forEach(user => { const transitiveMemberOf = new Set(); - const todo = [...user.spec.memberOf]; + const todo = [ + ...user.spec.memberOf, + ...groups + .filter(g => g.spec.members?.includes(user.metadata.name)) + .map(g => g.metadata.name), + ]; + for (;;) { const current = todo.pop(); if (!current) {