From 9735b00b6aa634d6541cf8ca7aa71c2b686f2eec Mon Sep 17 00:00:00 2001 From: Mikko Korhonen Date: Tue, 25 Jan 2022 11:21:53 +0200 Subject: [PATCH] refactor(catalog-import): access catalog filename and branchname via hooks and helpers Signed-off-by: Mikko Korhonen --- plugins/catalog-import/package.json | 1 + .../src/api/CatalogImportClient.ts | 21 +++++----------- .../ImportInfoCard/ImportInfoCard.tsx | 5 ++-- .../PreviewCatalogInfoComponent.tsx | 7 ++---- .../StepPrepareCreatePullRequest.tsx | 8 +++---- .../catalog-import/src/components/helpers.ts | 15 ++++++++++++ plugins/catalog-import/src/hooks/index.ts | 17 +++++++++++++ .../src/hooks/useCatalogFilename.ts | 24 +++++++++++++++++++ 8 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 plugins/catalog-import/src/hooks/index.ts create mode 100644 plugins/catalog-import/src/hooks/useCatalogFilename.ts diff --git a/plugins/catalog-import/package.json b/plugins/catalog-import/package.json index 5097f5cc9b..2508543541 100644 --- a/plugins/catalog-import/package.json +++ b/plugins/catalog-import/package.json @@ -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", diff --git a/plugins/catalog-import/src/api/CatalogImportClient.ts b/plugins/catalog-import/src/api/CatalogImportClient.ts index 218814d2fb..60745923b4 100644 --- a/plugins/catalog-import/src/api/CatalogImportClient.ts +++ b/plugins/catalog-import/src/api/CatalogImportClient.ts @@ -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({ diff --git a/plugins/catalog-import/src/components/ImportInfoCard/ImportInfoCard.tsx b/plugins/catalog-import/src/components/ImportInfoCard/ImportInfoCard.tsx index 4d78542e60..3e66e137eb 100644 --- a/plugins/catalog-import/src/components/ImportInfoCard/ImportInfoCard.tsx +++ b/plugins/catalog-import/src/components/ImportInfoCard/ImportInfoCard.tsx @@ -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 ( { - const configApi = useApi(configApiRef); - const catalogFilename = - configApi.getOptionalString('catalog.import.entityFilename') ?? - 'catalog-info.yaml'; + const catalogFilename = useCatalogFilename(); return ( diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx index d511294b61..a79caaa0b9 100644 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx @@ -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(); - const catalogFilename = - configApi.getOptionalString('catalog.import.entityFilename') ?? - 'catalog-info.yaml'; + const catalogFilename = useCatalogFilename(); const { loading: prDefaultsLoading, diff --git a/plugins/catalog-import/src/components/helpers.ts b/plugins/catalog-import/src/components/helpers.ts index 4240186459..b134a8726a 100644 --- a/plugins/catalog-import/src/components/helpers.ts +++ b/plugins/catalog-import/src/components/helpers.ts @@ -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' + ); +} diff --git a/plugins/catalog-import/src/hooks/index.ts b/plugins/catalog-import/src/hooks/index.ts new file mode 100644 index 0000000000..afdfb0cbe2 --- /dev/null +++ b/plugins/catalog-import/src/hooks/index.ts @@ -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'; diff --git a/plugins/catalog-import/src/hooks/useCatalogFilename.ts b/plugins/catalog-import/src/hooks/useCatalogFilename.ts new file mode 100644 index 0000000000..0e2d770d3e --- /dev/null +++ b/plugins/catalog-import/src/hooks/useCatalogFilename.ts @@ -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); +}