From 71e0812f613398fd8d40d66b0f1979d295a02f31 Mon Sep 17 00:00:00 2001 From: npiyush97 Date: Tue, 13 Feb 2024 19:55:33 +0530 Subject: [PATCH] fix related to catalog import accepting invalid name Signed-off-by: npiyush97 --- .changeset/spotty-carrots-refuse.md | 2 +- .../src/api/CatalogImportClient.ts | 23 +++++++++++++++++-- .../StepPrepareCreatePullRequest.tsx | 14 ----------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/.changeset/spotty-carrots-refuse.md b/.changeset/spotty-carrots-refuse.md index b615c7a302..47dcee7239 100644 --- a/.changeset/spotty-carrots-refuse.md +++ b/.changeset/spotty-carrots-refuse.md @@ -2,4 +2,4 @@ '@backstage/plugin-catalog-import': patch --- -fix related to component name not adhering to kubernetes validobjectname. +fix related to component name not adhering to kubernetes valid object name. diff --git a/plugins/catalog-import/src/api/CatalogImportClient.ts b/plugins/catalog-import/src/api/CatalogImportClient.ts index 7b582c90fa..52582bfe4d 100644 --- a/plugins/catalog-import/src/api/CatalogImportClient.ts +++ b/plugins/catalog-import/src/api/CatalogImportClient.ts @@ -28,6 +28,7 @@ import { ScmAuthApi } from '@backstage/integration-react'; import { Octokit } from '@octokit/rest'; import { Base64 } from 'js-base64'; import { AnalyzeResult, CatalogImportApi } from './CatalogImportApi'; +import YAML from 'yaml'; import { getGithubIntegrationConfig } from './GitHub'; import { getBranchName, getCatalogFilename } from '../components/helpers'; import { AnalyzeLocationResponse } from '@backstage/plugin-catalog-common'; @@ -175,12 +176,21 @@ the component will become available.\n\nFor more information, read an \ body: string; }): Promise<{ link: string; location: string }> { const { repositoryUrl, fileContent, title, body } = options; - + const parseData = YAML.parse(fileContent); + const { + metadata: { name }, + } = parseData; + if (!isValidComponentName(name)) { + throw new Error( + 'Component name: Must start and end with an alphanumeric character, and contain only alphanumeric characters, hyphens, underscores, and periods. Maximum length is 63 characters.', + ); + } const ghConfig = getGithubIntegrationConfig( this.scmIntegrationsApi, repositoryUrl, ); - + // eslint-disable-next-line no-console + console.log({ ghConfig }); if (ghConfig) { return await this.submitGitHubPrToRepo({ ...ghConfig, @@ -352,3 +362,12 @@ function formatHttpErrorMessage( ) { return `${message}, received http response status code ${error.status}: ${error.message}`; } + +function isValidComponentName(componentName: string) { + return ( + typeof componentName === 'string' && + componentName.length >= 1 && + componentName.length <= 63 && + /^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$/.test(componentName) + ); +} diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx index 1614488922..1cbdfce355 100644 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx @@ -143,25 +143,11 @@ export const StepPrepareCreatePullRequest = ( .sort(); }); - const isValidObjectName = (value: unknown): boolean => { - return ( - typeof value === 'string' && - value.length >= 1 && - value.length <= 63 && - /^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$/.test(value) - ); - }; - const handleResult = useCallback( async (data: FormData) => { setSubmitted(true); try { - if (!isValidObjectName(data.componentName)) { - throw new Error( - 'Component name: Must start and end with an alphanumeric character, and contain only alphanumeric characters, hyphens, underscores, and periods. Maximum length is 63 characters.', - ); - } const pr = await catalogImportApi.submitPullRequest({ repositoryUrl: analyzeResult.url, title: data.title,