Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Strip username from host #103

Merged
merged 1 commit into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/github-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,17 @@ export default class GitHubFile {
githubRepoURL () {
let url = this.gitURL()

if (url.match(/git@[^:]+:/)) {
if (url.match(/git@[^:]+:/)) { // git@github.com:user/repo.git
url = url.replace(/^git@([^:]+):(.+)$/, (match, host, repoPath) => {
repoPath = repoPath.replace(/^\/+/, '')
return `http://${host}/${repoPath}`
return `https://${host}/${repoPath}` // -> https://github.com/user/repo.git
})
} else if (url.match(/ssh:\/\/git@([^/]+)\//)) {
url = `http://${url.substring(10)}`
} else if (url.match(/^git:\/\/[^/]+\//)) {
url = `http${url.substring(3)}`
} else if (url.match(/^ssh:\/\/git@([^/]+)\//)) { // ssh://git@github.com/user/repo.git
url = `https://${url.substring(10)}` // -> https://github.com/user/repo.git
} else if (url.match(/^git:\/\/[^/]+\//)) { // git://github.com/user/repo.git
url = `https${url.substring(3)}` // -> https://github.com/user/repo.git
} else if (url.match(/^https?:\/\/\w+@/)) { // https://user@github.com/user/repo.git
url = url.replace(/^https?:\/\/\w+@/, 'https://') // -> https://github.com/user/repo.git
}

// Remove trailing .git and trailing slashes
Expand Down
15 changes: 10 additions & 5 deletions spec/github-file-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,25 +694,30 @@ describe('GitHubFile', function () {

it('returns the GitHub.com URL for an SSH remote URL', () => {
githubFile.gitURL = () => 'git@github.com:foo/bar.git'
expect(githubFile.githubRepoURL()).toBe('http://github.com/foo/bar')
expect(githubFile.githubRepoURL()).toBe('https://github.com/foo/bar')
})

it('returns a GitHub enterprise URL for a non-Github.com remote URL', () => {
githubFile.gitURL = () => 'https://git.enterprize.me/foo/bar.git'
expect(githubFile.githubRepoURL()).toBe('https://git.enterprize.me/foo/bar')

githubFile.gitURL = () => 'git@git.enterprize.me:foo/bar.git'
expect(githubFile.githubRepoURL()).toBe('http://git.enterprize.me/foo/bar')
expect(githubFile.githubRepoURL()).toBe('https://git.enterprize.me/foo/bar')
})

it('returns the GitHub.com URL for a git:// URL', () => {
githubFile.gitURL = () => 'git://github.com/foo/bar.git'
expect(githubFile.githubRepoURL()).toBe('http://github.com/foo/bar')
expect(githubFile.githubRepoURL()).toBe('https://github.com/foo/bar')
})

it('returns the GitHub.com URL for a user@github.com URL', () => {
githubFile.gitURL = () => 'https://user@github.com/foo/bar.git'
expect(githubFile.githubRepoURL()).toBe('https://github.com/foo/bar')
})

it('returns the GitHub.com URL for a ssh:// URL', () => {
githubFile.gitURL = () => 'ssh://git@github.com/foo/bar.git'
expect(githubFile.githubRepoURL()).toBe('http://github.com/foo/bar')
expect(githubFile.githubRepoURL()).toBe('https://github.com/foo/bar')
})

it('returns undefined for Bitbucket URLs', () => {
Expand All @@ -737,7 +742,7 @@ describe('GitHubFile', function () {
expect(githubFile.githubRepoURL()).toBe('https://github.com/foo/bar')

githubFile.gitURL = () => 'git@github.com:/foo/bar.git'
expect(githubFile.githubRepoURL()).toBe('http://github.com/foo/bar')
expect(githubFile.githubRepoURL()).toBe('https://github.com/foo/bar')
})
})

Expand Down