Merge pull request #8533 from RoadieHQ/gh-creds-interface

create an interface for gh creds provider
This commit is contained in:
Patrik Oldsberg
2021-12-23 14:29:17 +01:00
committed by GitHub
18 changed files with 136 additions and 59 deletions
@@ -16,6 +16,7 @@
import {
getGitHubFileFetchUrl,
SingleInstanceGithubCredentialsProvider,
GithubCredentialsProvider,
GitHubIntegration,
ScmIntegrations,
@@ -58,9 +59,8 @@ export class GithubUrlReader implements UrlReader {
static factory: ReaderFactory = ({ config, treeResponseFactory }) => {
const integrations = ScmIntegrations.fromConfig(config);
return integrations.github.list().map(integration => {
const credentialsProvider = GithubCredentialsProvider.create(
integration.config,
);
const credentialsProvider =
SingleInstanceGithubCredentialsProvider.create(integration.config);
const reader = new GithubUrlReader(integration, {
treeResponseFactory,
credentialsProvider,
+10 -2
View File
@@ -198,9 +198,8 @@ export type GithubCredentials = {
};
// @public
export class GithubCredentialsProvider {
export interface GithubCredentialsProvider {
// (undocumented)
static create(config: GitHubIntegrationConfig): GithubCredentialsProvider;
getCredentials(opts: { url: string }): Promise<GithubCredentials>;
}
@@ -423,6 +422,15 @@ export interface ScmIntegrationsGroup<T extends ScmIntegration> {
list(): T[];
}
// @public
export class SingleInstanceGithubCredentialsProvider
implements GithubCredentialsProvider
{
// (undocumented)
static create: (config: GitHubIntegrationConfig) => GithubCredentialsProvider;
getCredentials(opts: { url: string }): Promise<GithubCredentials>;
}
// Warnings were encountered during analysis:
//
// src/gitlab/config.d.ts:29:68 - (tsdoc-escape-right-brace) The "}" character should be escaped using a backslash to avoid confusion with a TSDoc inline tag
@@ -31,11 +31,11 @@ jest.doMock('@octokit/rest', () => {
return { Octokit };
});
import { GithubCredentialsProvider } from './GithubCredentialsProvider';
import { SingleInstanceGithubCredentialsProvider } from './SingleInstanceGithubCredentialsProvider';
import { RestEndpointMethodTypes } from '@octokit/rest';
import { DateTime } from 'luxon';
const github = GithubCredentialsProvider.create({
const github = SingleInstanceGithubCredentialsProvider.create({
host: 'github.com',
apps: [
{
@@ -49,7 +49,7 @@ const github = GithubCredentialsProvider.create({
token: 'hardcoded_token',
});
describe('GithubCredentialsProvider tests', () => {
describe('DefaultGithubCredentialsProvider tests', () => {
beforeEach(() => {
jest.resetAllMocks();
});
@@ -204,7 +204,7 @@ describe('GithubCredentialsProvider tests', () => {
});
it('should return the default token if no app is configured', async () => {
const githubProvider = GithubCredentialsProvider.create({
const githubProvider = SingleInstanceGithubCredentialsProvider.create({
host: 'github.com',
apps: [],
token: 'fallback_token',
@@ -218,7 +218,7 @@ describe('GithubCredentialsProvider tests', () => {
});
it('should return the configured token if there are no installations', async () => {
const githubProvider = GithubCredentialsProvider.create({
const githubProvider = SingleInstanceGithubCredentialsProvider.create({
host: 'github.com',
apps: [
{
@@ -243,7 +243,7 @@ describe('GithubCredentialsProvider tests', () => {
});
it('should return undefined if no token or apps are configured', async () => {
const githubProvider = GithubCredentialsProvider.create({
const githubProvider = SingleInstanceGithubCredentialsProvider.create({
host: 'github.com',
});
@@ -19,6 +19,11 @@ import { GithubAppConfig, GitHubIntegrationConfig } from './config';
import { createAppAuth } from '@octokit/auth-app';
import { Octokit, RestEndpointMethodTypes } from '@octokit/rest';
import { DateTime } from 'luxon';
import {
GithubCredentials,
GithubCredentialsProvider,
GithubCredentialType,
} from './types';
type InstallationData = {
installationId: number;
@@ -215,24 +220,6 @@ export class GithubAppCredentialsMux {
}
}
/**
* The type of credentials produced by the credential provider.
*
* @public
*/
export type GithubCredentialType = 'app' | 'token';
/**
* A set of credentials information for a GitHub integration.
*
* @public
*/
export type GithubCredentials = {
headers?: { [name: string]: string };
token?: string;
type: GithubCredentialType;
};
/**
* Handles the creation and caching of credentials for GitHub integrations.
*
@@ -241,13 +228,17 @@ export type GithubCredentials = {
*
* TODO: Possibly move this to a backend only package so that it's not used in the frontend by mistake
*/
export class GithubCredentialsProvider {
static create(config: GitHubIntegrationConfig): GithubCredentialsProvider {
return new GithubCredentialsProvider(
export class SingleInstanceGithubCredentialsProvider
implements GithubCredentialsProvider
{
static create: (
config: GitHubIntegrationConfig,
) => GithubCredentialsProvider = config => {
return new SingleInstanceGithubCredentialsProvider(
new GithubAppCredentialsMux(config),
config.token,
);
}
};
private constructor(
private readonly githubAppCredentialsMux: GithubAppCredentialsMux,
+1 -1
View File
@@ -16,7 +16,7 @@
import { GitHubIntegrationConfig } from './config';
import { getGitHubFileFetchUrl, getGitHubRequestOptions } from './core';
import { GithubCredentials } from './GithubCredentialsProvider';
import { GithubCredentials } from './types';
describe('github core', () => {
const appCredentials: GithubCredentials = {
+1 -1
View File
@@ -16,7 +16,7 @@
import parseGitUrl from 'git-url-parse';
import { GitHubIntegrationConfig } from './config';
import { GithubCredentials } from './GithubCredentialsProvider';
import { GithubCredentials } from './types';
/**
* Given a URL pointing to a file on a provider, returns a URL that is suitable
+4 -3
View File
@@ -22,10 +22,11 @@ export type { GithubAppConfig, GitHubIntegrationConfig } from './config';
export { getGitHubFileFetchUrl, getGitHubRequestOptions } from './core';
export {
GithubAppCredentialsMux,
GithubCredentialsProvider,
} from './GithubCredentialsProvider';
SingleInstanceGithubCredentialsProvider,
} from './SingleInstanceGithubCredentialsProvider';
export type {
GithubCredentials,
GithubCredentialsProvider,
GithubCredentialType,
} from './GithubCredentialsProvider';
} from './types';
export { GitHubIntegration, replaceGitHubUrlType } from './GitHubIntegration';
+43
View File
@@ -0,0 +1,43 @@
/*
* Copyright 2021 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.
*/
/**
* The type of credentials produced by the credential provider.
*
* @public
*/
export type GithubCredentialType = 'app' | 'token';
/**
* A set of credentials information for a GitHub integration.
*
* @public
*/
export type GithubCredentials = {
headers?: { [name: string]: string };
token?: string;
type: GithubCredentialType;
};
/**
* This allows implementations to be provided to retrieve GitHub credentials.
*
* @public
*
*/
export interface GithubCredentialsProvider {
getCredentials(opts: { url: string }): Promise<GithubCredentials>;
}