Add support for GitHub Enterprise from integration-config

This commit is contained in:
Mikael Östberg
2020-12-08 12:06:58 +01:00
parent 03e264519a
commit 7babbfaf3c
3 changed files with 15 additions and 2 deletions
@@ -16,6 +16,7 @@
import { createApiRef } from '@backstage/core';
import { PartialEntity } from '../util/types';
import { GitHubIntegrationConfig } from '@backstage/integration'
export const catalogImportApiRef = createApiRef<CatalogImportApi>({
id: 'plugin.catalog-import.service',
@@ -27,6 +28,7 @@ export interface CatalogImportApi {
owner: string;
repo: string;
fileContent: string;
githubIntegrationConfig: GitHubIntegrationConfig;
}): Promise<{ link: string; location: string }>;
createRepositoryLocation(options: { location: string }): Promise<void>;
generateEntityDefinitions(options: {
@@ -19,6 +19,7 @@ import { DiscoveryApi, OAuthApi } from '@backstage/core';
import { CatalogImportApi } from './CatalogImportApi';
import { AnalyzeLocationResponse } from '@backstage/plugin-catalog-backend';
import { PartialEntity } from '../util/types';
import { GitHubIntegrationConfig } from '@backstage/integration'
export class CatalogImportClient implements CatalogImportApi {
private readonly discoveryApi: DiscoveryApi;
@@ -91,15 +92,18 @@ export class CatalogImportClient implements CatalogImportApi {
owner,
repo,
fileContent,
githubIntegrationConfig,
}: {
owner: string;
repo: string;
fileContent: string;
githubIntegrationConfig: GitHubIntegrationConfig;
}): Promise<{ link: string; location: string }> {
const token = await this.githubAuthApi.getAccessToken(['repo']);
const octo = new Octokit({
auth: token,
baseUrl: githubIntegrationConfig.apiBaseUrl,
});
const branchName = 'backstage-integration';
@@ -179,7 +183,7 @@ export class CatalogImportClient implements CatalogImportApi {
return {
link: pullRequestResponse.data.html_url,
location: `https://github.com/${owner}/${repo}/blob/${repoData.data.default_branch}/${fileName}`,
location: `https://${githubIntegrationConfig.host}/${owner}/${repo}/blob/${repoData.data.default_branch}/${fileName}`,
};
}
}
@@ -15,15 +15,21 @@
*/
import * as YAML from 'yaml';
import { useApi } from '@backstage/core';
import { useApi, configApiRef } from '@backstage/core';
import { catalogImportApiRef } from '../api/CatalogImportApi';
import { ConfigSpec } from '../components/ImportComponentPage';
import { readGitHubIntegrationConfigs } from '@backstage/integration';
export function useGithubRepos() {
const api = useApi(catalogImportApiRef);
const config = useApi(configApiRef);
const submitPrToRepo = async (selectedRepo: ConfigSpec) => {
const [ownerName, repoName] = selectedRepo.location.split('/').slice(-2);
const configs = readGitHubIntegrationConfigs(
config.getOptionalConfigArray('integrations.github') ?? []
)
const githubIntegrationConfig = configs[0]
const submitPRResponse = await api
.submitPrToRepo({
owner: ownerName,
@@ -31,6 +37,7 @@ export function useGithubRepos() {
fileContent: selectedRepo.config
.map(entity => `---\n${YAML.stringify(entity)}`)
.join('\n'),
githubIntegrationConfig
})
.catch(e => {
throw new Error(`Failed to submit PR to repo:\n${e.message}`);