Skip to content

Commit

Permalink
Loosen --format repo URL restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeTechGuy committed Mar 21, 2024
1 parent 84ce6ea commit 5c758e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/lib/getRepoUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function getRepoUrl(
// It may already be a valid Repo URL
const url = new URL(gitURL)
// Some packages put a full URL in this field although it's not spec compliant. Let's detect that and use it if present
if (['github.com', 'gitlab.com', 'bitbucket.org'].includes(url.hostname) && url.protocol === 'https:') {
if (url.protocol === 'https:' || url.protocol === 'http:') {
return gitURL
}
} catch (e) {}
Expand All @@ -84,6 +84,7 @@ async function getRepoUrl(
// Remove the default branch path (/tree/HEAD) from a git url
return hostedGitURL.replace(/\/$/, '').replace(/\/tree\/HEAD$/, '')
}
return gitURL
}
return null
}
Expand Down
16 changes: 8 additions & 8 deletions test/getRepoUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ describe('getRepoUrl', () => {
it('return null repository field is unknown type', async () => {
should.equal(await getRepoUrl('package-name', { repository: true as any /* allow to compile */ }), null)
})
it('return url directly from repository field if valid github url', async () => {
it('return url directly from repository field if valid https url', async () => {
const url = await getRepoUrl('package-name', { repository: 'https://github.com/user/repo' })
url!.should.equal('https://github.com/user/repo')
})
it('return url directly from repository field if valid gitlab url', async () => {
const url = await getRepoUrl('package-name', { repository: 'https://gitlab.com/user/repo' })
url!.should.equal('https://gitlab.com/user/repo')
})
it('return url directly from repository field if valid bitbucket url', async () => {
const url = await getRepoUrl('package-name', { repository: 'https://bitbucket.org/user/repo' })
url!.should.equal('https://bitbucket.org/user/repo')
it('return url directly from repository field if valid http url', async () => {
const url = await getRepoUrl('package-name', { repository: 'http://anything.com/user/repo' })
url!.should.equal('http://anything.com/user/repo')
})
it('return url constructed from github shortcut syntax string', async () => {
const url = await getRepoUrl('package-name', { repository: 'user/repo' })
Expand All @@ -33,6 +29,10 @@ describe('getRepoUrl', () => {
const url = await getRepoUrl('package-name', { repository: 'github:user/repo' })
url!.should.equal('https://github.com/user/repo')
})
it('return url directly from url field if not a known git host', async () => {
const url = await getRepoUrl('package-name', { repository: { url: 'https://any.website.com/some/path' } })
url!.should.equal('https://any.website.com/some/path')
})
it('return url constructed from git-https protocol', async () => {
const url = await getRepoUrl('package-name', { repository: { url: 'git+https://github.com/user/repo.git' } })
url!.should.equal('https://github.com/user/repo')
Expand Down

0 comments on commit 5c758e6

Please sign in to comment.