Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (104 loc) · 3.47 KB
/
Copy pathindex.js
File metadata and controls
117 lines (104 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env node
'use strict';
const util = require('util');
const url = require('url');
const path = require('path');
const opn = require("opn");
let gitP = require('simple-git/promise');
let branch = null;
let fetchUrl = null;
let teamUrl = null;
function showUsage() {
console.log('------------------------');
console.log('Command Line Interface for creating and viewing Pull Requests in VSTS.');
console.log('USAGE', 'git-vs', 'ui|newpr|pulls|work|queries');
console.log('Install as git alias with: git config --global alias.vs "!git-vs" ');
console.log('------------------------');
process.exit(1);
}
function createNewPr(fetchUrl)
{
gitP().status().then(status => {
branch = status.current;
if (branch == "master") {
console.log("Cannot create a pull request when you are on master");
process.exit(1);
}
console.log("Pushing " + branch + " to remote...");
return gitP().push('origin', status.current, {'--set-upstream': null });
})
.then(() => {
let uriNewPR = fetchUrl + "/pullrequestcreate?sourceRef=" + branch;
console.log("Opening Pull Request UI.");
openWrapper(uriNewPR);
});
}
function openWrapper(uri) {
console.log("Opening browser...");
console.log(uri);
opn(uri);
}
function getVsUrls() {
return gitP().getRemotes(true)
.then(remotes => {
let origin = remotes.filter(r => r.name === 'origin')[0];
if (!origin) {
return Promise.reject('Cannot find remote origin');
}
fetchUrl = origin.refs.fetch;
if (fetchUrl.indexOf("visualstudio") > 0) {
teamUrl = fetchUrl.replace(/_git\/.*/, "");
return Promise.resolve(
{
"teamUrl": teamUrl,
"fetchUrl": fetchUrl
});
} else if (fetchUrl.indexOf("dev.azure.com") > 0) {
fetchUrl = fetchUrl.replace(/.*@/, "https://")
teamUrl = fetchUrl.replace(/_git\/.*/, "");
return Promise.resolve(
{
"teamUrl": teamUrl,
"fetchUrl": fetchUrl
});
}
return Promise.reject("Origin is not a visual studio domain or azure dev ops domain... FetchUrl = " + fetchUrl);
});
}
// *******************************************************************
// Main
// *******************************************************************
if (process.argv.length < 3) {
showUsage();
}
let verb = process.argv[2].toLowerCase();
getVsUrls().then(urls => {
switch (verb)
{
case 'pr':
case 'new':
case 'newpr':
createNewPr(urls.fetchUrl);
break;
case 'ui':
openWrapper(urls.fetchUrl + "/pullrequests?_a=mine");
break;
case 'work':
openWrapper(urls.teamUrl + "/_workitems/assignedtome");
break;
case 'queries':
openWrapper(urls.teamUrl + '_queries/favorites/');
break;
case 'pulls':
let u = url.parse(urls.fetchUrl);
openWrapper(u.protocol + "//" + u.hostname + "/_pulls");
break;
default:
showUsage();
}
})
.catch(error => {
console.error('ERROR', error)
process.exit(1);
});
/**/