From 57261f7e86940184853531167cde943c407d4cf7 Mon Sep 17 00:00:00 2001 From: Brian Fletcher Date: Thu, 23 Dec 2021 15:11:38 +0000 Subject: [PATCH] discovery processor to use gh creds interface Signed-off-by: Brian Fletcher --- packages/integration/api-report.md | 7 +++++- ...SingleInstanceGithubCredentialsProvider.ts | 5 ++-- packages/integration/src/github/index.ts | 1 + packages/integration/src/github/types.ts | 12 ++++++++++ plugins/catalog-backend/api-report.md | 8 ++++++- .../processors/GithubDiscoveryProcessor.ts | 24 ++++++++++++++++--- .../providers/GitHubOrgEntityProvider.ts | 5 +--- 7 files changed, 50 insertions(+), 12 deletions(-) diff --git a/packages/integration/api-report.md b/packages/integration/api-report.md index 00623dc917..ca24848db8 100644 --- a/packages/integration/api-report.md +++ b/packages/integration/api-report.md @@ -203,6 +203,11 @@ export interface GithubCredentialsProvider { getCredentials(opts: { url: string }): Promise; } +// @public +export type GithubCredentialsProviderFactory = ( + config: GitHubIntegrationConfig, +) => GithubCredentialsProvider; + // @public export type GithubCredentialType = 'app' | 'token'; @@ -427,7 +432,7 @@ export class SingleInstanceGithubCredentialsProvider implements GithubCredentialsProvider { // (undocumented) - static create: (config: GitHubIntegrationConfig) => GithubCredentialsProvider; + static create: GithubCredentialsProviderFactory; getCredentials(opts: { url: string }): Promise; } diff --git a/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts b/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts index 408c5b1348..5b6fa39e75 100644 --- a/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts +++ b/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts @@ -22,6 +22,7 @@ import { DateTime } from 'luxon'; import { GithubCredentials, GithubCredentialsProvider, + GithubCredentialsProviderFactory, GithubCredentialType, } from './types'; @@ -231,9 +232,7 @@ export class GithubAppCredentialsMux { export class SingleInstanceGithubCredentialsProvider implements GithubCredentialsProvider { - static create: ( - config: GitHubIntegrationConfig, - ) => GithubCredentialsProvider = config => { + static create: GithubCredentialsProviderFactory = config => { return new SingleInstanceGithubCredentialsProvider( new GithubAppCredentialsMux(config), config.token, diff --git a/packages/integration/src/github/index.ts b/packages/integration/src/github/index.ts index 429a2b1c76..fa8dfb4b86 100644 --- a/packages/integration/src/github/index.ts +++ b/packages/integration/src/github/index.ts @@ -27,6 +27,7 @@ export { export type { GithubCredentials, GithubCredentialsProvider, + GithubCredentialsProviderFactory, GithubCredentialType, } from './types'; export { GitHubIntegration, replaceGitHubUrlType } from './GitHubIntegration'; diff --git a/packages/integration/src/github/types.ts b/packages/integration/src/github/types.ts index 15f5375570..d8c1f0ee5e 100644 --- a/packages/integration/src/github/types.ts +++ b/packages/integration/src/github/types.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +import { GitHubIntegrationConfig } from './config'; + /** * The type of credentials produced by the credential provider. * @@ -41,3 +43,13 @@ export type GithubCredentials = { export interface GithubCredentialsProvider { getCredentials(opts: { url: string }): Promise; } + +/** + * This allows implementations to be provided to create credentials providers. + * + * @public + * + */ +export type GithubCredentialsProviderFactory = ( + config: GitHubIntegrationConfig, +) => GithubCredentialsProvider; diff --git a/plugins/catalog-backend/api-report.md b/plugins/catalog-backend/api-report.md index f773905ca0..13a7ec9a9e 100644 --- a/plugins/catalog-backend/api-report.md +++ b/plugins/catalog-backend/api-report.md @@ -16,6 +16,7 @@ import { EntityName } from '@backstage/catalog-model'; import { EntityPolicy } from '@backstage/catalog-model'; import { EntityRelationSpec } from '@backstage/catalog-model'; import express from 'express'; +import { GithubCredentialsProviderFactory } from '@backstage/integration'; import { GitHubIntegrationConfig } from '@backstage/integration'; import { IndexableDocument } from '@backstage/search-common'; import { JsonObject } from '@backstage/types'; @@ -1008,12 +1009,17 @@ function generalError( // // @public export class GithubDiscoveryProcessor implements CatalogProcessor { - constructor(options: { integrations: ScmIntegrations; logger: Logger_2 }); + constructor(options: { + integrations: ScmIntegrations; + logger: Logger_2; + githubCredentialsProviderFactory: GithubCredentialsProviderFactory; + }); // (undocumented) static fromConfig( config: Config, options: { logger: Logger_2; + githubCredentialsProviderFactory?: GithubCredentialsProviderFactory; }, ): GithubDiscoveryProcessor; // (undocumented) diff --git a/plugins/catalog-backend/src/ingestion/processors/GithubDiscoveryProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/GithubDiscoveryProcessor.ts index 046455c484..b1a1a948ba 100644 --- a/plugins/catalog-backend/src/ingestion/processors/GithubDiscoveryProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/GithubDiscoveryProcessor.ts @@ -19,6 +19,7 @@ import { Config } from '@backstage/config'; import { SingleInstanceGithubCredentialsProvider, ScmIntegrations, + GithubCredentialsProviderFactory, } from '@backstage/integration'; import { graphql } from '@octokit/graphql'; import { Logger } from 'winston'; @@ -43,19 +44,36 @@ import { CatalogProcessor, CatalogProcessorEmit } from './types'; export class GithubDiscoveryProcessor implements CatalogProcessor { private readonly integrations: ScmIntegrations; private readonly logger: Logger; + private githubCredentialsProviderFactory: GithubCredentialsProviderFactory; - static fromConfig(config: Config, options: { logger: Logger }) { + static fromConfig( + config: Config, + options: { + logger: Logger; + githubCredentialsProviderFactory?: GithubCredentialsProviderFactory; + }, + ) { const integrations = ScmIntegrations.fromConfig(config); + const githubCredentialsProviderFactory = + options.githubCredentialsProviderFactory || + SingleInstanceGithubCredentialsProvider.create; return new GithubDiscoveryProcessor({ ...options, integrations, + githubCredentialsProviderFactory, }); } - constructor(options: { integrations: ScmIntegrations; logger: Logger }) { + constructor(options: { + integrations: ScmIntegrations; + logger: Logger; + githubCredentialsProviderFactory: GithubCredentialsProviderFactory; + }) { this.integrations = options.integrations; this.logger = options.logger; + this.githubCredentialsProviderFactory = + options.githubCredentialsProviderFactory; } async readLocation( @@ -84,7 +102,7 @@ export class GithubDiscoveryProcessor implements CatalogProcessor { // about how to handle the wild card which is special for this processor. const orgUrl = `https://${host}/${org}`; - const { headers } = await SingleInstanceGithubCredentialsProvider.create( + const { headers } = await this.githubCredentialsProviderFactory( gitHubConfig, ).getCredentials({ url: orgUrl }); diff --git a/plugins/catalog-backend/src/ingestion/providers/GitHubOrgEntityProvider.ts b/plugins/catalog-backend/src/ingestion/providers/GitHubOrgEntityProvider.ts index b8059a783c..7d49a295ef 100644 --- a/plugins/catalog-backend/src/ingestion/providers/GitHubOrgEntityProvider.ts +++ b/plugins/catalog-backend/src/ingestion/providers/GitHubOrgEntityProvider.ts @@ -28,10 +28,7 @@ import { import { graphql } from '@octokit/graphql'; import { merge } from 'lodash'; import { Logger } from 'winston'; -import { - EntityProvider, - EntityProviderConnection, -} from '../../providers/types'; +import { EntityProvider, EntityProviderConnection } from '../../providers'; import { getOrganizationTeams, getOrganizationUsers,