Kasra Bigdeli
2020-03-16 21:42:14 -04:00
parent 5239a06828
commit e5dd3340a2
2 changed files with 58 additions and 13 deletions

View File

@@ -34,7 +34,10 @@ export default class GitHelper {
uuid.v4()
)
const REPO_GIT_PATH = GitHelper.sanitizeRepoPathSsh(repo)
const sanitized = GitHelper.sanitizeRepoPathSsh(repo)
const REPO_GIT_PATH = sanitized.repoPath
const SSH_PORT = sanitized.port
const DOMAIN = GitHelper.getDomainFromSanitizedSshRepoPath(
REPO_GIT_PATH
)
@@ -53,7 +56,7 @@ export default class GitHelper {
})
.then(function() {
return exec(
`ssh-keyscan -H ${DOMAIN} >> /root/.ssh/known_hosts`
`ssh-keyscan -p ${SSH_PORT} -H ${DOMAIN} >> /root/.ssh/known_hosts`
)
})
.then(function() {
@@ -85,9 +88,10 @@ export default class GitHelper {
}
}
// input is like this: git@github.com:caprover/caprover-cli.git
// input is like this: ssh://git@github.com:22/caprover/caprover-cli.git
static getDomainFromSanitizedSshRepoPath(input: string) {
return input.substring(4, input.indexOf(':'))
input = input.substring(10)
return input.substring(0, input.indexOf(':'))
}
// It returns a string like this "github.com/username/repository.git"
@@ -103,7 +107,7 @@ export default class GitHelper {
return input.replace(/\/$/, '')
}
// It returns a string like this "git@github.com:caprover/caprover-cli.git"
// It returns a string like this "ssh://git@github.com:22/caprover/caprover-cli.git"
static sanitizeRepoPathSsh(input: string) {
input = Utils.removeHttpHttps(input)
if (!input.startsWith('git@')) {
@@ -112,6 +116,31 @@ export default class GitHelper {
input = 'git@' + input
}
return input.replace(/\/$/, '')
// 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('Marformatted SSH path: ' + input)
}
if (!input.toLowerCase().startsWith('ssh://')) {
input = 'ssh://' + input
}
return {
repoPath: input.replace(/\/$/, ''),
port: port,
}
}
}

View File

@@ -20,22 +20,38 @@ test('Testing - sanitizeRepoPathSsh', () => {
expect(
GitHelper.sanitizeRepoPathSsh(
' git@github.com:username/repository.git/ '
)
).toBe('git@github.com:username/repository.git')
).repoPath
).toBe('ssh://git@github.com:22/username/repository.git')
})
test('Testing - sanitizeRepoPathSsh - port', () => {
expect(
GitHelper.sanitizeRepoPathSsh(
' git@github.com:username/repository.git/ '
).port
).toBe('22')
})
test('Testing - sanitizeRepoPathSsh - custom port', () => {
expect(
GitHelper.sanitizeRepoPathSsh(
' git@github.com:1234/username/repository.git/ '
).port
).toBe('1234')
})
test('Testing - sanitizeRepoPathSsh from HTTPS', () => {
expect(
GitHelper.sanitizeRepoPathSsh(
' https://github.com/username/repository.git/ '
)
).toBe('git@github.com:username/repository.git')
).repoPath
).toBe('ssh://git@github.com:22/username/repository.git')
})
test('Testing - getDomainFromSanitizedSshRepoPath - pure', () => {
expect(
GitHelper.getDomainFromSanitizedSshRepoPath(
'git@github.com:username/repository.git'
'ssh://git@github.com:132/username/repository.git'
)
).toBe('github.com')
})
@@ -45,7 +61,7 @@ test('Testing - getDomainFromSanitizedSshRepoPath', () => {
GitHelper.getDomainFromSanitizedSshRepoPath(
GitHelper.sanitizeRepoPathSsh(
' git@github.com:username/repository.git/ '
)
).repoPath
)
).toBe('github.com')
})
@@ -55,7 +71,7 @@ test('Testing - getDomainFromSanitizedSshRepoPath from HTTPS', () => {
GitHelper.getDomainFromSanitizedSshRepoPath(
GitHelper.sanitizeRepoPathSsh(
' https://github.com/username/repository.git/ '
)
).repoPath
)
).toBe('github.com')
})