Merge pull request #3613 from O5ten/fix-enterprise-support-catalog-importer

Add support for GitHub Enterprise in CatalogImporterClient
This commit is contained in:
Fredrik Adelöw
2020-12-10 13:56:41 +01:00
committed by GitHub
4 changed files with 31 additions and 3 deletions
+1
View File
@@ -34,6 +34,7 @@
"@backstage/core": "^0.4.0",
"@backstage/plugin-catalog": "^0.2.6",
"@backstage/plugin-catalog-backend": "^0.4.0",
"@backstage/integration": "^0.1.3",
"@backstage/theme": "^0.2.2",
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
@@ -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,35 @@
*/
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 parseGitUri from 'git-url-parse';
// TODO: (O5ten) Refactor into a core API instead of direct usage like this
// https://github.com/backstage/backstage/pull/3613#issuecomment-7408929430
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 {
name: repoName,
owner: ownerName,
resource: hostname,
} = parseGitUri(selectedRepo.location);
const configs = readGitHubIntegrationConfigs(
config.getOptionalConfigArray('integrations.github') ?? [],
);
const githubIntegrationConfig = configs.find(v => v.host === hostname);
if (!githubIntegrationConfig) {
throw new Error(
`Unable to locate github-integration for repo-location: ${selectedRepo.location}`,
);
}
const submitPRResponse = await api
.submitPrToRepo({
owner: ownerName,
@@ -31,6 +51,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}`);