Used credentialsProvider as to determine which kind of authentication is going to be used instead of adding an accessType top the GitHubIntegrationConfig

This commit is contained in:
ebarrios
2021-02-15 14:45:40 +01:00
parent 7a4d468d97
commit 18ed99c0e5
3 changed files with 9 additions and 11 deletions
+1
View File
@@ -30,6 +30,7 @@
},
"dependencies": {
"@backstage/config": "^0.1.2",
"@backstage/integration": "^0.4.0",
"cross-fetch": "^3.0.6",
"git-url-parse": "^11.4.4",
"@octokit/rest": "^18.0.12",
+4 -3
View File
@@ -16,6 +16,7 @@
import { Config } from '@backstage/config';
import { isValidHost } from '../helpers';
import { GithubCredentialsProvider } from '@backstage/integration';
const GITHUB_HOST = 'github.com';
const GITHUB_API_BASE_URL = 'https://api.github.com';
@@ -60,11 +61,11 @@ export type GitHubIntegrationConfig = {
token?: string;
/**
* The accessType (oAuth|githubApp) to use for requests to this provider.
* The credentialsProvider to use for requests to this provider.
*
* If no user is specified, oAuth access is used.
* If no credentialsProvider is created, undefined is used.
*/
accessType?: string;
credentialsProvider?: GithubCredentialsProvider | undefined;
/**
* The GitHub Apps configuration to use for requests to this provider.
@@ -31,15 +31,13 @@ export class GithubPublisher implements PublisherBase {
config: GitHubIntegrationConfig,
{ repoVisibility }: { repoVisibility: RepoVisibilityOptions },
) {
config.accessType = 'oAuth';
const credentialsProvider = GithubCredentialsProvider.create(config);
let credentialsProvider: GithubCredentialsProvider | undefined = undefined;
if (!config.token) {
if (!config.apps) {
return undefined;
}
config.accessType = 'githubApp';
credentialsProvider = GithubCredentialsProvider.create(config);
}
const githubClient = new Octokit({
@@ -49,7 +47,6 @@ export class GithubPublisher implements PublisherBase {
return new GithubPublisher({
token: config.token || '',
accessType: config.accessType,
credentialsProvider,
client: githubClient,
repoVisibility,
@@ -60,8 +57,7 @@ export class GithubPublisher implements PublisherBase {
constructor(
private readonly config: {
token: string;
accessType: string;
credentialsProvider: GithubCredentialsProvider;
credentialsProvider: GithubCredentialsProvider | undefined;
client: Octokit;
repoVisibility: RepoVisibilityOptions;
apiBaseUrl: string | undefined;
@@ -80,7 +76,7 @@ export class GithubPublisher implements PublisherBase {
password: 'x-oauth-basic',
};
if (this.config.accessType === 'githubApp') {
if (this.config.credentialsProvider) {
this.config.token =
(
await this.config.credentialsProvider.getCredentials({