support any domain and ssh-user when using git deploy

This commit is contained in:
Nico Beierle
2021-11-05 16:43:12 +01:00
parent 513b781038
commit 4cc507ad4b
2 changed files with 55 additions and 34 deletions

View File

@@ -10,6 +10,22 @@ import Utils from './Utils'
const exec = util.promisify(childPross.exec) const exec = util.promisify(childPross.exec)
export default class GitHelper { export default class GitHelper {
static #SSH_PATH_RE = new RegExp(
[
/^\s*/,
/(?:(?<proto>[a-z]+):\/\/)?/,
/(?:(?<user>[a-z_][a-z0-9_-]+)@)?/,
/(?<domain>[^\s\/\?#:]+)/,
/(?::(?<port>[0-9]{1,5}))?/,
/(?:[\/:](?<owner>[^\s\/\?#:]+))?/,
/(?:[\/:](?<repo>[^\s\/\?#:.]+))/,
/(?:.git)?\/?\s*$/,
]
.map((r) => r.source)
.join(''),
'i'
)
static getLastHash(directory: string) { static getLastHash(directory: string) {
return git(directory) // return git(directory) //
.silent(true) // .silent(true) //
@@ -93,8 +109,7 @@ export default class GitHelper {
// input is like this: ssh://git@github.com:22/caprover/caprover-cli.git // input is like this: ssh://git@github.com:22/caprover/caprover-cli.git
static getDomainFromSanitizedSshRepoPath(input: string) { static getDomainFromSanitizedSshRepoPath(input: string) {
input = input.substring(10) return GitHelper.sanitizeRepoPathSsh(input).domain
return input.substring(0, input.indexOf(':'))
} }
// It returns a string like this "github.com/username/repository.git" // It returns a string like this "github.com/username/repository.git"
@@ -112,40 +127,22 @@ export default class GitHelper {
// It returns a string like this "ssh://git@github.com:22/caprover/caprover-cli.git" // It returns a string like this "ssh://git@github.com:22/caprover/caprover-cli.git"
static sanitizeRepoPathSsh(input: string) { static sanitizeRepoPathSsh(input: string) {
input = Utils.removeHttpHttps(input) const found = input.match(GitHelper.#SSH_PATH_RE)
if (!input.startsWith('git@')) { if (!found) {
// If we get here, we have something like github.com/username/repository.git
if (input.indexOf(':') < 0) {
input = input.replace('/', ':')
}
input = `git@${input}`
}
// At this point we have one of the following:
// git@github.com:22/caprover/caprover
// git@github.com:caprover/caprover
let port = '22'
const split = input.split(':')
if (split.length == 2) {
const secondSplit = split[1].split('/')
if (`${Number(secondSplit[0])}` === secondSplit[0]) {
// input is already in this format: git@github.com:22/caprover/caprover
port = `${Number(secondSplit[0])}`
} else {
input = `${split[0]}:22/${split[1]}`
}
} else {
throw new Error(`Malformatted SSH path: ${input}`) throw new Error(`Malformatted SSH path: ${input}`)
} }
if (!input.toLowerCase().startsWith('ssh://')) {
input = `ssh://${input}`
}
return { return {
repoPath: input.replace(/\/$/, ''), user: found.groups?.user ?? 'git',
port: port, domain: found.groups?.domain,
port: Number(found.groups?.port ?? 22),
owner: found.groups?.owner ?? '',
repo: found.groups?.repo,
get repoPath() {
return `ssh://${this.user}@${this.domain}:${this.port}/${
this.owner
}${this.owner && '/'}${this.repo}.git`
},
} }
} }
} }

View File

@@ -29,7 +29,7 @@ test('Testing - sanitizeRepoPathSsh - port', () => {
GitHelper.sanitizeRepoPathSsh( GitHelper.sanitizeRepoPathSsh(
' git@github.com:username/repository.git/ ' ' git@github.com:username/repository.git/ '
).port ).port
).toBe('22') ).toBe(22)
}) })
test('Testing - sanitizeRepoPathSsh - custom port', () => { test('Testing - sanitizeRepoPathSsh - custom port', () => {
@@ -37,7 +37,7 @@ test('Testing - sanitizeRepoPathSsh - custom port', () => {
GitHelper.sanitizeRepoPathSsh( GitHelper.sanitizeRepoPathSsh(
' git@github.com:1234/username/repository.git/ ' ' git@github.com:1234/username/repository.git/ '
).port ).port
).toBe('1234') ).toBe(1234)
}) })
test('Testing - sanitizeRepoPathSsh from HTTPS', () => { test('Testing - sanitizeRepoPathSsh from HTTPS', () => {
@@ -48,6 +48,22 @@ test('Testing - sanitizeRepoPathSsh from HTTPS', () => {
).toBe('ssh://git@github.com:22/username/repository.git') ).toBe('ssh://git@github.com:22/username/repository.git')
}) })
test('Testing - sanitizeRepoPathSsh - alt domain', () => {
expect(
GitHelper.sanitizeRepoPathSsh(
' https://gitea@git.alt-domain.com:2221/username/repository/ '
).repoPath
).toBe('ssh://gitea@git.alt-domain.com:2221/username/repository.git')
})
test('Testing - sanitizeRepoPathSsh - no owner', () => {
expect(
GitHelper.sanitizeRepoPathSsh(
' foo@git.alt-domain.com:repository.git '
).repoPath
).toBe('ssh://foo@git.alt-domain.com:22/repository.git')
})
test('Testing - getDomainFromSanitizedSshRepoPath - pure', () => { test('Testing - getDomainFromSanitizedSshRepoPath - pure', () => {
expect( expect(
GitHelper.getDomainFromSanitizedSshRepoPath( GitHelper.getDomainFromSanitizedSshRepoPath(
@@ -75,3 +91,11 @@ test('Testing - getDomainFromSanitizedSshRepoPath from HTTPS', () => {
) )
).toBe('github.com') ).toBe('github.com')
}) })
test('Testing - getDomainFromSanitizedSshRepoPath - alt domain', () => {
expect(
GitHelper.getDomainFromSanitizedSshRepoPath(
' ssh://user@some.do-main.com/owner/repository.git/ '
)
).toBe('some.do-main.com')
})