Skip to content

Commit b1d8e87

Browse files
johnpaularthurbugthesystem
authored andcommitted
Add Pull Request for BitBucket and GitLab (#52)
1 parent cfefa5a commit b1d8e87

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
},
2727
"activationEvents": [
2828
"onCommand:extension.openInGitHub",
29-
"onCommand:extension.copyGitHubLinkToClipboard"
29+
"onCommand:extension.copyGitHubLinkToClipboard",
30+
"onCommand:extension.openPrGitProvider"
3031
],
3132
"main": "./src/extension",
3233
"contributes": {
@@ -38,6 +39,10 @@
3839
{
3940
"command": "extension.copyGitHubLinkToClipboard",
4041
"title": "Copy GitHub link to clipboard"
42+
},
43+
{
44+
"command": "extension.openPrGitProvider",
45+
"title": "Open Pull Request"
4146
}
4247
],
4348
"configuration": {
@@ -63,6 +68,11 @@
6368
"command": "extension.copyGitHubLinkToClipboard",
6469
"key": "ctrl+l c",
6570
"mac": "ctrl+l c"
71+
},
72+
{
73+
"command": "extension.openPrGitProvider",
74+
"key": "ctrl+l p",
75+
"mac": "ctrl+l p"
6676
}
6777
],
6878
"menus": {
@@ -72,6 +82,9 @@
7282
},
7383
{
7484
"command": "extension.copyGitHubLinkToClipboard"
85+
},
86+
{
87+
"command": "extension.openPrGitProvider"
7588
}
7689
]
7790
}

src/extension.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var findParentDir = require('find-parent-dir');
1717

1818
const gitProvider = require('./gitProvider');
1919

20-
function getGitProviderLink(cb, fileFsPath, line) {
20+
function getGitProviderLink(cb, fileFsPath, line, pr) {
2121
var cwd = workspace.rootPath;
2222
var repoDir = findParentDir.sync(workspace.rootPath, '.git') || cwd;
2323

@@ -44,11 +44,19 @@ function getGitProviderLink(cb, fileFsPath, line) {
4444
subdir = repoRelativePath + subdir;
4545
}
4646

47-
cb(provider.webUrl(branch, subdir, line));
47+
if (pr){
48+
cb(provider.prUrl(branch));
49+
}
50+
else {
51+
cb(provider.webUrl(branch, subdir, line));
52+
}
53+
4854
});
4955
});
5056
}
5157

58+
59+
5260
function getGitProviderLinkForFile(fileFsPath, cb) {
5361
getGitProviderLink(cb, fileFsPath);
5462
}
@@ -66,8 +74,16 @@ function getGitProviderLinkForRepo(cb) {
6674
getGitProviderLink(cb);
6775
}
6876

69-
function branchOnCallingContext(args, cb) {
70-
if (args && args.fsPath) {
77+
function getGitProviderPullRequest(cb) {
78+
getGitProviderLink(cb, undefined, undefined, true);
79+
}
80+
81+
82+
function branchOnCallingContext(args, cb, pr) {
83+
if (pr) {
84+
getGitProviderPullRequest(cb);
85+
}
86+
else if (args && args.fsPath) {
7187
getGitProviderLinkForFile(args.fsPath, cb);
7288
}
7389
else if (Window.activeTextEditor) {
@@ -78,6 +94,7 @@ function branchOnCallingContext(args, cb) {
7894
}
7995
}
8096

97+
8198
function openInGitProvider(args) {
8299
branchOnCallingContext(args, open);
83100
}
@@ -86,10 +103,15 @@ function copyGitProviderLinkToClipboard(args) {
86103
branchOnCallingContext(args, copy);
87104
}
88105

106+
function openPrGitProvider(args) {
107+
branchOnCallingContext(args, open, true);
108+
}
109+
89110
//TODO: rename openInGitHub to openInGitProvider
90111
function activate(context) {
91112
context.subscriptions.push(commands.registerCommand('extension.openInGitHub', openInGitProvider));
92113
context.subscriptions.push(commands.registerCommand('extension.copyGitHubLinkToClipboard', copyGitProviderLinkToClipboard));
114+
context.subscriptions.push(commands.registerCommand('extension.openPrGitProvider', openPrGitProvider));
93115
}
94116

95117
exports.activate = activate;

src/gitProvider.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class BaseProvider {
2424
webUrl(branch, filePath, line) {
2525
return '';
2626
}
27+
prUrl(branch) {
28+
return '';
29+
}
2730
}
2831

2932
class GitHub extends BaseProvider {
@@ -33,12 +36,18 @@ class GitHub extends BaseProvider {
3336
}
3437
return `${this.baseUrl}/tree/${branch}`;
3538
}
39+
prUrl(branch){
40+
return `${this.baseUrl}/pull/new/${branch}`;
41+
}
3642
}
3743

3844
class Bitbucket extends BaseProvider {
3945
webUrl(branch, filePath, line) {
4046
return `${this.baseUrl}/src/${branch}` + (filePath ? `${filePath}` : '') + (line ? `#cl-${line}` : '');
4147
}
48+
prUrl(branch){
49+
return `${this.baseUrl}/pull-requests/new?source=${branch}`;
50+
}
4251
}
4352

4453
class GitLab extends GitHub {

0 commit comments

Comments
 (0)