-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from itiut/add-support-for-gitlab-com
Add support for GitLab.com
- Loading branch information
Showing
5 changed files
with
338 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict'; | ||
|
||
const workspace = require('vscode').workspace | ||
const querystring = require('querystring'); | ||
const gitUrlParse = require('git-url-parse'); | ||
|
||
class BaseProvider { | ||
constructor(gitUrl) { | ||
this.gitUrl = gitUrl; | ||
} | ||
|
||
get baseUrl() { | ||
return this.gitUrl.toString('https').replace(/\.git/, ''); | ||
} | ||
|
||
/** | ||
* Get the Web URL. | ||
* | ||
* @param {string} branch | ||
* @param {string} filePath The file path relative to repository root, beginning with '/'. | ||
* @param {number} line | ||
* @return {string} The URL to be opened with the browser. | ||
*/ | ||
webUrl(branch, filePath, line) { | ||
return ''; | ||
} | ||
} | ||
|
||
class GitHub extends BaseProvider { | ||
webUrl(branch, filePath, line) { | ||
if (filePath) { | ||
return `${this.baseUrl}/blob/${branch}${filePath}` + (line ? '#L' + line : ''); | ||
} | ||
return `${this.baseUrl}/tree/${branch}`; | ||
} | ||
} | ||
|
||
class Bitbucket extends BaseProvider { | ||
webUrl(branch, filePath, line) { | ||
return `${this.baseUrl}/src/${branch}` + (filePath ? `${filePath}` : '') + (line ? `#cl-${line}` : ''); | ||
} | ||
} | ||
|
||
class GitLab extends GitHub { | ||
} | ||
|
||
class VisualStudio extends BaseProvider { | ||
get baseUrl() { | ||
return `https://${this.gitUrl.resource}${this.gitUrl.pathname}`.replace(/\.git/, ''); | ||
} | ||
|
||
webUrl(branch, filePath, line) { | ||
let query = { | ||
version: `GB${branch}`, | ||
}; | ||
if (filePath) { | ||
query['path'] = filePath; | ||
} | ||
if (line) { | ||
query['line'] = line; | ||
} | ||
return `${this.baseUrl}?${querystring.stringify(query)}`; | ||
} | ||
} | ||
|
||
const gitHubDomain = workspace.getConfiguration('openInGitHub').get('gitHubDomain', 'github.com'); | ||
|
||
const providers = { | ||
[gitHubDomain]: GitHub, | ||
'bitbucket.org': Bitbucket, | ||
'gitlab.com': GitLab, | ||
'visualstudio.com': VisualStudio, | ||
}; | ||
|
||
/** | ||
* Get the Git provider of the remote URL. | ||
* | ||
* @param {string} remoteUrl | ||
* @return {BaseProvider|null} | ||
*/ | ||
function gitProvider(remoteUrl) { | ||
const gitUrl = gitUrlParse(remoteUrl); | ||
for (const domain of Object.keys(providers)) { | ||
if (domain === gitUrl.resource || domain === gitUrl.source) { | ||
return new providers[domain](gitUrl); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
module.exports = gitProvider; |
Oops, something went wrong.