Skip to content

Commit 41ef196

Browse files
linarnanbugthesystem
authored andcommitted
Try Support gitlab (#58)
1 parent 1223b50 commit 41ef196

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
"preLaunchTask": "npm"
3232
}
3333
]
34-
}
34+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Add following line into workspace settings;
7272
{
7373
"openInGitHub.gitHubDomain":"your custom github domain here",
7474
"openInGitHub.requireSelectionForLines":false // If enabled, the copied or opened URL won't include line number(s) unless there's an active selection
75+
"openInGitHub.providerType": "gitlab" //github, gitlab, bitbucket, ...
7576
}
7677
```
7778

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060
"type": "boolean",
6161
"default": false,
6262
"description": "If enabled, the copied or opened URL won't include line number(s) unless there's an active selection"
63+
},
64+
"openInGitHub.providerType": {
65+
"type": "string",
66+
"default": "gitlab",
67+
"enum": [
68+
"gitlab",
69+
"github",
70+
"bitbucket"
71+
],
72+
"description": "specify the provider type in Custom Site"
6373
}
6474
}
6575
},

src/extension.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ function getGitProviderLink(cb, fileFsPath, lines, pr) {
2626
cwd: repoDir
2727
}, function (err, config) {
2828
const rawUri = config['remote \"origin\"'].url;
29-
const provider = gitProvider(rawUri);
29+
var provider = null;
3030

31-
if (!provider) {
32-
Window.showWarningMessage('Unknown Git provider.');
31+
try {
32+
provider = gitProvider(rawUri);
33+
} catch (e) {
34+
let errmsg = e.toString();
35+
Window.showWarningMessage(`Unknown Git provider. ${errmsg}`);
3336
return;
3437
}
3538

@@ -46,7 +49,12 @@ function getGitProviderLink(cb, fileFsPath, lines, pr) {
4649
}
4750

4851
if (pr){
49-
cb(provider.prUrl(branch));
52+
try {
53+
cb(provider.prUrl(branch));
54+
}catch (e){
55+
Window.showWarningMessage(e.toString());
56+
return;
57+
}
5058
}
5159
else {
5260
if (lines) {

src/gitProvider.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ class Bitbucket extends BaseProvider {
5252
}
5353

5454
class GitLab extends GitHub {
55+
webUrl(branch, filePath, line, endLine) {
56+
if (filePath) {
57+
return `${this.baseUrl}/blob/${branch}` + (filePath ? `${filePath}` : '') + (line ? `#L-${line}` : '');
58+
}
59+
return `${this.baseUrl}/tree/${branch}`;
60+
}
61+
prUrl(branch){
62+
//https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
63+
// doesn't support yet, require target_branch, title to supply further
64+
throw new Error(`doesn't support Merge Request from URL in gitlab provider yet`);
65+
//TODO
66+
//return `${this.baseUrl}/merge-requests/new?source_branch=${branch}&target_branch=${????}&title=${????}`;
67+
}
5568
}
5669

5770
class VisualStudio extends BaseProvider {
@@ -74,6 +87,7 @@ class VisualStudio extends BaseProvider {
7487
}
7588

7689
const gitHubDomain = workspace.getConfiguration('openInGitHub').get('gitHubDomain', 'github.com');
90+
const providerType = workspace.getConfiguration('openInGitHub').get('providerType', 'unknown');
7791

7892
const providers = {
7993
[gitHubDomain]: GitHub,
@@ -89,13 +103,16 @@ const providers = {
89103
* @return {BaseProvider|null}
90104
*/
91105
function gitProvider(remoteUrl) {
106+
console.log(providerType);
92107
const gitUrl = gitUrlParse(remoteUrl);
93108
for (const domain of Object.keys(providers)) {
94109
if (domain === gitUrl.resource || domain === gitUrl.source) {
95110
return new providers[domain](gitUrl);
111+
}else if( domain.indexOf(providerType) > -1 ){
112+
return new providers[domain](gitUrl);
96113
}
97114
}
98-
return null;
115+
throw new Error('unknown Provider');
99116
}
100117

101118
module.exports = gitProvider;

0 commit comments

Comments
 (0)