Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fleet: validate GitRepo Url #13282

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shell/assets/translations/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6040,6 +6040,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