Skip to content

Commit 4cc507a

Browse files
committed
support any domain and ssh-user when using git deploy
1 parent 513b781 commit 4cc507a

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

src/utils/GitHelper.ts

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ import Utils from './Utils'
1010
const exec = util.promisify(childPross.exec)
1111

1212
export default class GitHelper {
13+
static #SSH_PATH_RE = new RegExp(
14+
[
15+
/^\s*/,
16+
/(?:(?<proto>[a-z]+):\/\/)?/,
17+
/(?:(?<user>[a-z_][a-z0-9_-]+)@)?/,
18+
/(?<domain>[^\s\/\?#:]+)/,
19+
/(?::(?<port>[0-9]{1,5}))?/,
20+
/(?:[\/:](?<owner>[^\s\/\?#:]+))?/,
21+
/(?:[\/:](?<repo>[^\s\/\?#:.]+))/,
22+
/(?:.git)?\/?\s*$/,
23+
]
24+
.map((r) => r.source)
25+
.join(''),
26+
'i'
27+
)
28+
1329
static getLastHash(directory: string) {
1430
return git(directory) //
1531
.silent(true) //
@@ -93,8 +109,7 @@ export default class GitHelper {
93109

94110
// input is like this: ssh://git@github.com:22/caprover/caprover-cli.git
95111
static getDomainFromSanitizedSshRepoPath(input: string) {
96-
input = input.substring(10)
97-
return input.substring(0, input.indexOf(':'))
112+
return GitHelper.sanitizeRepoPathSsh(input).domain
98113
}
99114

100115
// It returns a string like this "github.com/username/repository.git"
@@ -112,40 +127,22 @@ export default class GitHelper {
112127

113128
// It returns a string like this "ssh://git@github.com:22/caprover/caprover-cli.git"
114129
static sanitizeRepoPathSsh(input: string) {
115-
input = Utils.removeHttpHttps(input)
116-
if (!input.startsWith('git@')) {
117-
// If we get here, we have something like github.com/username/repository.git
118-
if (input.indexOf(':') < 0) {
119-
input = input.replace('/', ':')
120-
}
121-
input = `git@${input}`
122-
}
123-
124-
// At this point we have one of the following:
125-
// git@github.com:22/caprover/caprover
126-
// git@github.com:caprover/caprover
127-
128-
let port = '22'
129-
const split = input.split(':')
130-
if (split.length == 2) {
131-
const secondSplit = split[1].split('/')
132-
if (`${Number(secondSplit[0])}` === secondSplit[0]) {
133-
// input is already in this format: git@github.com:22/caprover/caprover
134-
port = `${Number(secondSplit[0])}`
135-
} else {
136-
input = `${split[0]}:22/${split[1]}`
137-
}
138-
} else {
130+
const found = input.match(GitHelper.#SSH_PATH_RE)
131+
if (!found) {
139132
throw new Error(`Malformatted SSH path: ${input}`)
140133
}
141134

142-
if (!input.toLowerCase().startsWith('ssh://')) {
143-
input = `ssh://${input}`
144-
}
145-
146135
return {
147-
repoPath: input.replace(/\/$/, ''),
148-
port: port,
136+
user: found.groups?.user ?? 'git',
137+
domain: found.groups?.domain,
138+
port: Number(found.groups?.port ?? 22),
139+
owner: found.groups?.owner ?? '',
140+
repo: found.groups?.repo,
141+
get repoPath() {
142+
return `ssh://${this.user}@${this.domain}:${this.port}/${
143+
this.owner
144+
}${this.owner && '/'}${this.repo}.git`
145+
},
149146
}
150147
}
151148
}

tests/GitHelper.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ test('Testing - sanitizeRepoPathSsh - port', () => {
2929
GitHelper.sanitizeRepoPathSsh(
3030
' git@github.com:username/repository.git/ '
3131
).port
32-
).toBe('22')
32+
).toBe(22)
3333
})
3434

3535
test('Testing - sanitizeRepoPathSsh - custom port', () => {
3636
expect(
3737
GitHelper.sanitizeRepoPathSsh(
3838
' git@github.com:1234/username/repository.git/ '
3939
).port
40-
).toBe('1234')
40+
).toBe(1234)
4141
})
4242

4343
test('Testing - sanitizeRepoPathSsh from HTTPS', () => {
@@ -48,6 +48,22 @@ test('Testing - sanitizeRepoPathSsh from HTTPS', () => {
4848
).toBe('ssh://git@github.com:22/username/repository.git')
4949
})
5050

51+
test('Testing - sanitizeRepoPathSsh - alt domain', () => {
52+
expect(
53+
GitHelper.sanitizeRepoPathSsh(
54+
' https://gitea@git.alt-domain.com:2221/username/repository/ '
55+
).repoPath
56+
).toBe('ssh://gitea@git.alt-domain.com:2221/username/repository.git')
57+
})
58+
59+
test('Testing - sanitizeRepoPathSsh - no owner', () => {
60+
expect(
61+
GitHelper.sanitizeRepoPathSsh(
62+
' foo@git.alt-domain.com:repository.git '
63+
).repoPath
64+
).toBe('ssh://foo@git.alt-domain.com:22/repository.git')
65+
})
66+
5167
test('Testing - getDomainFromSanitizedSshRepoPath - pure', () => {
5268
expect(
5369
GitHelper.getDomainFromSanitizedSshRepoPath(
@@ -75,3 +91,11 @@ test('Testing - getDomainFromSanitizedSshRepoPath from HTTPS', () => {
7591
)
7692
).toBe('github.com')
7793
})
94+
95+
test('Testing - getDomainFromSanitizedSshRepoPath - alt domain', () => {
96+
expect(
97+
GitHelper.getDomainFromSanitizedSshRepoPath(
98+
' ssh://user@some.do-main.com/owner/repository.git/ '
99+
)
100+
).toBe('some.do-main.com')
101+
})

0 commit comments

Comments
 (0)