chore: refactor function inputs
Signed-off-by: Markus <markus.siebert@deutschebahn.com>
This commit is contained in:
@@ -16,52 +16,74 @@
|
||||
import { GroupEntity, UserEntity } from '@backstage/catalog-model';
|
||||
import {
|
||||
GitLabGroup,
|
||||
GitLabUser,
|
||||
GitlabProviderConfig,
|
||||
GroupNameTransformer,
|
||||
GroupNameTransformerOptions,
|
||||
GroupTransformerOptions,
|
||||
UserTransformerOptions,
|
||||
} from './types';
|
||||
import { GitLabIntegrationConfig } from '@backstage/integration';
|
||||
|
||||
export function defaultGroupNameTransformer(
|
||||
group: GitLabGroup,
|
||||
config: GitlabProviderConfig,
|
||||
options: GroupNameTransformerOptions,
|
||||
): string {
|
||||
if (config.group && group.full_path.startsWith(`${config.group}/`)) {
|
||||
return group.full_path.replace(`${config.group}/`, '').replaceAll('/', '-');
|
||||
if (
|
||||
options.providerConfig.group &&
|
||||
options.group.full_path.startsWith(`${options.providerConfig.group}/`)
|
||||
) {
|
||||
return options.group.full_path
|
||||
.replace(`${options.providerConfig.group}/`, '')
|
||||
.replaceAll('/', '-');
|
||||
}
|
||||
return group.full_path.replaceAll('/', '-');
|
||||
return options.group.full_path.replaceAll('/', '-');
|
||||
}
|
||||
|
||||
export function defaultGroupTransformer(
|
||||
group: GitLabGroup,
|
||||
providerConfig: GitlabProviderConfig,
|
||||
groupNameTransformer: GroupNameTransformer,
|
||||
): GroupEntity {
|
||||
const annotations: { [annotationName: string]: string } = {};
|
||||
export function defaultGroupEntitiesTransformer(
|
||||
options: GroupTransformerOptions,
|
||||
): GroupEntity[] {
|
||||
const idMapped: { [groupId: number]: GitLabGroup } = {};
|
||||
const entities: GroupEntity[] = [];
|
||||
|
||||
annotations[`${providerConfig.host}/team-path`] = group.full_path;
|
||||
|
||||
const entity: GroupEntity = {
|
||||
apiVersion: 'backstage.io/v1alpha1',
|
||||
kind: 'Group',
|
||||
metadata: {
|
||||
name: groupNameTransformer(group, providerConfig),
|
||||
annotations: annotations,
|
||||
},
|
||||
spec: {
|
||||
type: 'team',
|
||||
children: [],
|
||||
profile: {
|
||||
displayName: group.name,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (group.description) {
|
||||
entity.metadata.description = group.description;
|
||||
for (const group of options.groups) {
|
||||
idMapped[group.id] = group;
|
||||
}
|
||||
|
||||
return entity;
|
||||
for (const group of options.groups) {
|
||||
const annotations: { [annotationName: string]: string } = {};
|
||||
|
||||
annotations[`${options.providerConfig.host}/team-path`] = group.full_path;
|
||||
|
||||
const entity: GroupEntity = {
|
||||
apiVersion: 'backstage.io/v1alpha1',
|
||||
kind: 'Group',
|
||||
metadata: {
|
||||
name: options.groupNameTransformer({
|
||||
group,
|
||||
providerConfig: options.providerConfig,
|
||||
}),
|
||||
annotations: annotations,
|
||||
},
|
||||
spec: {
|
||||
type: 'team',
|
||||
children: [],
|
||||
profile: {
|
||||
displayName: group.name,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (group.description) {
|
||||
entity.metadata.description = group.description;
|
||||
}
|
||||
|
||||
if (group.parent_id && idMapped.hasOwnProperty(group.parent_id)) {
|
||||
entity.spec.parent = options.groupNameTransformer({
|
||||
group: idMapped[group.parent_id],
|
||||
providerConfig: options.providerConfig,
|
||||
});
|
||||
}
|
||||
|
||||
entities.push(entity);
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,36 +93,34 @@ export function defaultGroupTransformer(
|
||||
* @public
|
||||
*/
|
||||
export function defaultUserTransformer(
|
||||
user: GitLabUser,
|
||||
integrationConfig: GitLabIntegrationConfig,
|
||||
providerConfig: GitlabProviderConfig,
|
||||
groupNameTransformer: GroupNameTransformer,
|
||||
options: UserTransformerOptions,
|
||||
): UserEntity {
|
||||
const annotations: { [annotationName: string]: string } = {};
|
||||
|
||||
annotations[`${integrationConfig.host}/user-login`] = user.web_url;
|
||||
if (user?.group_saml_identity?.extern_uid) {
|
||||
annotations[`${integrationConfig.host}/saml-external-uid`] =
|
||||
user.group_saml_identity.extern_uid;
|
||||
annotations[`${options.integrationConfig.host}/user-login`] =
|
||||
options.user.web_url;
|
||||
if (options.user?.group_saml_identity?.extern_uid) {
|
||||
annotations[`${options.integrationConfig.host}/saml-external-uid`] =
|
||||
options.user.group_saml_identity.extern_uid;
|
||||
}
|
||||
|
||||
const entity: UserEntity = {
|
||||
apiVersion: 'backstage.io/v1alpha1',
|
||||
kind: 'User',
|
||||
metadata: {
|
||||
name: user.username,
|
||||
name: options.user.username,
|
||||
annotations: annotations,
|
||||
},
|
||||
spec: {
|
||||
profile: {
|
||||
displayName: user.name || undefined,
|
||||
picture: user.avatar_url || undefined,
|
||||
displayName: options.user.name || undefined,
|
||||
picture: options.user.avatar_url || undefined,
|
||||
},
|
||||
memberOf: [],
|
||||
},
|
||||
};
|
||||
|
||||
if (user.email) {
|
||||
if (options.user.email) {
|
||||
if (!entity.spec) {
|
||||
entity.spec = {};
|
||||
}
|
||||
@@ -109,15 +129,20 @@ export function defaultUserTransformer(
|
||||
entity.spec.profile = {};
|
||||
}
|
||||
|
||||
entity.spec.profile.email = user.email;
|
||||
entity.spec.profile.email = options.user.email;
|
||||
}
|
||||
|
||||
if (user.groups) {
|
||||
for (const group of user.groups) {
|
||||
if (options.user.groups) {
|
||||
for (const group of options.user.groups) {
|
||||
if (!entity.spec.memberOf) {
|
||||
entity.spec.memberOf = [];
|
||||
}
|
||||
entity.spec.memberOf.push(groupNameTransformer(group, providerConfig));
|
||||
entity.spec.memberOf.push(
|
||||
options.groupNameTransformer({
|
||||
group,
|
||||
providerConfig: options.providerConfig,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -149,21 +149,25 @@ export type GitlabProviderConfig = {
|
||||
* @public
|
||||
*/
|
||||
export type GroupNameTransformer = (
|
||||
group: GitLabGroup,
|
||||
config: GitlabProviderConfig,
|
||||
options: GroupNameTransformerOptions,
|
||||
) => string;
|
||||
|
||||
export interface GroupNameTransformerOptions {
|
||||
group: GitLabGroup;
|
||||
providerConfig: GitlabProviderConfig;
|
||||
}
|
||||
/**
|
||||
* Customize the ingested User entity
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export type UserTransformer = (
|
||||
user: GitLabUser,
|
||||
intConfig: GitLabIntegrationConfig,
|
||||
provConfig: GitlabProviderConfig,
|
||||
groupNameTransformer: GroupNameTransformer,
|
||||
) => UserEntity;
|
||||
export type UserTransformer = (options: UserTransformerOptions) => UserEntity;
|
||||
export interface UserTransformerOptions {
|
||||
user: GitLabUser;
|
||||
integrationConfig: GitLabIntegrationConfig;
|
||||
providerConfig: GitlabProviderConfig;
|
||||
groupNameTransformer: GroupNameTransformer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize the ingested Group entity
|
||||
@@ -171,7 +175,10 @@ export type UserTransformer = (
|
||||
* @public
|
||||
*/
|
||||
export type GroupTransformer = (
|
||||
group: GitLabGroup,
|
||||
provConfig: GitlabProviderConfig,
|
||||
groupNameTransformer: GroupNameTransformer,
|
||||
) => GroupEntity;
|
||||
options: GroupTransformerOptions,
|
||||
) => GroupEntity[];
|
||||
export interface GroupTransformerOptions {
|
||||
groups: GitLabGroup[];
|
||||
providerConfig: GitlabProviderConfig;
|
||||
groupNameTransformer: GroupNameTransformer;
|
||||
}
|
||||
|
||||
+19
-42
@@ -18,7 +18,6 @@ import {
|
||||
ANNOTATION_LOCATION,
|
||||
ANNOTATION_ORIGIN_LOCATION,
|
||||
Entity,
|
||||
GroupEntity,
|
||||
} from '@backstage/catalog-model';
|
||||
import { Config } from '@backstage/config';
|
||||
import { GitLabIntegration, ScmIntegrations } from '@backstage/integration';
|
||||
@@ -41,12 +40,12 @@ import {
|
||||
GitLabUser,
|
||||
PagedResponse,
|
||||
UserTransformer,
|
||||
GroupTransformer,
|
||||
GroupTransformer as GroupEntitiesTransformer,
|
||||
GroupNameTransformer,
|
||||
} from '../lib/types';
|
||||
import {
|
||||
defaultGroupNameTransformer,
|
||||
defaultGroupTransformer,
|
||||
defaultGroupEntitiesTransformer,
|
||||
defaultUserTransformer,
|
||||
} from '../lib/defaultTransformers';
|
||||
|
||||
@@ -71,7 +70,7 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
|
||||
private readonly scheduleFn: () => Promise<void>;
|
||||
private connection?: EntityProviderConnection;
|
||||
private userTransformer: UserTransformer;
|
||||
private groupTransformer: GroupTransformer;
|
||||
private groupEntitiesTransformer: GroupEntitiesTransformer;
|
||||
private groupNameTransformer: GroupNameTransformer;
|
||||
|
||||
static fromConfig(
|
||||
@@ -81,7 +80,7 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
|
||||
schedule?: TaskRunner;
|
||||
scheduler?: PluginTaskScheduler;
|
||||
userTransformer?: UserTransformer;
|
||||
groupTransformer?: GroupTransformer;
|
||||
groupEntitiesTransformer?: GroupEntitiesTransformer;
|
||||
groupNameTransformer?: GroupNameTransformer;
|
||||
},
|
||||
): GitlabOrgDiscoveryEntityProvider[] {
|
||||
@@ -140,7 +139,7 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
|
||||
logger: Logger;
|
||||
taskRunner: TaskRunner;
|
||||
userTransformer?: UserTransformer;
|
||||
groupTransformer?: GroupTransformer;
|
||||
groupEntitiesTransformer?: GroupEntitiesTransformer;
|
||||
groupNameTransformer?: GroupNameTransformer;
|
||||
}) {
|
||||
this.config = options.config;
|
||||
@@ -150,7 +149,8 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
|
||||
});
|
||||
this.scheduleFn = this.createScheduleFn(options.taskRunner);
|
||||
this.userTransformer = options.userTransformer ?? defaultUserTransformer;
|
||||
this.groupTransformer = options.groupTransformer ?? defaultGroupTransformer;
|
||||
this.groupEntitiesTransformer =
|
||||
options.groupEntitiesTransformer ?? defaultGroupEntitiesTransformer;
|
||||
this.groupNameTransformer =
|
||||
options.groupNameTransformer ?? defaultGroupNameTransformer;
|
||||
}
|
||||
@@ -295,14 +295,19 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
|
||||
});
|
||||
|
||||
const userEntities = res.matches.map(p =>
|
||||
this.userTransformer(
|
||||
p,
|
||||
this.integration.config,
|
||||
this.config,
|
||||
this.groupNameTransformer,
|
||||
),
|
||||
this.userTransformer({
|
||||
user: p,
|
||||
integrationConfig: this.integration.config,
|
||||
providerConfig: this.config,
|
||||
groupNameTransformer: this.groupNameTransformer,
|
||||
}),
|
||||
);
|
||||
const groupEntities = this.createGroupEntities(groupsWithUsers);
|
||||
|
||||
const groupEntities = this.groupEntitiesTransformer({
|
||||
groups: groupsWithUsers,
|
||||
providerConfig: this.config,
|
||||
groupNameTransformer: this.groupNameTransformer,
|
||||
});
|
||||
|
||||
await this.connection.applyMutation({
|
||||
type: 'full',
|
||||
@@ -317,34 +322,6 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider {
|
||||
});
|
||||
}
|
||||
|
||||
private createGroupEntities(groupResult: GitLabGroup[]): GroupEntity[] {
|
||||
const idMapped: { [groupId: number]: GitLabGroup } = {};
|
||||
const entities: GroupEntity[] = [];
|
||||
|
||||
for (const group of groupResult) {
|
||||
idMapped[group.id] = group;
|
||||
}
|
||||
|
||||
for (const group of groupResult) {
|
||||
const entity = this.groupTransformer(
|
||||
group,
|
||||
this.config,
|
||||
this.groupNameTransformer,
|
||||
);
|
||||
|
||||
if (group.parent_id && idMapped.hasOwnProperty(group.parent_id)) {
|
||||
entity.spec.parent = this.groupNameTransformer(
|
||||
idMapped[group.parent_id],
|
||||
this.config,
|
||||
);
|
||||
}
|
||||
|
||||
entities.push(entity);
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
private withLocations(host: string, baseUrl: string, entity: Entity): Entity {
|
||||
const location =
|
||||
entity.kind === 'Group'
|
||||
|
||||
Reference in New Issue
Block a user