fix related to catalog import accepting invalid name

Signed-off-by: npiyush97 <npiyush35@gmail.com>
This commit is contained in:
npiyush97
2024-02-13 19:55:33 +05:30
parent 9545b960bd
commit 71e0812f61
3 changed files with 22 additions and 17 deletions
+1 -1
View File
@@ -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.
@@ -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)
);
}
@@ -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,