Skip to content

Commit

Permalink
Merge pull request backstage#6866 from SDA-SE/feat/org-transform-msgraph
Browse files Browse the repository at this point in the history
Pass along a `OrganizationTransformer` to the read step
  • Loading branch information
Fox32 authored Aug 18, 2021
2 parents 37e25f3 + be498d2 commit 557faeb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-fishes-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend-module-msgraph': patch
---

Pass along a `OrganizationTransformer` to the read step
3 changes: 3 additions & 0 deletions plugins/catalog-backend-module-msgraph/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class MicrosoftGraphOrgReaderProcessor implements CatalogProcessor {
logger: Logger_2;
userTransformer?: UserTransformer;
groupTransformer?: GroupTransformer;
organizationTransformer?: OrganizationTransformer;
});
// (undocumented)
static fromConfig(
Expand All @@ -121,6 +122,7 @@ export class MicrosoftGraphOrgReaderProcessor implements CatalogProcessor {
logger: Logger_2;
userTransformer?: UserTransformer;
groupTransformer?: GroupTransformer;
organizationTransformer?: OrganizationTransformer;
},
): MicrosoftGraphOrgReaderProcessor;
// (undocumented)
Expand Down Expand Up @@ -174,6 +176,7 @@ export function readMicrosoftGraphOrg(
groupFilter?: string;
userTransformer?: UserTransformer;
groupTransformer?: GroupTransformer;
organizationTransformer?: OrganizationTransformer;
logger: Logger_2;
},
): Promise<{
Expand Down
18 changes: 13 additions & 5 deletions plugins/catalog-backend-module-msgraph/src/microsoftGraph/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '@backstage/catalog-model';
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
import limiterFactory from 'p-limit';
import { Logger } from 'winston';
import { MicrosoftGraphClient } from './client';
import {
MICROSOFT_GRAPH_GROUP_ID_ANNOTATION,
Expand All @@ -33,7 +34,6 @@ import {
OrganizationTransformer,
UserTransformer,
} from './types';
import { Logger } from 'winston';

export async function defaultUserTransformer(
user: MicrosoftGraph.User,
Expand Down Expand Up @@ -212,7 +212,11 @@ export async function defaultGroupTransformer(
export async function readMicrosoftGraphGroups(
client: MicrosoftGraphClient,
tenantId: string,
options?: { groupFilter?: string; transformer?: GroupTransformer },
options?: {
groupFilter?: string;
groupTransformer?: GroupTransformer;
organizationTransformer?: OrganizationTransformer;
},
): Promise<{
groups: GroupEntity[]; // With all relations empty
rootGroup: GroupEntity | undefined; // With all relations empty
Expand All @@ -224,13 +228,15 @@ export async function readMicrosoftGraphGroups(
const groupMemberOf: Map<string, Set<string>> = new Map();
const limiter = limiterFactory(10);

const { rootGroup } = await readMicrosoftGraphOrganization(client, tenantId);
const { rootGroup } = await readMicrosoftGraphOrganization(client, tenantId, {
transformer: options?.organizationTransformer,
});
if (rootGroup) {
groupMember.set(rootGroup.metadata.name, new Set<string>());
groups.push(rootGroup);
}

const transformer = options?.transformer ?? defaultGroupTransformer;
const transformer = options?.groupTransformer ?? defaultGroupTransformer;
const promises: Promise<void>[] = [];

for await (const group of client.getGroups({
Expand Down Expand Up @@ -384,6 +390,7 @@ export async function readMicrosoftGraphOrg(
groupFilter?: string;
userTransformer?: UserTransformer;
groupTransformer?: GroupTransformer;
organizationTransformer?: OrganizationTransformer;
logger: Logger;
},
): Promise<{ users: UserEntity[]; groups: GroupEntity[] }> {
Expand All @@ -395,7 +402,8 @@ export async function readMicrosoftGraphOrg(
const { groups, rootGroup, groupMember, groupMemberOf } =
await readMicrosoftGraphGroups(client, tenantId, {
groupFilter: options?.groupFilter,
transformer: options?.groupTransformer,
groupTransformer: options?.groupTransformer,
organizationTransformer: options?.organizationTransformer,
});

resolveRelations(rootGroup, groups, users, groupMember, groupMemberOf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
GroupTransformer,
MicrosoftGraphClient,
MicrosoftGraphProviderConfig,
OrganizationTransformer,
readMicrosoftGraphConfig,
readMicrosoftGraphOrg,
UserTransformer,
Expand All @@ -39,13 +40,15 @@ export class MicrosoftGraphOrgReaderProcessor implements CatalogProcessor {
private readonly logger: Logger;
private readonly userTransformer?: UserTransformer;
private readonly groupTransformer?: GroupTransformer;
private readonly organizationTransformer?: OrganizationTransformer;

static fromConfig(
config: Config,
options: {
logger: Logger;
userTransformer?: UserTransformer;
groupTransformer?: GroupTransformer;
organizationTransformer?: OrganizationTransformer;
},
) {
const c = config.getOptionalConfig('catalog.processors.microsoftGraphOrg');
Expand All @@ -60,11 +63,13 @@ export class MicrosoftGraphOrgReaderProcessor implements CatalogProcessor {
logger: Logger;
userTransformer?: UserTransformer;
groupTransformer?: GroupTransformer;
organizationTransformer?: OrganizationTransformer;
}) {
this.providers = options.providers;
this.logger = options.logger;
this.userTransformer = options.userTransformer;
this.groupTransformer = options.groupTransformer;
this.organizationTransformer = options.organizationTransformer;
}

async readLocation(
Expand Down Expand Up @@ -99,6 +104,7 @@ export class MicrosoftGraphOrgReaderProcessor implements CatalogProcessor {
groupFilter: provider.groupFilter,
userTransformer: this.userTransformer,
groupTransformer: this.groupTransformer,
organizationTransformer: this.organizationTransformer,
logger: this.logger,
},
);
Expand Down

0 comments on commit 557faeb

Please sign in to comment.