discovery processor to use gh creds interface

Signed-off-by: Brian Fletcher <brian@roadie.io>
This commit is contained in:
Brian Fletcher
2021-12-23 15:11:38 +00:00
parent 1fc4105bb0
commit 57261f7e86
7 changed files with 50 additions and 12 deletions
+6 -1
View File
@@ -203,6 +203,11 @@ export interface GithubCredentialsProvider {
getCredentials(opts: { url: string }): Promise<GithubCredentials>;
}
// @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<GithubCredentials>;
}
@@ -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,
+1
View File
@@ -27,6 +27,7 @@ export {
export type {
GithubCredentials,
GithubCredentialsProvider,
GithubCredentialsProviderFactory,
GithubCredentialType,
} from './types';
export { GitHubIntegration, replaceGitHubUrlType } from './GitHubIntegration';
+12
View File
@@ -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<GithubCredentials>;
}
/**
* This allows implementations to be provided to create credentials providers.
*
* @public
*
*/
export type GithubCredentialsProviderFactory = (
config: GitHubIntegrationConfig,
) => GithubCredentialsProvider;
+7 -1
View File
@@ -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)
@@ -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 });
@@ -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,