Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit 841e2a2

Browse files
author
Ryan Garant
committed
fix(alias): add alias cmd back
re #661
1 parent f4de999 commit 841e2a2

File tree

9 files changed

+212
-14
lines changed

9 files changed

+212
-14
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"type": "node",
1010
"request": "launch",
1111
"runtimeArgs": ["-r", "ts-node/register"],
12-
"args": ["${workspaceFolder}/src/debug.ts", "is", "test"],
12+
"args": ["${workspaceFolder}/src/debug.ts", "user", "--user=zeno"],
1313
// "args": ["${workspaceFolder}/src/debug.ts", "ji", "LWM-117", "--status"],
1414
"console": "integratedTerminal"
1515
},

README.md

+70-10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [Gists](#gists)
2626
- [User](#user)
2727
- [Milestone](#milestone)
28+
- [Alias](#alias)
2829
- [Config](#config)
2930
- [Plugins](#plugins)
3031
- [Team](#team)
@@ -112,6 +113,16 @@ gh pull-request
112113
| `-r`, `--repo` | _Optional_ | `String` |
113114
| `-u`, `--user` | _Optional_ | `String` |
114115

116+
```
117+
gh pr
118+
```
119+
120+
- Get information about a pull request.
121+
122+
```
123+
gh pr --info number
124+
```
125+
115126
### 2. List
116127

117128
| Option | Usage | Type |
@@ -139,16 +150,6 @@ gh pull-request
139150
- **Shortcut** for listing open pull requests for the current repository.
140151
- To turn off pretty printing of output in a table add `"pretty_print": false` to your `~/.gh-json` config
141152

142-
```
143-
gh pr
144-
```
145-
146-
- Get information about a pull request.
147-
148-
```
149-
gh pr --info number
150-
```
151-
152153
- List open pull requests for all branches from all your repositories.
153154

154155
```
@@ -1208,6 +1209,65 @@ gh ms --list --user node-gh --repo gh
12081209
gh ms --list --all --organization node-gh
12091210
```
12101211

1212+
## Alias
1213+
1214+
This cmd provides something similar to shell aliases. If there are aliases in your .gh.json file, we will attempt to resolve the user, PR forwarder or PR submitter to your alias.
1215+
1216+
```
1217+
gh alias
1218+
```
1219+
1220+
> **Alias:** `gh al`
1221+
1222+
### 1. List
1223+
1224+
| Option | Usage | Type |
1225+
| -------------- | ------------ | --------- |
1226+
| `-l`, `--list` | **Required** | `Boolean` |
1227+
1228+
#### Examples
1229+
1230+
- **Shortcut** for listing aliases.
1231+
1232+
```
1233+
gh alias
1234+
```
1235+
1236+
- List aliases.
1237+
1238+
```
1239+
gh alias --list
1240+
```
1241+
1242+
### 2. Add
1243+
1244+
| Option | Usage | Type |
1245+
| -------------- | ------------ | -------- |
1246+
| `-a`, `--add` | **Required** | `String` |
1247+
| `-u`, `--user` | **Required** | `String` |
1248+
1249+
#### Examples
1250+
1251+
- Create alias for username.
1252+
1253+
```
1254+
gh alias --add zeno --user zenorocha
1255+
```
1256+
1257+
### 3. Remove
1258+
1259+
| Option | Usage | Type |
1260+
| ---------------- | ------------ | -------- |
1261+
| `-r`, `--remove` | **Required** | `String` |
1262+
1263+
#### Examples
1264+
1265+
- Remove alias.
1266+
1267+
```
1268+
gh alias --remove zeno
1269+
```
1270+
12111271
## Config
12121272

12131273
There are some pretty useful configurations that you can set on [.gh.json](default.gh.json).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`E2E: Alias Module Test User alias is expanded \`gh user --user=zeno\` 1`] = `
4+
"You're logged in as zenorocha
5+
"
6+
`;

__tests__/alias.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
3+
* (see file: CONTRIBUTORS)
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
import { runCmd } from './testUtils'
8+
9+
describe('E2E: Alias Module Test', () => {
10+
it('User alias is expanded `gh user --user=zeno`', done => {
11+
expect(runCmd('gh user --user=zeno')).toMatchSnapshot()
12+
done()
13+
})
14+
})

default.gh.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,9 @@
109109

110110
"ssh": true,
111111

112-
"signature": ""
112+
"signature": "",
113+
114+
"alias": {
115+
"zeno": "zenorocha"
116+
}
113117
}

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/base.ts

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ export function clone(o) {
1919

2020
export function load() {}
2121

22+
/**
23+
* Checks if there are aliases in your .gh.json file.
24+
* If there are aliases in your .gh.json file, we will attempt to resolve the user, PR forwarder or PR submitter to your alias.
25+
*/
26+
export function expandAliases(options) {
27+
if (config.alias) {
28+
options.fwd = config.alias[options.fwd] || options.fwd
29+
options.submit = config.alias[options.submit] || options.submit
30+
options.user = config.alias[options.user] || options.user
31+
}
32+
}
33+
2234
export function find(filepath, opt_pattern) {
2335
return fs.readdirSync(filepath).filter(file => {
2436
return (opt_pattern || /.*/).test(file)

src/cmd.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as fs from 'fs'
1010
import * as nopt from 'nopt'
1111
import * as path from 'path'
1212
import * as updateNotifier from 'update-notifier'
13-
import { find, getUser } from './base'
13+
import { find, getUser, expandAliases } from './base'
1414
import * as configs from './configs'
1515
import * as git from './git'
1616
import * as logger from './logger'
@@ -144,6 +144,8 @@ export async function setUp() {
144144
}
145145
}
146146

147+
expandAliases(options)
148+
147149
options.repo = options.repo || git.getRepoFromRemoteURL(remoteUrl)
148150
options.currentBranch = testing ? 'master' : git.getCurrentBranch()
149151
options.github_host = config.github_host

src/cmds/alias.ts

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
3+
* (see file: CONTRIBUTORS)
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
// -- Requires -------------------------------------------------------------------------------------
8+
9+
import * as base from '../base'
10+
import * as configs from '../configs'
11+
import * as logger from '../logger'
12+
13+
const config = base.getConfig()
14+
15+
// -- Constructor ----------------------------------------------------------------------------------
16+
17+
export default function Alias(options) {
18+
this.options = options
19+
}
20+
21+
// -- Constants ------------------------------------------------------------------------------------
22+
23+
Alias.DETAILS = {
24+
alias: 'al',
25+
description: 'Create alias for a username.',
26+
commands: ['add', 'list', 'remove'],
27+
options: {
28+
add: String,
29+
list: Boolean,
30+
remove: String,
31+
user: String,
32+
},
33+
shorthands: {
34+
a: ['--add'],
35+
l: ['--list'],
36+
r: ['--remove'],
37+
u: ['--user'],
38+
},
39+
payload(payload, options) {
40+
if (payload[0]) {
41+
options.add = payload[0]
42+
options.user = payload[1]
43+
} else {
44+
options.list = true
45+
}
46+
},
47+
}
48+
49+
// -- Commands -------------------------------------------------------------------------------------
50+
51+
Alias.prototype.run = function() {
52+
const instance = this
53+
const options = instance.options
54+
55+
if (options.add) {
56+
if (!options.user) {
57+
logger.error('You must specify an user, try --user username.')
58+
}
59+
60+
logger.debug(`Creating alias ${options.add}`)
61+
instance.add()
62+
}
63+
64+
if (options.list) {
65+
instance.list((err, data) => {
66+
let item
67+
68+
for (item in data) {
69+
if (data.hasOwnProperty(item)) {
70+
logger.log(`${logger.colors.cyan(item)}: ${logger.colors.magenta(data[item])}`)
71+
}
72+
}
73+
})
74+
}
75+
76+
if (options.remove) {
77+
logger.debug(`Removing alias ${options.remove}`)
78+
instance.remove()
79+
}
80+
}
81+
82+
Alias.prototype.add = function() {
83+
const instance = this
84+
const options = instance.options
85+
86+
configs.writeGlobalConfig(`alias.${options.add}`, options.user)
87+
}
88+
89+
Alias.prototype.list = function(opt_callback) {
90+
opt_callback && opt_callback(null, config.alias)
91+
}
92+
93+
Alias.prototype.remove = function() {
94+
const instance = this
95+
const options = instance.options
96+
97+
delete config.alias[options.remove]
98+
99+
configs.writeGlobalConfig('alias', config.alias)
100+
}

0 commit comments

Comments
 (0)