Skip to content
Draft
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
9 changes: 8 additions & 1 deletion src/cli/commands/test/iac/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,21 @@ export function getProjectNameFromGitUrl(url: string) {
/^(git|https?|ftp):\/\/[^:/]+(:[^/]+)?\/(?<name>.*).git\/?$/,
/^[^@]+@[^:]+:(?<name>.*).git$/,
/^(https?):\/\/github.com\/(?<name>.*)$/,
/^https?:\/\/dev\.azure\.com\/(?<name>[^/]+\/[^/]+\/_git\/[^/]+)/,
/^https?:\/\/ssh\.dev\.azure\.com\/v3\/(?<name>.*)$/,
/^git@ssh\.dev\.azure\.com:v3\/(?<name>.*)$/,
];

const trimmed = url.trim();

for (const regexp of regexps) {
const match = trimmed.match(regexp);

if (match && match.groups) {
if (match?.groups?.name) {
// Only strip "/_git/" if we are dealing with an Azure url
if (url.includes('dev.azure.com')) {
return match.groups.name.replace('/_git/', '/');
}
return match.groups.name;
}
}
Expand Down
78 changes: 42 additions & 36 deletions test/jest/unit/cli/commands/test/iac/meta.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,57 +193,63 @@ describe('buildMeta', () => {
});

describe('getProjectNameFromGitUrl', () => {
const urls = [
const testCases = [
// SSH URLs without ~username expansion, as documented by "git clone".

'ssh://user@host.xz:1234/user/repo.git/',
'ssh://host.xz:1234/user/repo.git/',
'ssh://user@host.xz/user/repo.git/',
'ssh://host.xz/user/repo.git/',
'ssh://user@host.xz:1234/user/repo.git',
'ssh://host.xz:1234/user/repo.git',
'ssh://user@host.xz/user/repo.git',
'ssh://host.xz/user/repo.git',
['ssh://user@host.xz:1234/user/repo.git/', 'user/repo'],
['ssh://host.xz:1234/user/repo.git/', 'user/repo'],
['ssh://user@host.xz/user/repo.git/', 'user/repo'],
['ssh://host.xz/user/repo.git/', 'user/repo'],
['ssh://user@host.xz:1234/user/repo.git', 'user/repo'],
['ssh://host.xz:1234/user/repo.git', 'user/repo'],
['ssh://user@host.xz/user/repo.git', 'user/repo'],
['ssh://host.xz/user/repo.git', 'user/repo'],

// Git URLs without ~username expansion, as documented by "git clone".

'git://host.xz:1234/user/repo.git/',
'git://host.xz/user/repo.git/',
'git://host.xz:1234/user/repo.git',
'git://host.xz/user/repo.git',
['git://host.xz:1234/user/repo.git/', 'user/repo'],
['git://host.xz/user/repo.git/', 'user/repo'],
['git://host.xz:1234/user/repo.git', 'user/repo'],
['git://host.xz/user/repo.git', 'user/repo'],

// HTTP URLs, as documented by "git clone".

'http://host.xz:1234/user/repo.git/',
'http://host.xz/user/repo.git/',
'http://host.xz:1234/user/repo.git',
'http://host.xz/user/repo.git',
['http://host.xz:1234/user/repo.git/', 'user/repo'],
['http://host.xz/user/repo.git/', 'user/repo'],
['http://host.xz:1234/user/repo.git', 'user/repo'],
['http://host.xz/user/repo.git', 'user/repo'],

// HTTPS URLs, as documented by "git clone".

'https://host.xz:1234/user/repo.git/',
'https://host.xz/user/repo.git/',
'https://host.xz:1234/user/repo.git',
'https://host.xz/user/repo.git',
['https://host.xz:1234/user/repo.git/', 'user/repo'],
['https://host.xz/user/repo.git/', 'user/repo'],
['https://host.xz:1234/user/repo.git', 'user/repo'],
['https://host.xz/user/repo.git', 'user/repo'],

// SSH URLs without protocol, as used by GitHub.

'git@github.com:user/repo.git',
['git@github.com:user/repo.git', 'user/repo'],

// Remote URLs set up by 'actions/checkout' in GitHub workflows.

'https://github.com/user/repo',
'http://github.com/user/repo',
['https://github.com/user/repo', 'user/repo'],
['http://github.com/user/repo', 'user/repo'],

// Azure DevOps SSH via Proxy - http://ssh.dev.azure.com/v3/Org/Project/Repo
[
'http://ssh.dev.azure.com/v3/org-user/project/repo',
'org-user/project/repo',
],
// Azure DevOps Standard HTTP - https://dev.azure.com/Org/Project/_git/Repo
[
'https://dev.azure.com/org-user/project/_git/repo',
'org-user/project/repo',
],
// Azure DevOps SSH - git@ssh.dev.azure.com:v3/Org/Project/Repo
['git@ssh.dev.azure.com:v3/org-user/project/repo', 'org-user/project/repo'],

// If everything else fails, the URL should be returned as-is, but trimmed.

'user/repo',
' user/repo',
'user/repo ',
['user/repo', 'user/repo'],
[' user/repo', 'user/repo'],
['user/repo ', 'user/repo'],
];

it.each(urls)('should parse %s', (url) => {
expect(getProjectNameFromGitUrl(url)).toBe('user/repo');
it.each(testCases)('parses "%s" -> "%s"', (url, expected) => {
expect(getProjectNameFromGitUrl(url)).toBe(expected);
});
});

Expand Down