refactor(catalog-import): access catalog filename and branchname via hooks and helpers

Signed-off-by: Mikko Korhonen <mikko.korhonen@gmail.com>
This commit is contained in:
Mikko Korhonen
2022-01-25 11:21:53 +02:00
parent 2734946be0
commit 9735b00b6a
8 changed files with 70 additions and 28 deletions
+1
View File
@@ -34,6 +34,7 @@
"@backstage/catalog-client": "^0.5.5",
"@backstage/catalog-model": "^0.9.10",
"@backstage/core-components": "^0.8.6",
"@backstage/config": "^0.1.13",
"@backstage/core-plugin-api": "^0.6.0",
"@backstage/errors": "^0.2.0",
"@backstage/integration": "^0.7.2",
@@ -32,6 +32,7 @@ import { PartialEntity } from '../types';
import { AnalyzeResult, CatalogImportApi } from './CatalogImportApi';
import { getGithubIntegrationConfig } from './GitHub';
import { trimEnd } from 'lodash';
import { getBranchName, getCatalogFilename } from '../components/helpers';
export class CatalogImportClient implements CatalogImportApi {
private readonly discoveryApi: DiscoveryApi;
@@ -87,9 +88,7 @@ export class CatalogImportClient implements CatalogImportApi {
const ghConfig = getGithubIntegrationConfig(this.scmIntegrationsApi, url);
if (!ghConfig) {
const other = this.scmIntegrationsApi.byUrl(url);
const catalogFilename =
this.configApi.getOptionalString('catalog.import.entityFilename') ??
'catalog-info.yaml';
const catalogFilename = getCatalogFilename(this.configApi);
if (other) {
throw new Error(
@@ -131,9 +130,7 @@ export class CatalogImportClient implements CatalogImportApi {
const appTitle =
this.configApi.getOptionalString('app.title') ?? 'Backstage';
const appBaseUrl = this.configApi.getString('app.baseUrl');
const catalogFilename =
this.configApi.getOptionalString('catalog.import.entityFilename') ??
'catalog-info.yaml';
const catalogFilename = getCatalogFilename(this.configApi);
return {
title: `Add ${catalogFilename} config file`,
@@ -228,9 +225,7 @@ the component will become available.\n\nFor more information, read an \
auth: token,
baseUrl: githubIntegrationConfig.apiBaseUrl,
});
const catalogFilename =
this.configApi.getOptionalString('catalog.import.entityFilename') ??
'catalog-info.yaml';
const catalogFilename = getCatalogFilename(this.configApi);
const query = `repo:${owner}/${repo}+filename:${catalogFilename}`;
const searchResult = await octo.search.code({ q: query }).catch(e => {
@@ -303,12 +298,8 @@ the component will become available.\n\nFor more information, read an \
baseUrl: githubIntegrationConfig.apiBaseUrl,
});
const branchName =
this.configApi.getOptionalString('catalog.pullRequestBranchName') ??
'backstage-integration';
const fileName =
this.configApi.getOptionalString('catalog.import.entityFilename') ??
'catalog-info.yaml';
const branchName = getBranchName(this.configApi);
const fileName = getCatalogFilename(this.configApi);
const repoData = await octo.repos
.get({
@@ -19,6 +19,7 @@ import { configApiRef, useApi } from '@backstage/core-plugin-api';
import { Chip, Typography } from '@material-ui/core';
import React from 'react';
import { catalogImportApiRef } from '../../api';
import { useCatalogFilename } from '../../hooks';
type Props = {
exampleLocationUrl?: string;
@@ -36,9 +37,7 @@ export const ImportInfoCard = ({
const integrations = configApi.getConfig('integrations');
const hasGithubIntegration = integrations.has('github');
const catalogFilename =
configApi.getOptionalString('catalog.import.entityFilename') ??
'catalog-info.yaml';
const catalogFilename = useCatalogFilename();
return (
<InfoCard
@@ -19,8 +19,8 @@ import { Card, CardContent, CardHeader } from '@material-ui/core';
import React from 'react';
import YAML from 'yaml';
import { CodeSnippet } from '@backstage/core-components';
import { configApiRef, useApi } from '@backstage/core-plugin-api';
import { trimEnd } from 'lodash';
import { useCatalogFilename } from '../../hooks';
type Props = {
repositoryUrl: string;
@@ -33,10 +33,7 @@ export const PreviewCatalogInfoComponent = ({
entities,
classes,
}: Props) => {
const configApi = useApi(configApiRef);
const catalogFilename =
configApi.getOptionalString('catalog.import.entityFilename') ??
'catalog-info.yaml';
const catalogFilename = useCatalogFilename();
return (
<Card variant="outlined" className={classes?.card}>
@@ -15,7 +15,7 @@
*/
import { Entity } from '@backstage/catalog-model';
import { configApiRef, errorApiRef, useApi } from '@backstage/core-plugin-api';
import { errorApiRef, useApi } from '@backstage/core-plugin-api';
import { assertError } from '@backstage/errors';
import {
catalogApiRef,
@@ -28,6 +28,7 @@ import { UnpackNestedValue, UseFormReturn } from 'react-hook-form';
import useAsync from 'react-use/lib/useAsync';
import YAML from 'yaml';
import { AnalyzeResult, catalogImportApiRef } from '../../api';
import { useCatalogFilename } from '../../hooks';
import { PartialEntity } from '../../types';
import { BackButton, NextButton } from '../Buttons';
import { PrepareResult } from '../useImportState';
@@ -102,14 +103,11 @@ export const StepPrepareCreatePullRequest = ({
const catalogApi = useApi(catalogApiRef);
const catalogImportApi = useApi(catalogImportApiRef);
const errorApi = useApi(errorApiRef);
const configApi = useApi(configApiRef);
const [submitted, setSubmitted] = useState(false);
const [error, setError] = useState<string>();
const catalogFilename =
configApi.getOptionalString('catalog.import.entityFilename') ??
'catalog-info.yaml';
const catalogFilename = useCatalogFilename();
const {
loading: prDefaultsLoading,
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import type { Config } from '@backstage/config';
import { UseFormRegisterReturn } from 'react-hook-form';
/**
@@ -31,3 +32,17 @@ export function asInputRef(renderResult: UseFormRegisterReturn) {
...rest,
};
}
export function getCatalogFilename(config: Config): string {
return (
config.getOptionalString('catalog.import.entityFilename') ??
'catalog-info.yaml'
);
}
export function getBranchName(config: Config): string {
return (
config.getOptionalString('catalog.import.pullRequestBranchName') ??
'backstage-integration'
);
}
+17
View File
@@ -0,0 +1,17 @@
/*
* Copyright 2022 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.
*/
export { useCatalogFilename } from './useCatalogFilename';
@@ -0,0 +1,24 @@
/*
* Copyright 2022 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.
*/
import { useApi, configApiRef } from '@backstage/core-plugin-api';
import { getCatalogFilename } from '../components/helpers';
export function useCatalogFilename(): string {
const config = useApi(configApiRef);
return getCatalogFilename(config);
}