Skip to content

Commit aaed25b

Browse files
authored
fix: npm repository url gap (CM-1305) (#4334)
Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>
1 parent a934419 commit aaed25b

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

services/apps/packages_worker/src/utils/__tests__/canonicalizeRepoUrl.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ describe('canonicalizeRepoUrl', () => {
2222
['gitlab:group/project', 'https://gitlab.com/group/project', 'gitlab'],
2323
['bitbucket:team/repo', 'https://bitbucket.org/team/repo', 'bitbucket'],
2424
['https://example.com/owner/repo', 'https://example.com/owner/repo', 'other'],
25+
['git+https://github.com/1aGh/md-claude.git', 'https://github.com/1agh/md-claude', 'github'],
26+
[
27+
'ssh://git@github.com:1inch/limit-order-protocol-utils.git',
28+
'https://github.com/1inch/limit-order-protocol-utils',
29+
'github',
30+
],
31+
['ssh://git@github.com:2222/foo/bar.git', 'https://github.com/foo/bar', 'github'],
2532
])('canonicalizes %s', (input, expectedUrl, expectedHost) => {
2633
expect(canonicalizeRepoUrl(input)).toEqual({ url: expectedUrl, host: expectedHost })
2734
})
@@ -44,10 +51,14 @@ describe('canonicalizeRepoUrl', () => {
4451
})
4552
})
4653

47-
it.each([['not a url'], ['https://github.com/onlyowner'], [''], [' ']])(
48-
'returns null for unparseable input %s',
49-
(input) => {
50-
expect(canonicalizeRepoUrl(input)).toBeNull()
51-
},
52-
)
54+
it.each([
55+
['not a url'],
56+
['https://github.com/onlyowner'],
57+
[''],
58+
[' '],
59+
['123'],
60+
['https://github.com/Wscats'],
61+
])('returns null for unparseable input %s', (input) => {
62+
expect(canonicalizeRepoUrl(input)).toBeNull()
63+
})
5364
})

services/apps/packages_worker/src/utils/canonicalizeRepoUrl.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,18 @@ export function canonicalizeRepoUrl(raw: string): CanonicalRepo | null {
5454
s = `https://${scp[1]}/${scp[2]}`
5555
}
5656

57-
s = s.replace(/^ssh:\/\/git@([^/]+)\//, 'https://$1/')
57+
// ssh:// with an scp-style `host:path` (colon instead of slash) is not valid URL
58+
// syntax — the part after `:` looks like a port to the URL parser and throws.
59+
// Rewrite it before the generic ssh://git@host/path case below. A numeric-only
60+
// segment before the next `/` is a real port (e.g. `ssh://git@host:2222/owner/repo`),
61+
// not an scp-style owner — leave those for the generic case, which URL parses fine.
62+
const sshScp = s.match(/^ssh:\/\/git@([^/:]+):(.+)$/)
63+
const sshScpIsPort = sshScp ? /^\d+$/.test(sshScp[2].split('/')[0]) : false
64+
if (sshScp && !sshScpIsPort) {
65+
s = `https://${sshScp[1]}/${sshScp[2]}`
66+
} else {
67+
s = s.replace(/^ssh:\/\/git@([^/]+)\//, 'https://$1/')
68+
}
5869
s = s.replace(/^git:\/\//, 'https://')
5970

6071
let u: URL

0 commit comments

Comments
 (0)