| 
 | 1 | +import * as assert from 'assert';  | 
 | 2 | +import { suite, test } from 'mocha';  | 
 | 3 | +import { getGitLabPullRequestIdentityFromMaybeUrl } from '../gitlab.utils';  | 
 | 4 | + | 
 | 5 | +suite('Test GitLab PR URL parsing to identity: getPullRequestIdentityFromMaybeUrl()', () => {  | 
 | 6 | +	function t(message: string, query: string, prNumber: string | undefined, ownerAndRepo?: string) {  | 
 | 7 | +		assert.deepStrictEqual(  | 
 | 8 | +			getGitLabPullRequestIdentityFromMaybeUrl(query),  | 
 | 9 | +			prNumber == null  | 
 | 10 | +				? undefined  | 
 | 11 | +				: {  | 
 | 12 | +						ownerAndRepo: ownerAndRepo,  | 
 | 13 | +						prNumber: prNumber,  | 
 | 14 | +				  },  | 
 | 15 | +			`${message} (${JSON.stringify(query)})`,  | 
 | 16 | +		);  | 
 | 17 | +	}  | 
 | 18 | + | 
 | 19 | +	test('full URL or without protocol but with domain, should parse to ownerAndRepo and prNumber', () => {  | 
 | 20 | +		t('full URL', 'https://gitlab.com/eamodio/vscode-gitlens/-/merge_requests/1', '1', 'eamodio/vscode-gitlens');  | 
 | 21 | +		t(  | 
 | 22 | +			'with suffix',  | 
 | 23 | +			'https://gitlab.com/eamodio/vscode-gitlens/-/merge_requests/1/files?diff=unified#hello',  | 
 | 24 | +			'1',  | 
 | 25 | +			'eamodio/vscode-gitlens',  | 
 | 26 | +		);  | 
 | 27 | +		t(  | 
 | 28 | +			'with query',  | 
 | 29 | +			'https://gitlab.com/eamodio/vscode-gitlens/-/merge_requests/1?diff=unified#hello',  | 
 | 30 | +			'1',  | 
 | 31 | +			'eamodio/vscode-gitlens',  | 
 | 32 | +		);  | 
 | 33 | + | 
 | 34 | +		t(  | 
 | 35 | +			'with anchor',  | 
 | 36 | +			'https://gitlab.com/eamodio/vscode-gitlens/-/merge_requests/1#hello',  | 
 | 37 | +			'1',  | 
 | 38 | +			'eamodio/vscode-gitlens',  | 
 | 39 | +		);  | 
 | 40 | +		t(  | 
 | 41 | +			'a weird suffix',  | 
 | 42 | +			'https://gitlab.com/eamodio/vscode-gitlens/-/merge_requests/1-files',  | 
 | 43 | +			'1',  | 
 | 44 | +			'eamodio/vscode-gitlens',  | 
 | 45 | +		);  | 
 | 46 | +		t('numeric repo name', 'https://gitlab.com/sergeibbb/1/-/merge_requests/16', '16', 'sergeibbb/1');  | 
 | 47 | + | 
 | 48 | +		t(  | 
 | 49 | +			'no protocol with leading slash',  | 
 | 50 | +			'/gitlab.com/sergeibbb/1/-/merge_requests/16?diff=unified',  | 
 | 51 | +			'16',  | 
 | 52 | +			'sergeibbb/1',  | 
 | 53 | +		);  | 
 | 54 | +		t('no protocol without leading slash', 'gitlab.com/sergeibbb/1/-/merge_requests/16/files', '16', 'sergeibbb/1');  | 
 | 55 | +	});  | 
 | 56 | +	test('no domain, should parse to ownerAndRepo and prNumber', () => {  | 
 | 57 | +		t('with leading slash', '/sergeibbb/1/-/merge_requests/16#hello', '16', 'sergeibbb/1');  | 
 | 58 | +		t(  | 
 | 59 | +			'words in repo name',  | 
 | 60 | +			'eamodio/vscode-gitlens/-/merge_requests/1?diff=unified#hello',  | 
 | 61 | +			'1',  | 
 | 62 | +			'eamodio/vscode-gitlens',  | 
 | 63 | +		);  | 
 | 64 | +		t('numeric repo name', 'sergeibbb/1/-/merge_requests/16/files', '16', 'sergeibbb/1');  | 
 | 65 | +	});  | 
 | 66 | + | 
 | 67 | +	test('domain vs. no domain', () => {  | 
 | 68 | +		t(  | 
 | 69 | +			'with anchor',  | 
 | 70 | +			'https://gitlab.com/eamodio/vscode-gitlens/-/merge_requests/1#hello/sergeibbb/1/-/merge_requests/16',  | 
 | 71 | +			'1',  | 
 | 72 | +			'eamodio/vscode-gitlens',  | 
 | 73 | +		);  | 
 | 74 | +	});  | 
 | 75 | + | 
 | 76 | +	test('numbers', () => {  | 
 | 77 | +		t('has "-/merge_requests/" fragment', '/-/merge_requests/16/files#hello', '16');  | 
 | 78 | +		t('has "-/merge_requests/" fragment with double slash', '1//-/merge_requests/16?diff=unified#hello', '16');  | 
 | 79 | +		t('with leading slash', '/16/files#hello', '16');  | 
 | 80 | +		t('just a number', '16', '16');  | 
 | 81 | +		t('with a hash', '#16', '16');  | 
 | 82 | +	});  | 
 | 83 | + | 
 | 84 | +	test('does not match', () => {  | 
 | 85 | +		t('without leading slash', '16?diff=unified#hello', undefined);  | 
 | 86 | +		t('with leading hash', '/#16/files#hello', undefined);  | 
 | 87 | +		t('number is a part of a word', 'hello16', undefined);  | 
 | 88 | +		t('number is a part of a word', '16hello', undefined);  | 
 | 89 | + | 
 | 90 | +		t('GitHub', 'https://github.com/eamodio/vscode-gitlens/pull/16', '16');  | 
 | 91 | + | 
 | 92 | +		t('with a number', '1/16?diff=unified#hello', '16');  | 
 | 93 | +		t('with a number and slash', '/1/16?diff=unified#hello', '1');  | 
 | 94 | +		t('with a word', 'anything/16?diff=unified#hello', '16');  | 
 | 95 | + | 
 | 96 | +		t(  | 
 | 97 | +			'with a wrong character leading to "-/merge_requests/"',  | 
 | 98 | +			'sergeibbb/1/--/merge_requests/16?diff=unified#hello',  | 
 | 99 | +			'1',  | 
 | 100 | +		);  | 
 | 101 | +		t(  | 
 | 102 | +			'with a wrong character leading to "-/merge_requests/"',  | 
 | 103 | +			'sergeibbb/1--/merge_requests/16?diff=unified#hello',  | 
 | 104 | +			'1',  | 
 | 105 | +		);  | 
 | 106 | +	});  | 
 | 107 | +});  | 
0 commit comments