Skip to content

Commit

Permalink
feat: add bitbucket support (#12)
Browse files Browse the repository at this point in the history
* feat(platform): add support for bitbucket

fix #6

* docs(readme): update list of suppported platforms

* fix(bitbucket): add username@ handling for remote urls

* refactor: now all repos will have username@ trimmed by default
  • Loading branch information
keevan committed Feb 14, 2022
1 parent 83b1b46 commit 8704a37
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ Or search for the `git-link` package via `Settings > Packages`. Read more on [At

### Supported platforms

- Works for GitHub.com (public, tested).
- Works for GitLab.com (public, tested).
- It might work for others (untested).
- Works for GitHub.com (public, tested)
- Works for GitLab.com (public, tested)
- Works for BitBucket.org (public, tested)
- Works for Azure DevOps (dev.azure.com) (public, tested)
- It might work for others (untested, PRs welcome)

### Features

Expand Down
2 changes: 2 additions & 0 deletions lib/git-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ export default {
// TODO: Figure out urls with non-standard ports?
.replace(/(:)([^\/])/gm, '/$2')
.replace(/\.git$/, '')
// Remove the leading username from the repo link
.replace(/(\/\/)(.*@)/gm, '$1')

return repo
},
Expand Down
12 changes: 3 additions & 9 deletions lib/platforms/azure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
const azure = ({
repo,
}) => {
if (repo.indexOf('@') !== -1) {
const regex = /(\/\/)(.*@)/gm;
const subst = `$1`;
this.repo = repo.replace(regex, subst);
} else {
const regex = /ssh.dev.azure.com\/v3\/([^\/]+)\/([^\/]+)/gm;
const subst = `dev.azure.com/$1/$2/_git`;
this.repo = repo.replace(regex, subst);
}
const regex = /ssh.dev.azure.com\/v3\/([^\/]+)\/([^\/]+)/gm;
const subst = `dev.azure.com/$1/$2/_git`;
this.repo = repo.replace(regex, subst);

return {
getRepo: () => this.repo,
Expand Down
26 changes: 26 additions & 0 deletions lib/platforms/bitbucket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use babel';

const bitbucket = ({
repo,
}) => ({
hostsRepo: ({ host, response }) => {
// If it's the known public domain (BitBucket.org)
if (host.toLowerCase() === 'bitbucket.org') {
return true
}

// If the header has the x-view-name
if (response.headers['x-view-name'].indexOf('bitbucket') !== -1) {
return true
}

// Otherwise..
return false
},
getSelectionLink: ({ commitHash, relativePath, start, end }) => `${repo}/src/${commitHash}${relativePath}#lines-${start}:${end}`,
getLineLink: ({ commitHash, relativePath, start }) => `${repo}/src/${commitHash}${relativePath}#lines-${start}`,
getFileLink: ({ commitHash, relativePath }) => `${repo}/src/${commitHash}${relativePath}`,

})

export default bitbucket
1 change: 1 addition & 0 deletions lib/platforms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
export { default as github } from './github'
export { default as gitlab } from './gitlab'
export { default as azure } from './azure'
export { default as bitbucket } from './bitbucket'
export { default as default } from './default'
17 changes: 17 additions & 0 deletions spec/git-link-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ describe('GitLink', () => {
})
})

describe('Test platform support - bitbucket', () => {
it("has to detect bitbucket", () => {
const urls = [
'https://bitbucket.org/user/repo.git',
'git@bitbucket.org:user/repo.git',
'https://username@bitbucket.org/user/repo.git',
]
urls.forEach(async url => {
const repo = GitLink.getRepoFromOrigin(url)
const p = await platform.create({ repo })
expect('bitbucket').toEqual(p.type)
const resolvedRepo = p.getRepo()
expect('https://bitbucket.org/user/repo').toEqual(resolvedRepo)
})
})
})

describe('Parsing functions - getCommitHashFromLog', () => {
it("has to return the expected hash format", () => {
// Expectations
Expand Down

0 comments on commit 8704a37

Please sign in to comment.