Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
fix(repo): enable enterprise & custom hosts for git clone
Browse files Browse the repository at this point in the history
fix #651
  • Loading branch information
Ryan Garant committed Oct 2, 2019
1 parent f311729 commit cd7b8bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,8 @@ gh re --delete foo
| `-P`, `--protocol` | _Optional_ | `String` |
| `-u`, `--user` | _Optional_ | `String` |

> If you have custom ssh config, you can add `"api": { "git_host": "custom-name", ... }` to your .gh.json file.
#### Examples

- Clone a repository.
Expand Down
32 changes: 18 additions & 14 deletions src/cmds/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,14 @@ export async function run(options, done) {
var { data } = await getRepo(options)
} catch (err) {
throw new Error(
`Can't clone ${logger.colors.green(`${user}/${options.repo}`)}. ${
JSON.parse(err).message
}\n${err}`
`Can't clone ${logger.colors.green(`${user}/${options.repo}`)}.\n${err}`
)
}

logger.log(data.html_url)

let repoUrl

if (options.protocol) {
if (options.protocol === 'https') {
repoUrl = `https://github.com/${user}/${options.repo}.git`
}
} else {
repoUrl = `git@github.com:${user}/${options.repo}.git`
}

if (data) {
clone_(user, options.repo, repoUrl)
clone_(user, options.repo, getCloneUrl(options, config.api.git_host))
}

await afterHooks('repo.get', { options })
Expand Down Expand Up @@ -401,6 +389,22 @@ function deleteLabel(options, user): Promise<Octokit.Response<Octokit.IssuesDele
return options.GitHub.issues.deleteLabel(payload)
}

/**
* If user has a custom git_host defined we will use that (helpful for custom ssh rules).
* Otherwise pluck it from the previously set up github_host.
*/
function getCloneUrl({ repo, user, protocol, github_host }, gitHost?: string): string {
const hostWithoutProtocol = gitHost || github_host.split('://')[1]
let repoUrl = `git@${hostWithoutProtocol}:${user}/${repo}.git`

if (protocol === 'https') {
repoUrl = `https://${hostWithoutProtocol}/${user}/${repo}.git`
}
console.log('repoUrl', repoUrl)

return repoUrl
}

function getRepo(options): Promise<Octokit.Response<Octokit.IssuesGetResponse>> {
const payload = {
repo: options.repo,
Expand Down

0 comments on commit cd7b8bd

Please sign in to comment.