-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·135 lines (111 loc) · 3.63 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node
'use strict';
const program = require('commander');
const currentBranchName = require('git-branch').sync();
const parse = require('parse-git-config');
const config = parse.sync();
const git = parse.keys(config);
const parseUrl = require('git-url-parse');
const opn = require('opn');
const defaults = {
remote: 'origin',
base: 'master',
destination: currentBranchName,
};
const getGitUrl = url => {
const { resource, name, owner } = parseUrl(url);
return `https://${resource}/${owner}/${name}`;
};
const getProviderName = url => parseUrl(url).source.split('.')[0];
const actions = {
github: url => ({
pull: name =>
`${getGitUrl(url)}/pull/${name}`,
createPull: (base, destination) =>
`${getGitUrl(url)}/compare/${base}...${destination}`,
issues: () =>
`${getGitUrl(url)}/issues`,
issue: issueNumber =>
`${getGitUrl(url)}/issues/${issueNumber}`,
createIssue: (title = '', description = '') =>
`${getGitUrl(url)}/issues/new?title=${title}&body=${description}`,
}),
gitlab: url => ({
pull: name =>
`${getGitUrl(url)}/merge_requests/${name}`,
createPull: (base, destination) =>
`${getGitUrl(url)}/merge_requests/new?merge_request[source_branch]=${base}&merge_request[target_branch]=${destination}`,
issues: () =>
`${getGitUrl(url)}/issues`,
issue: issueNumber =>
`${getGitUrl(url)}/issues/${issueNumber}`,
createIssue: (title = '', description = '') =>
`${getGitUrl(url)}/issues/new?issue[title]=${title}&issue[title]=${description}`,
}),
};
const openUrl = url => opn(encodeURI(url), { wait: false });
const getAvailableProviders = Object.keys(actions).join(', ');
const noProviderSupportedMessage = () => console.log(
'gitc does not support your git provider yet. Supported: %s',
getAvailableProviders,
);
// Pull Requests
program
.command('pull [number]')
.alias('p')
.option('-c, --create [branch]', 'The branch you want to open the pull request. The default is your current branch.')
.option(
'-b, --base [branch]',
'Your base branch, usally the branch that will receive the changes',
defaults.base,
)
.option(
'-r, --remote [remote]',
'Your remote configuration, usally "origin"',
defaults.remote,
)
.action((number, { remote, create, base }) => {
const url = git.remote[remote].url;
const provider = actions[getProviderName(url)];
if (!provider) {
return noProviderSupportedMessage();
}
const { createPull, pull } = provider(url);
if (create) {
const shouldUseDefault = typeof create === 'boolean';
const destination = shouldUseDefault ? defaults.destination : create;
return openUrl(createPull(base, destination));
}
return openUrl(pull(number || defaults.destination));
});
// Issues
program
.command('issue [number]')
.alias('i')
.option('-c, --create')
.option('-t, --title [title]', "Your issue's title.", '')
.option('-b, --body [description]', "Your issue's description.", '')
.option(
'-r, --remote [remote]',
'Your remote configuration, usally "origin"',
defaults.remote,
)
.action((number, { remote, create, title, body }) => {
const url = git.remote[remote].url;
const provider = actions[getProviderName(url)];
if (!provider) {
return noProviderSupportedMessage();
}
const { createIssue, issues, issue } = provider(url);
if (create) {
return openUrl(createIssue(title, body));
}
if (!number) {
return openUrl(issues());
}
return openUrl(issue(number));
});
if (!process.argv.slice(2).length) {
program.outputHelp();
}
program.parse(process.argv);