From 7ac3306e271df385ca11621e113518c57141d6a1 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 15 Aug 2022 13:53:11 +0200 Subject: [PATCH] catalog-backend-module-github: add initial module export for provider MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: blam Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- .../api-report.md | 12 ++++ .../package.json | 2 + .../src/index.ts | 2 + .../src/module.ts | 69 +++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 plugins/catalog-backend-module-github/src/module.ts diff --git a/plugins/catalog-backend-module-github/api-report.md b/plugins/catalog-backend-module-github/api-report.md index 3e4c2cb06b..8e8055bafc 100644 --- a/plugins/catalog-backend-module-github/api-report.md +++ b/plugins/catalog-backend-module-github/api-report.md @@ -3,6 +3,7 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +import { BackendFeature } from '@backstage/backend-plugin-api'; import { CatalogProcessor } from '@backstage/plugin-catalog-backend'; import { CatalogProcessorEmit } from '@backstage/plugin-catalog-backend'; import { Config } from '@backstage/config'; @@ -14,6 +15,7 @@ import { LocationSpec } from '@backstage/plugin-catalog-backend'; import { Logger } from 'winston'; import { ScmIntegrationRegistry } from '@backstage/integration'; import { TaskRunner } from '@backstage/backend-tasks'; +import { TaskScheduleDefinition } from '@backstage/backend-tasks'; // @public export class GithubDiscoveryProcessor implements CatalogProcessor { @@ -58,6 +60,16 @@ export class GitHubEntityProvider implements EntityProvider { refresh(logger: Logger): Promise; } +// @alpha +export const githubEntityProviderCatalogModule: ( + options?: GithubEntityProviderCatalogModuleOptions | undefined, +) => BackendFeature; + +// @alpha +export type GithubEntityProviderCatalogModuleOptions = { + schedule?: TaskScheduleDefinition; +}; + // @public export type GithubMultiOrgConfig = Array<{ name: string; diff --git a/plugins/catalog-backend-module-github/package.json b/plugins/catalog-backend-module-github/package.json index a0b7f10833..88a540077e 100644 --- a/plugins/catalog-backend-module-github/package.json +++ b/plugins/catalog-backend-module-github/package.json @@ -35,11 +35,13 @@ "dependencies": { "@backstage/backend-common": "^0.15.0", "@backstage/backend-tasks": "^0.3.4", + "@backstage/backend-plugin-api": "^0.1.1", "@backstage/catalog-model": "^1.1.0", "@backstage/config": "^1.0.1", "@backstage/errors": "^1.1.0", "@backstage/integration": "^1.3.0", "@backstage/plugin-catalog-backend": "^1.3.1", + "@backstage/plugin-catalog-node": "^1.0.1", "@backstage/types": "^1.0.0", "@octokit/graphql": "^5.0.0", "lodash": "^4.17.21", diff --git a/plugins/catalog-backend-module-github/src/index.ts b/plugins/catalog-backend-module-github/src/index.ts index 22a1167d48..d793d48f1e 100644 --- a/plugins/catalog-backend-module-github/src/index.ts +++ b/plugins/catalog-backend-module-github/src/index.ts @@ -27,3 +27,5 @@ export { GitHubEntityProvider } from './providers/GitHubEntityProvider'; export { GitHubOrgEntityProvider } from './providers/GitHubOrgEntityProvider'; export type { GitHubOrgEntityProviderOptions } from './providers/GitHubOrgEntityProvider'; export type { GithubMultiOrgConfig } from './lib'; +export { githubEntityProviderCatalogModule } from './module'; +export type { GithubEntityProviderCatalogModuleOptions } from './module'; diff --git a/plugins/catalog-backend-module-github/src/module.ts b/plugins/catalog-backend-module-github/src/module.ts new file mode 100644 index 0000000000..4eccef0611 --- /dev/null +++ b/plugins/catalog-backend-module-github/src/module.ts @@ -0,0 +1,69 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + createBackendModule, + loggerToWinstonLogger, + configServiceRef, + loggerServiceRef, + schedulerServiceRef, +} from '@backstage/backend-plugin-api'; +import { TaskScheduleDefinition } from '@backstage/backend-tasks'; +import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node'; +import { GitHubEntityProvider } from './providers/GitHubEntityProvider'; + +/** + * Options for {@link githubEntityProviderCatalogModule}. + * + * @alpha + */ +export type GithubEntityProviderCatalogModuleOptions = { + schedule?: TaskScheduleDefinition; +}; + +/** + * Registers the GitHubEntityProvider with the catalog processing extension point. + * + * @alpha + */ +export const githubEntityProviderCatalogModule = createBackendModule({ + pluginId: 'catalog', + moduleId: 'github-entity-provider', + register(env, options?: GithubEntityProviderCatalogModuleOptions) { + env.registerInit({ + deps: { + config: configServiceRef, + catalog: catalogProcessingExtensionPoint, + logger: loggerServiceRef, + scheduler: schedulerServiceRef, + }, + async init({ config, catalog, logger, scheduler }) { + const scheduleDef = options?.schedule ?? { + frequency: { seconds: 600 }, + timeout: { seconds: 900 }, + initialDelay: { seconds: 3 }, + }; + + catalog.addEntityProvider( + GitHubEntityProvider.fromConfig(config, { + logger: loggerToWinstonLogger(logger), + schedule: scheduler.createScheduledTaskRunner(scheduleDef), + }), + ); + }, + }); + }, +});