-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathgithub.mts
More file actions
274 lines (254 loc) · 7.89 KB
/
Copy pathgithub.mts
File metadata and controls
274 lines (254 loc) · 7.89 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* GitHub utilities for Socket CLI. Provides GitHub API integration for
* repository operations and GHSA vulnerability data.
*
* Authentication:
*
* - GetGitHubToken: Retrieve GitHub token from env/git config
* - GetOctokit: Get authenticated Octokit instance
* - GetOctokitGraphql: Get authenticated GraphQL client
*
* Caching:
*
* - 5-minute TTL for API responses
* - Automatic cache invalidation
* - Persistent cache in node_modules/.cache
*
* GHSA Operations:
*
* - CacheFetch: Cache API responses with TTL
* - FetchGhsaDetails: Fetch GitHub Security Advisory details
* - GetGhsaUrl: Generate GHSA advisory URL
* - ReadCache/writeCache: Persistent cache operations
*
* Repository Operations:
*
* - GraphQL queries for complex operations
* - Integration with Octokit REST API
* - Support for GitHub Actions environment variables
*/
import {
graphql as OctokitGraphql,
GraphqlResponseError,
} from '@octokit/graphql'
import { Octokit } from '@octokit/rest'
import { isDebugNs } from '@socketsecurity/lib-stable/debug/namespace'
import { debugDirNs, debugNs } from '@socketsecurity/lib-stable/debug/output'
import { parseUrl } from '@socketsecurity/lib-stable/url/parse'
import { cacheFetch } from './github-cache.mts'
import { spawnGit } from './spawn-git.mts'
import { GITHUB_API_URL } from '../../env/github-api-url.mts'
import { GITHUB_SERVER_URL } from '../../env/github-server-url.mts'
import { SOCKET_CLI_GITHUB_TOKEN } from '../../env/socket-cli-github-token.mts'
import { formatErrorWithDetail } from '../error/errors.mts'
import type { components } from '@octokit/openapi-types'
export type { CacheEntry } from './github-cache.mts'
export { cacheFetch, readCache, writeCache } from './github-cache.mts'
export {
GITHUB_ERR_ABUSE_DETECTION,
GITHUB_ERR_AUTH_FAILED,
GITHUB_ERR_GRAPHQL_RATE_LIMIT,
GITHUB_ERR_RATE_LIMIT,
handleGitHubApiError,
handleGraphqlError,
isGraphqlRateLimitError,
isRetryableGitHubError,
withGitHubRetry,
} from './github-errors.mts'
export type Pr = components['schemas']['pull-request']
let octokit: Octokit | undefined
let octokitGraphql: typeof OctokitGraphql | undefined
export type PrAutoMergeState = {
enabled: boolean
details?: string[] | undefined
}
export async function enablePrAutoMerge({
node_id: prId,
}: Pr): Promise<PrAutoMergeState> {
const graphqlClient = getOctokitGraphql()
try {
const gqlResp = await graphqlClient<{
enablePullRequestAutoMerge?:
| {
pullRequest?: { number?: number | undefined } | undefined
}
| undefined
}>(
`
mutation EnableAutoMerge($pullRequestId: ID!) {
enablePullRequestAutoMerge(input: {
pullRequestId: $pullRequestId,
mergeMethod: SQUASH
}) {
pullRequest {
number
}
}
}`,
{ pullRequestId: prId },
)
const respPrNumber =
gqlResp?.enablePullRequestAutoMerge?.pullRequest?.number
/* c8 ignore start - GraphQL success path requires a successful enablePullRequestAutoMerge response; tests mock the call to fail */
if (respPrNumber) {
return { enabled: true }
}
/* c8 ignore stop */
} catch (e) {
/* c8 ignore start - GraphqlResponseError with structured .errors requires the GitHub GraphQL endpoint to respond with that exact shape; tests cover the generic catch path */
if (
e instanceof GraphqlResponseError &&
Array.isArray(e.errors) &&
e.errors.length
) {
const details = e.errors.map(({ message: m }) => m.trim())
return { enabled: false, details }
}
/* c8 ignore stop */
}
return { enabled: false }
}
export type GhsaDetails = {
ghsaId: string
cveId?: string | undefined
summary: string
severity: string
publishedAt: string
withdrawnAt?: string | undefined
references: Array<{
url: string
}>
vulnerabilities: {
nodes: Array<{
package: {
ecosystem: string
name: string
}
vulnerableVersionRange: string
}>
}
}
export async function fetchGhsaDetails(
ids: string[],
): Promise<Map<string, GhsaDetails>> {
const results = new Map<string, GhsaDetails>()
if (!ids.length) {
return results
}
const graphqlClient = getOctokitGraphql()
try {
// Use '::' delimiter to avoid collisions (GHSA IDs contain hyphens).
const gqlCacheKey = `${ids.join('::')}-graphql-snapshot`
const aliases = ids
.map(
(id, index) =>
`advisory${index}: securityAdvisory(ghsaId: "${id}") {
ghsaId
summary
severity
publishedAt
withdrawnAt
vulnerabilities(first: 10) {
nodes {
package {
ecosystem
name
}
vulnerableVersionRange
}
}
}`,
)
.join('\n')
const gqlResp = await cacheFetch(gqlCacheKey, () =>
graphqlClient<Record<string, GhsaDetails | undefined>>(`
query {
${aliases}
}
`),
)
/* c8 ignore start - GQL response loop; cacheFetch wraps the call so the inner factory only fires on cache miss, which tests don't trigger */
for (let i = 0, { length } = ids; i < length; i += 1) {
const id = ids[i]!
const advisoryKey = `advisory${i}`
const advisory = gqlResp?.[advisoryKey]
if (advisory?.ghsaId) {
results.set(id, advisory)
} else {
debugNs('notice', `miss: no advisory found for ${id}`)
}
}
/* c8 ignore stop */
} catch (e) {
debugNs('error', formatErrorWithDetail('Failed to fetch GHSA details', e))
debugDirNs('error', e)
}
return results
}
export function getOctokit(): Octokit {
if (octokit === undefined) {
if (!SOCKET_CLI_GITHUB_TOKEN) {
debugNs('notice', 'miss: SOCKET_CLI_GITHUB_TOKEN env var')
}
const octokitOptions = {
...(SOCKET_CLI_GITHUB_TOKEN ? { auth: SOCKET_CLI_GITHUB_TOKEN } : {}),
...(GITHUB_API_URL ? { baseUrl: GITHUB_API_URL } : {}),
}
debugDirNs('inspect', { octokitOptions })
octokit = new Octokit(octokitOptions)
}
return octokit
}
export function getOctokitGraphql(): typeof OctokitGraphql {
if (!octokitGraphql) {
if (!SOCKET_CLI_GITHUB_TOKEN) {
debugNs('notice', 'miss: SOCKET_CLI_GITHUB_TOKEN env var')
}
octokitGraphql = OctokitGraphql.defaults({
headers: {
authorization: `token ${SOCKET_CLI_GITHUB_TOKEN}`,
},
})
}
return octokitGraphql
}
export async function setGitRemoteGithubRepoUrl(
owner: string,
repo: string,
token: string,
cwd = process.cwd(),
): Promise<boolean> {
const urlObj = parseUrl(GITHUB_SERVER_URL || '')
const host = urlObj?.host
/* c8 ignore start - GITHUB_SERVER_URL defaults to a parseable URL; only fires when env var is malformed */
if (!host) {
debugNs('error', 'invalid: GITHUB_SERVER_URL env var')
debugDirNs('inspect', { GITHUB_SERVER_URL })
return false
}
/* c8 ignore stop */
const url = `https://x-access-token:${token}@${host}/${owner}/${repo}`
// Redact the access token from the debug line — the real URL is still
// passed to spawn, but the token must never reach logs.
const redactedUrl = `https://x-access-token:***@${host}/${owner}/${repo}`
const quotedCmd = `\`git remote set-url origin ${redactedUrl}\``
debugNs('stdio', `spawn: ${quotedCmd}`)
try {
// `spawnGit` is what keeps the token out of a repository-supplied `git`
// shim: it resolves git outside the checkout under audit before the
// credential ever reaches argv.
await spawnGit(['remote', 'set-url'], {
cwd,
operands: ['origin', url],
stdio: isDebugNs('stdio') ? 'inherit' : 'ignore',
})
return true
/* c8 ignore start - git command failure path; tests run in real cwd with valid git */
} catch (e) {
debugNs('error', `Git command failed: ${quotedCmd}`)
debugDirNs('inspect', { cmd: quotedCmd })
debugDirNs('error', e)
}
return false
/* c8 ignore stop */
}