Skip to content

Commit

Permalink
feat(gitlab): support new backend system (#62)
Browse files Browse the repository at this point in the history
Add support to install the gitlab user processor via a catalog module
  • Loading branch information
zhammer authored Aug 27, 2024
1 parent 22d68b0 commit 98343ef
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
21 changes: 6 additions & 15 deletions plugins/gitlab-catalog-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,12 @@ Enriches existing `User` entities with information from Gitlab, notably the user

#### Installation

Add the following to your `packages/backend/catalog.ts`:
Add the following to your `packages/backend/index.ts`:

```ts
import { GitlabUserProcessor } from '@seatgeek/backstage-plugin-gitlab-catalog-backend';
export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
const builder = CatalogBuilder.create(env);
builder.addProcessor(
// Add the gitlab user processor
GitlabUserProcessor.fromConfig(env.config, env.logger),
);
const { processingEngine, router } = await builder.build();
processingEngine.start();
return router;
}
// in your imports
import { catalogModuleGitlabUserProcessor } from '@seatgeek/backstage-plugin-gitlab-catalog-backend';
// in your catalog modules
backend.add(catalogModuleGitlabUserProcessor);
```
2 changes: 2 additions & 0 deletions plugins/gitlab-catalog-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
},
"dependencies": {
"@backstage/backend-common": "^0.24.0",
"@backstage/backend-plugin-api": "^0.8.0",
"@backstage/catalog-model": "^1.6.0",
"@backstage/config": "^1.2.0",
"@backstage/plugin-catalog-common": "^1.0.26",
"@backstage/plugin-catalog-node": "^1.12.5",
"@gitbeaker/rest": "^40.0.1",
"@types/express": "*",
"express": "^4.17.1",
Expand Down
11 changes: 7 additions & 4 deletions plugins/gitlab-catalog-backend/src/GitlabUserProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright SeatGeek
* Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms.
*/
import { LoggerService } from '@backstage/backend-plugin-api';
import { Entity, isUserEntity } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import { LocationSpec } from '@backstage/plugin-catalog-common';
Expand All @@ -10,7 +11,6 @@ import type {
CatalogProcessorEmit,
} from '@backstage/plugin-catalog-node';
import { ExpandedUserSchema, Gitlab } from '@gitbeaker/rest';
import { Logger } from 'winston';

const GITLAB_PER_PAGE_LIMIT = 500;
const GITLAB_DEFAULT_HOST = 'https://gitlab.com';
Expand All @@ -23,7 +23,7 @@ const GITLAB_DEFAULT_HOST = 'https://gitlab.com';
*/
export class GitlabUserProcessor implements CatalogProcessor {
private readonly gitlab: InstanceType<typeof Gitlab>;
private readonly logger: Logger;
private readonly logger: LoggerService;
private cacheLoaded: boolean;
private userLookup: Map<string, ExpandedUserSchema>;
// guarantee that users are loaded only once
Expand Down Expand Up @@ -77,7 +77,10 @@ export class GitlabUserProcessor implements CatalogProcessor {
return this.userLookup;
}

static fromConfig(config: Config, logger: Logger): GitlabUserProcessor[] {
static fromConfig(
config: Config,
logger: LoggerService,
): GitlabUserProcessor[] {
const token = config.getOptionalString('gitlabCatalog.token');
if (!token) {
logger.warn(
Expand All @@ -103,7 +106,7 @@ export class GitlabUserProcessor implements CatalogProcessor {
];
}

constructor(gitlab: InstanceType<typeof Gitlab>, logger: Logger) {
constructor(gitlab: InstanceType<typeof Gitlab>, logger: LoggerService) {
this.gitlab = gitlab;
this.logger = logger;
this.userLookup = new Map();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright SeatGeek
* Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms.
*/
import {
coreServices,
createBackendModule,
} from '@backstage/backend-plugin-api';
import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha';
import { GitlabUserProcessor } from './GitlabUserProcessor';

export const catalogModuleGitlabUserProcessor = createBackendModule({
pluginId: 'catalog',
moduleId: 'gitlab-user-processor',
register(env) {
env.registerInit({
deps: {
catalog: catalogProcessingExtensionPoint,
config: coreServices.rootConfig,
logger: coreServices.logger,
},
async init({ catalog, config, logger }) {
const gitlabUserProcessor = GitlabUserProcessor.fromConfig(
config,
logger,
);
catalog.addProcessor(gitlabUserProcessor);
},
});
},
});
1 change: 1 addition & 0 deletions plugins/gitlab-catalog-backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms.
*/
export { GitlabUserProcessor } from './GitlabUserProcessor';
export { catalogModuleGitlabUserProcessor } from './catalogModuleGitlabUserProcessor';

0 comments on commit 98343ef

Please sign in to comment.