Skip to content

Commit

Permalink
updated regex for resource name validation and convert to kebabCase i…
Browse files Browse the repository at this point in the history
…f name is not valid
  • Loading branch information
invincibleJai committed Jun 30, 2021
1 parent d3b71bd commit 697a634
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
Expand Up @@ -46,12 +46,14 @@ describe('ValidationUtils', () => {

describe('createComponentName', () => {
const invalidConvertedtoValidNamePair: { [key: string]: string } = {
'0name': 'ocp-0name',
'-name': 'ocp--name',
'name-': 'ocp-name',
'invalid&name': 'ocp-invalidname',
'invalid name': 'ocp-invalidname',
'invalid-Name': 'ocp-invalid-name',
'0name': 'ocp-0-name',
Name: 'name',
'-name': 'name',
'name-': 'name',
'invalid&name': 'invalid-name',
'invalid name': 'invalid-name',
'invalid-Name': 'invalid-name',
InvalidName: 'invalid-name',
};
const validNames: string[] = ['name', 'valid-name', 'name0', 'name-0'];

Expand Down Expand Up @@ -145,6 +147,21 @@ describe('ValidationUtils', () => {
expect(name).toEqual('wild-west-frontend');
});

it('should convert the detected name to valid kebabCase', async () => {
expect(
detectGitRepoName('https://github.com/openshift-evangelists/Wild-West-Frontend'),
).toEqual('wild-west-frontend');
expect(
detectGitRepoName('https://github.com/openshift-evangelists/wildWestFrontend'),
).toEqual('wild-west-frontend');
expect(
detectGitRepoName('https://github.com/openshift-evangelists/wild-west-frontend.git'),
).toEqual('wild-west-frontend-git');
expect(
detectGitRepoName('https://github.com/openshift-evangelists/Wild-West-Frontend123'),
).toEqual('wild-west-frontend-123');
});

it('should throw an error if name is invalid', async () => {
const mockData = cloneDeep(mockFormData);
mockData.name = 'app_name';
Expand Down
Expand Up @@ -19,7 +19,7 @@ import {
NormalizedBuilderImages,
} from '../../../utils/imagestream-utils';
import { GitData, GitReadableTypes, GitTypes } from '../import-types';
import { detectGitType, detectGitRepoName, createComponentName } from '../import-validation-utils';
import { detectGitType, detectGitRepoName } from '../import-validation-utils';
import FormSection from '../section/FormSection';
import AdvancedGitOptions from './AdvancedGitOptions';
import SampleRepo from './SampleRepo';
Expand Down Expand Up @@ -107,10 +107,7 @@ const GitSection: React.FC<GitSectionProps> = ({
return;
}

gitRepoName &&
!nameTouched &&
!values.name &&
setFieldValue('name', createComponentName(gitRepoName));
gitRepoName && !nameTouched && !values.name && setFieldValue('name', gitRepoName);
gitRepoName &&
values.formType !== 'edit' &&
!values.application.name &&
Expand Down Expand Up @@ -192,10 +189,7 @@ const GitSection: React.FC<GitSectionProps> = ({
const handleGitUrlBlur = React.useCallback(() => {
const { url } = values.git;
const gitRepoName = detectGitRepoName(url);
values.formType !== 'edit' &&
gitRepoName &&
!nameTouched &&
setFieldValue('name', createComponentName(gitRepoName));
values.formType !== 'edit' && gitRepoName && !nameTouched && setFieldValue('name', gitRepoName);
gitRepoName &&
values.formType !== 'edit' &&
!values.application.name &&
Expand Down
Expand Up @@ -64,18 +64,18 @@ export const detectGitType = (url: string): string => {
return GitTypes.unsure;
};

export const detectGitRepoName = (url: string): string | undefined => {
if (!gitUrlRegex.test(url)) {
return undefined;
export const createComponentName = (nameString: string): string => {
if (nameRegex.test(nameString)) {
return nameString;
}

return _.kebabCase(url.split('/').pop());
const kebabCaseStr = _.kebabCase(nameString);
return nameString.match(/^\d/) ? `ocp-${kebabCaseStr}` : kebabCaseStr;
};

export const createComponentName = (nameString: string): string => {
const prefixToValidate = 'ocp-';
if (!nameRegex.test(nameString)) {
return `${prefixToValidate}${nameString.replace(/[^-a-zA-Z0-9]|(-*)$/g, '').toLowerCase()}`;
export const detectGitRepoName = (url: string): string | undefined => {
if (!gitUrlRegex.test(url)) {
return undefined;
}
return nameString;
const name = url.split('/').pop();
return createComponentName(name);
};

0 comments on commit 697a634

Please sign in to comment.