-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.ts
More file actions
110 lines (105 loc) · 3.21 KB
/
token.ts
File metadata and controls
110 lines (105 loc) · 3.21 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
/**
* @file GitHub token resolution. Three sources, in priority order: environment
* variables, then `git config github.token`. The combined fallback
* (`getGitHubTokenWithFallback`) is what most callers want; the individual
* helpers exist so callers can constrain the search (e.g., env-only contexts
* where shelling out to git would be wrong).
*/
import { getGhToken, getGithubToken } from '../env/github'
import { getSocketCliGithubToken } from '../env/socket-cli'
import { spawn } from '../process/spawn/child'
import type { SpawnOptions } from '../process/spawn/types'
/**
* Get GitHub authentication token from environment variables. Checks multiple
* environment variable names in priority order.
*
* Environment variables checked (in order):
*
* 1. `GITHUB_TOKEN` - Standard GitHub token variable
* 2. `GH_TOKEN` - Alternative GitHub CLI token variable
* 3. `SOCKET_CLI_GITHUB_TOKEN` - Socket-specific token variable
*
* @example
* ;```ts
* const token = getGitHubToken()
* if (!token) {
* console.warn('No GitHub token found')
* }
* ```
*
* @returns The first available GitHub token, or `undefined` if none found
*/
export function getGitHubToken(): string | undefined {
return (
getGithubToken() || getGhToken() || getSocketCliGithubToken() || undefined
)
}
/**
* Get GitHub authentication token from git config. Reads the `github.token`
* configuration value from git config. This is a fallback method when
* environment variables don't contain a token.
*
* @example
* ;```ts
* const token = await getGitHubTokenFromGitConfig()
* if (token) {
* console.log('Found token in git config')
* }
* ```
*
* @example
* ;```ts
* // With custom working directory
* const token = await getGitHubTokenFromGitConfig({
* cwd: '/path/to/repo',
* })
* ```
*
* @param options - Spawn options for git command execution.
*
* @returns GitHub token from git config, or `undefined` if not configured
*/
export async function getGitHubTokenFromGitConfig(
options?: SpawnOptions | undefined,
): Promise<string | undefined> {
/* c8 ignore start - External git process call */
try {
const result = await spawn('git', ['config', 'github.token'], {
...options,
stdio: 'pipe',
})
if (result.code === 0 && result.stdout) {
return result.stdout.toString().trim()
}
} catch {
// Ignore errors - git config may not have token.
}
return undefined
/* c8 ignore stop */
}
/**
* Get GitHub authentication token from all available sources. Checks
* environment variables first, then falls back to git config. This is the
* recommended way to get a GitHub token with maximum compatibility.
*
* Priority order:
*
* 1. Environment variables (GITHUB_TOKEN, GH_TOKEN, SOCKET_CLI_GITHUB_TOKEN)
* 2. Git config (github.token)
*
* @example
* ;```ts
* const token = await getGitHubTokenWithFallback()
* if (!token) {
* throw new ErrorCtor('GitHub token required')
* }
* ```
*
* @returns GitHub token from first available source, or `undefined` if none
* found.
*/
export async function getGitHubTokenWithFallback(): Promise<
string | undefined
> {
return getGitHubToken() || (await getGitHubTokenFromGitConfig())
}