Skip to content

Commit

Permalink
Merge pull request #13282 from torchiaf/11239-validate-git-repo-url
Browse files Browse the repository at this point in the history
Fleet: validate GitRepo Url
  • Loading branch information
torchiaf authored Feb 5, 2025
2 parents 90a2cdb + 2ffa22c commit 29e4b82
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions shell/assets/translations/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6042,6 +6042,8 @@ validation:
flowOutput:
both: Requires "Output" or "Cluster Output" to be selected.
global: Requires "Cluster Output" to be selected.
git:
repository: Repository URL must be a HTTP(s) or SSH url with no trailing spaces
output:
logdna:
apiKey: Required an "Api Key" to be set.
Expand Down
8 changes: 7 additions & 1 deletion shell/edit/fleet.cattle.io.gitrepo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ export default {
stepRepoInfo,
stepTargetInfo,
displayHelmRepoURLRegex: false,
fvFormRuleSets: [{ path: 'spec.repo', rules: ['required'] }]
fvFormRuleSets: [{
path: 'spec.repo',
rules: [
'required',
'gitRepository'
],
}]
};
},
Expand Down
39 changes: 39 additions & 0 deletions shell/utils/validators/formRules/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,45 @@ describe('formRules', () => {
);
});

describe('gitRepository', () => {
const message = JSON.stringify({ message: 'validation.git.repository' });
const testCases = [
// Valid HTTP(s)
['https://github.com/rancher/dashboard.git', undefined],
['http://github.com/rancher/dashboard.git', undefined],
['https://github.com/rancher/dashboard', undefined],
['https://github.com/rancher/dashboard/', undefined],

// Valid SSH
['[email protected]:rancher/dashboard.git', undefined],
['[email protected]:rancher/dashboard', undefined],
['[email protected]:rancher/dashboard/', undefined],

// Not valid HTTP(s)
['https://github.com/rancher/ dashboard.git', message],
['http://github.com/rancher/ dashboard.git', message],
['https://github.com/rancher/dashboard ', message],
['foo://github.com/rancher/dashboard/', message],
['github.com/rancher/dashboard/', message],

// Not valid SSH
['[email protected]:rancher/ dashboard.git', message],
['[email protected]:rancher/dashboard ', message],
['[email protected]/dashboard', message],

[undefined, undefined]
];

it.each(testCases)(
'should return undefined or correct message based on the provided Git url: %p',
(url, expected) => {
const formRuleResult = formRules.gitRepository(url);

expect(formRuleResult).toStrictEqual(expected);
}
);
});

describe('alphanumeric', () => {
const message = JSON.stringify({ message: 'validation.alphanumeric', key: 'testDisplayKey' });
const testCases = [
Expand Down
3 changes: 3 additions & 0 deletions shell/utils/validators/formRules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export default function(t: Translation, { key = 'Value' }: ValidationOptions): {

const url: Validator = (val: string) => val && !isUrl(val) ? t('validation.setting.serverUrl.url') : undefined;

const gitRepository: Validator = (val: string) => val && !/^((http|git|ssh|http(s)|file|\/?)|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:\/\-]+)([\d\/\w.-]+?)(.git){0,1}(\/)?$/gm.test(val) ? t('validation.git.repository') : undefined;

const alphanumeric: Validator = (val: string) => val && !/^[a-zA-Z0-9]+$/.test(val) ? t('validation.alphanumeric', { key }) : undefined;

const interval: Validator = (val: string) => !/^\d+[hms]$/.test(val) ? t('validation.monitoring.route.interval', { key }) : undefined;
Expand Down Expand Up @@ -486,6 +488,7 @@ export default function(t: Translation, { key = 'Value' }: ValidationOptions): {
dnsLabelRestricted,
externalName,
fileRequired,
gitRepository,
groupsAreValid,
hostname,
imageUrl,
Expand Down

0 comments on commit 29e4b82

Please sign in to comment.