gh-setup-git-identity 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +23 -6
- package/package.json +1 -1
- package/src/cli.js +15 -7
- package/src/index.js +76 -2
- package/test/index.test.js +8 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ A tool to setup git identity based on current GitHub user.
|
|
|
13
13
|
Instead of manually running:
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
printf "
|
|
16
|
+
printf "y" | gh auth login -h github.com -s repo,workflow,user,read:org,gist --git-protocol https --web
|
|
17
17
|
|
|
18
18
|
USERNAME=$(gh api user --jq '.login')
|
|
19
19
|
EMAIL=$(gh api user/emails --jq '.[] | select(.primary==true) | .email')
|
|
@@ -93,16 +93,23 @@ Options:
|
|
|
93
93
|
|
|
94
94
|
### First Run (Not Authenticated)
|
|
95
95
|
|
|
96
|
-
If you haven't authenticated with GitHub CLI yet, the tool will
|
|
96
|
+
If you haven't authenticated with GitHub CLI yet, the tool will automatically start the authentication process:
|
|
97
97
|
|
|
98
98
|
```
|
|
99
|
-
GitHub CLI is not authenticated.
|
|
99
|
+
GitHub CLI is not authenticated. Starting authentication...
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
Starting GitHub CLI authentication...
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
! First copy your one-time code: XXXX-XXXX
|
|
104
|
+
Press Enter to open github.com in your browser...
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The tool runs `gh auth login` automatically with the required scopes (`repo,workflow,user,read:org,gist`). Follow the browser-based authentication flow to complete login.
|
|
108
|
+
|
|
109
|
+
If automatic authentication fails, you can run the command manually:
|
|
104
110
|
|
|
105
|
-
|
|
111
|
+
```bash
|
|
112
|
+
printf "y" | gh auth login -h github.com -s repo,workflow,user,read:org,gist --git-protocol https --web
|
|
106
113
|
```
|
|
107
114
|
|
|
108
115
|
### Successful Run
|
|
@@ -159,6 +166,16 @@ Check if GitHub CLI is authenticated.
|
|
|
159
166
|
|
|
160
167
|
**Returns:** `Promise<boolean>`
|
|
161
168
|
|
|
169
|
+
#### `runGhAuthLogin(options?)`
|
|
170
|
+
|
|
171
|
+
Run `gh auth login` interactively with the required scopes.
|
|
172
|
+
|
|
173
|
+
**Parameters:**
|
|
174
|
+
- `options.verbose` - Enable verbose logging (default: `false`)
|
|
175
|
+
- `options.logger` - Custom logger (default: `console`)
|
|
176
|
+
|
|
177
|
+
**Returns:** `Promise<boolean>` - `true` if login was successful
|
|
178
|
+
|
|
162
179
|
#### `getGitHubUserInfo(options?)`
|
|
163
180
|
|
|
164
181
|
Get GitHub user information (username and primary email).
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { makeConfig } from 'lino-arguments';
|
|
10
|
-
import { setupGitIdentity, isGhAuthenticated } from './index.js';
|
|
10
|
+
import { setupGitIdentity, isGhAuthenticated, runGhAuthLogin } from './index.js';
|
|
11
11
|
|
|
12
12
|
// Parse command-line arguments with environment variable and .lenv support
|
|
13
13
|
const config = makeConfig({
|
|
@@ -67,14 +67,22 @@ async function main() {
|
|
|
67
67
|
|
|
68
68
|
if (!authenticated) {
|
|
69
69
|
console.log('');
|
|
70
|
-
console.log('GitHub CLI is not authenticated.');
|
|
70
|
+
console.log('GitHub CLI is not authenticated. Starting authentication...');
|
|
71
71
|
console.log('');
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
|
|
73
|
+
// Automatically run gh auth login
|
|
74
|
+
const loginSuccess = await runGhAuthLogin({ verbose: config.verbose });
|
|
75
|
+
|
|
76
|
+
if (!loginSuccess) {
|
|
77
|
+
console.log('');
|
|
78
|
+
console.log('Authentication failed. Please try running manually:');
|
|
79
|
+
console.log('');
|
|
80
|
+
console.log(' printf "y" | gh auth login -h github.com -s repo,workflow,user,read:org,gist --git-protocol https --web');
|
|
81
|
+
console.log('');
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
|
84
|
+
|
|
75
85
|
console.log('');
|
|
76
|
-
console.log('After logging in, run gh-setup-git-identity again.');
|
|
77
|
-
process.exit(1);
|
|
78
86
|
}
|
|
79
87
|
|
|
80
88
|
// Prepare options
|
package/src/index.js
CHANGED
|
@@ -41,11 +41,12 @@ function createDefaultLogger(options = {}) {
|
|
|
41
41
|
*
|
|
42
42
|
* @param {string} command - The command to execute
|
|
43
43
|
* @param {string[]} args - The command arguments
|
|
44
|
+
* @param {Object} options - Spawn options
|
|
44
45
|
* @returns {Promise<{stdout: string, stderr: string, exitCode: number}>}
|
|
45
46
|
*/
|
|
46
|
-
function execCommand(command, args = []) {
|
|
47
|
+
function execCommand(command, args = [], options = {}) {
|
|
47
48
|
return new Promise((resolve) => {
|
|
48
|
-
const child = spawn(command, args, { stdio: 'pipe', shell: false });
|
|
49
|
+
const child = spawn(command, args, { stdio: 'pipe', shell: false, ...options });
|
|
49
50
|
|
|
50
51
|
let stdout = '';
|
|
51
52
|
let stderr = '';
|
|
@@ -76,6 +77,78 @@ function execCommand(command, args = []) {
|
|
|
76
77
|
});
|
|
77
78
|
}
|
|
78
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Execute an interactive command (inheriting stdio)
|
|
82
|
+
*
|
|
83
|
+
* @param {string} command - The command to execute
|
|
84
|
+
* @param {string[]} args - The command arguments
|
|
85
|
+
* @param {Object} options - Options
|
|
86
|
+
* @param {string} options.input - Optional input to pipe to stdin
|
|
87
|
+
* @returns {Promise<{exitCode: number}>}
|
|
88
|
+
*/
|
|
89
|
+
function execInteractiveCommand(command, args = [], options = {}) {
|
|
90
|
+
return new Promise((resolve) => {
|
|
91
|
+
const { input } = options;
|
|
92
|
+
const child = spawn(command, args, {
|
|
93
|
+
stdio: input ? ['pipe', 'inherit', 'inherit'] : 'inherit',
|
|
94
|
+
shell: false
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
if (input && child.stdin) {
|
|
98
|
+
child.stdin.write(input);
|
|
99
|
+
child.stdin.end();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
child.on('close', (exitCode) => {
|
|
103
|
+
resolve({
|
|
104
|
+
exitCode: exitCode || 0
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
child.on('error', (error) => {
|
|
109
|
+
console.error(`Failed to execute command: ${error.message}`);
|
|
110
|
+
resolve({
|
|
111
|
+
exitCode: 1
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Run gh auth login interactively
|
|
119
|
+
*
|
|
120
|
+
* @param {Object} options - Options
|
|
121
|
+
* @param {boolean} options.verbose - Enable verbose logging
|
|
122
|
+
* @param {Object} options.logger - Custom logger
|
|
123
|
+
* @returns {Promise<boolean>} True if login was successful
|
|
124
|
+
*/
|
|
125
|
+
export async function runGhAuthLogin(options = {}) {
|
|
126
|
+
const { verbose = false, logger = console } = options;
|
|
127
|
+
const log = createDefaultLogger({ verbose, logger });
|
|
128
|
+
|
|
129
|
+
log(() => 'Starting GitHub CLI authentication...');
|
|
130
|
+
log(() => '');
|
|
131
|
+
|
|
132
|
+
// Run gh auth login with the required scopes
|
|
133
|
+
// Use 'y' as input to confirm default account selection
|
|
134
|
+
const result = await execInteractiveCommand('gh', [
|
|
135
|
+
'auth', 'login',
|
|
136
|
+
'-h', 'github.com',
|
|
137
|
+
'-s', 'repo,workflow,user,read:org,gist',
|
|
138
|
+
'--git-protocol', 'https',
|
|
139
|
+
'--web'
|
|
140
|
+
], { input: 'y\n' });
|
|
141
|
+
|
|
142
|
+
if (result.exitCode !== 0) {
|
|
143
|
+
log.error(() => 'GitHub CLI authentication failed');
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
log(() => '');
|
|
148
|
+
log(() => 'GitHub CLI authentication successful!');
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
|
|
79
152
|
/**
|
|
80
153
|
* Check if GitHub CLI is authenticated
|
|
81
154
|
*
|
|
@@ -302,6 +375,7 @@ export async function verifyGitIdentity(options = {}) {
|
|
|
302
375
|
|
|
303
376
|
export default {
|
|
304
377
|
isGhAuthenticated,
|
|
378
|
+
runGhAuthLogin,
|
|
305
379
|
getGitHubUsername,
|
|
306
380
|
getGitHubEmail,
|
|
307
381
|
getGitHubUserInfo,
|
package/test/index.test.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { test, assert } from 'test-anywhere';
|
|
6
6
|
import {
|
|
7
7
|
isGhAuthenticated,
|
|
8
|
+
runGhAuthLogin,
|
|
8
9
|
getGitHubUsername,
|
|
9
10
|
getGitHubEmail,
|
|
10
11
|
getGitHubUserInfo,
|
|
@@ -83,11 +84,17 @@ test('getGitHubUserInfo - returns user info when authenticated', async () => {
|
|
|
83
84
|
assert.ok(info.email.includes('@'));
|
|
84
85
|
});
|
|
85
86
|
|
|
87
|
+
// Test: runGhAuthLogin function exists and is a function
|
|
88
|
+
test('runGhAuthLogin - is exported as a function', async () => {
|
|
89
|
+
assert.equal(typeof runGhAuthLogin, 'function');
|
|
90
|
+
});
|
|
91
|
+
|
|
86
92
|
// Test: module exports
|
|
87
93
|
test('module exports all expected functions', async () => {
|
|
88
94
|
const module = await import('../src/index.js');
|
|
89
95
|
|
|
90
96
|
assert.ok(typeof module.isGhAuthenticated === 'function');
|
|
97
|
+
assert.ok(typeof module.runGhAuthLogin === 'function');
|
|
91
98
|
assert.ok(typeof module.getGitHubUsername === 'function');
|
|
92
99
|
assert.ok(typeof module.getGitHubEmail === 'function');
|
|
93
100
|
assert.ok(typeof module.getGitHubUserInfo === 'function');
|
|
@@ -104,6 +111,7 @@ test('default export contains all functions', async () => {
|
|
|
104
111
|
|
|
105
112
|
assert.ok(typeof defaultExport === 'object');
|
|
106
113
|
assert.ok(typeof defaultExport.isGhAuthenticated === 'function');
|
|
114
|
+
assert.ok(typeof defaultExport.runGhAuthLogin === 'function');
|
|
107
115
|
assert.ok(typeof defaultExport.getGitHubUsername === 'function');
|
|
108
116
|
assert.ok(typeof defaultExport.getGitHubEmail === 'function');
|
|
109
117
|
assert.ok(typeof defaultExport.getGitHubUserInfo === 'function');
|