Skip to content
Open
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
42 changes: 42 additions & 0 deletions apps/web/src/modules/oh/utils/form.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { validateCoderUrl, validateCommitSHA } from './form';

describe('source repository URL validation', () => {
it.each([
'https://github.com/owner/repo',
'https://gitee.com/owner/repo.git',
'https://gitcode.com/owner/repo/',
'http://github.com/owner/repo',
'https://GITHUB.COM/owner/repo',
])('accepts a repository on a supported host: %s', async (value) => {
await expect(validateCoderUrl(null, value)).resolves.toBeUndefined();
});

it.each([
'https://github.com.example.org/owner/repo',
'https://example.org/github.com/owner/repo',
'https://example.org/?repo=https://gitee.com/owner/repo',
'https://github.com@example.org/owner/repo',
'https://notgithub.com/owner/repo',
'ftp://github.com/owner/repo',
'github.com/owner/repo',
'https://github.com',
'https://github.com/owner',
'',
undefined,
])(
'rejects a value that is not a supported repository URL: %s',
async (value) => {
await expect(validateCoderUrl(null, value)).rejects.toThrow(
'请输入正确的Github、Gitee、Gitcode仓库'
);
}
);

it('retains the independent optional commit SHA validation', async () => {
await expect(
validateCommitSHA(null, 'a'.repeat(40))
).resolves.toBeUndefined();
await expect(validateCommitSHA(null, '')).resolves.toBeUndefined();
await expect(validateCommitSHA(null, 'invalid')).rejects.toThrow();
});
});
11 changes: 9 additions & 2 deletions apps/web/src/modules/oh/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ export const validateCommitSHA = (_, value) => {
};
export const validateCoderUrl = (_, value) => {
const validHosts = ['github.com', 'gitee.com', 'gitcode.com'];
for (let host of validHosts) {
if (value?.includes(host)) {
try {
const url = new URL(value);
if (
['http:', 'https:'].includes(url.protocol) &&
validHosts.includes(url.hostname) &&
url.pathname.split('/').filter(Boolean).length >= 2
) {
return Promise.resolve();
}
} catch {
// Malformed or relative URLs cannot identify a supported source repository.
}
return Promise.reject(new Error('请输入正确的Github、Gitee、Gitcode仓库'));
};