{{ message }}
Repository navigation
Expand file tree
/
Copy pathgithub-repository-identity.ts
More file actions
257 lines (238 loc) · 8.81 KB
/
Copy pathgithub-repository-identity.ts
File metadata and controls
257 lines (238 loc) · 8.81 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
import { runCoalescedProbe, type CoalescedProbes } from '../git/coalesced-probe'
import { readRemoteUrl } from '../git/remote-url-probe'
import type { GhAccountBinding } from '../../shared/github/account-binding'
import type { GitHubOwnerRepo } from '../../shared/github/pull-request-types'
import { readLocalGitConfigSignature } from './local-git-config-signature'
import {
parseGitHubOwnerRepo,
parseGitHubRemoteIdentity,
type GitHubRemoteIdentity
} from './github-remote-identity-parsing'
import { classifyGitHubOwnerRepoFromRemoteUrl } from './github-ssh-host-alias-resolution'
import { isStableMissingGitRemoteError } from '../git/stable-missing-git-remote-error'
import type { GitAdmissionTier } from '../git/command-runner/git-exec-options'
import { requireReachableGitRoute } from '../providers/execution-host-provider-dispatch'
import { getConnectionExecutionHostId } from '../../shared/execution-host'
export type OwnerRepo = GitHubOwnerRepo
export type { GitHubRemoteIdentity }
export { parseGitHubOwnerRepo, parseGitHubRemoteIdentity }
export type GitHubRepoContext = {
repoPath: string
connectionId?: string | null
wslDistro?: string
admissionTier?: GitAdmissionTier
/** SSH keeps the binding even when cwd is omitted from gh options. */
ghAccount?: GhAccountBinding
}
export type LocalGitExecOptions = {
wslDistro?: string
admissionTier?: GitAdmissionTier
ghAccount?: GhAccountBinding
}
export type GitHubRemoteIdentityProbeOptions = {
requireVerifiedSshProbe?: boolean
}
export function githubRepoContext(
repoPath: string,
connectionId?: string | null,
localGitOptions: LocalGitExecOptions = {}
): GitHubRepoContext {
return {
repoPath,
connectionId: connectionId ?? null,
...(localGitOptions.wslDistro ? { wslDistro: localGitOptions.wslDistro } : {}),
...(localGitOptions.admissionTier ? { admissionTier: localGitOptions.admissionTier } : {}),
...(localGitOptions.ghAccount ? { ghAccount: localGitOptions.ghAccount } : {})
}
}
export function ghRepoExecOptions(context: GitHubRepoContext): {
cwd?: string
encoding?: BufferEncoding
wslDistro?: string
admissionTier?: GitAdmissionTier
ghAccount?: GhAccountBinding
} {
const account = context.ghAccount ? { ghAccount: context.ghAccount } : {}
return context.connectionId
? { ...account }
: {
cwd: context.repoPath,
...(context.wslDistro ? { wslDistro: context.wslDistro } : {}),
...(context.admissionTier ? { admissionTier: context.admissionTier } : {}),
...account
}
}
const OWNER_REPO_POSITIVE_CACHE_TTL_MS = 30_000
const OWNER_REPO_NEGATIVE_CACHE_TTL_MS = 5 * 60_000
// Signed entries revalidate against Git config before reuse.
const OWNER_REPO_SIGNED_CACHE_TTL_MS = 5 * 60_000
const OWNER_REPO_CACHE_MAX_ENTRIES = 512
type OwnerRepoCacheEntry = {
value: OwnerRepo | null
expiresAt: number
configSignature?: string
}
const ownerRepoCache = new Map<string, OwnerRepoCacheEntry>()
const ownerRepoInFlight: CoalescedProbes<OwnerRepo | null> = new Map()
/** @internal - exposed for tests only */
export function _resetOwnerRepoCache(): void {
ownerRepoCache.clear()
ownerRepoInFlight.clear()
}
/** @internal - exposed for tests only */
export function _getOwnerRepoCacheSize(): number {
return ownerRepoCache.size
}
function pruneOwnerRepoCache(now: number): void {
for (const [key, entry] of ownerRepoCache) {
if (entry.expiresAt <= now) {
ownerRepoCache.delete(key)
}
}
while (ownerRepoCache.size > OWNER_REPO_CACHE_MAX_ENTRIES) {
const oldestKey = ownerRepoCache.keys().next().value
if (oldestKey === undefined) {
return
}
ownerRepoCache.delete(oldestKey)
}
}
export async function getRemoteUrlForRepo(
context: GitHubRepoContext,
remoteName: string
): Promise<string | null> {
return readRemoteUrl(context, remoteName)
}
function getOwnerRepoCacheTtl(value: OwnerRepo | null, configSignature?: string): number {
if (configSignature) {
return value ? OWNER_REPO_SIGNED_CACHE_TTL_MS : OWNER_REPO_NEGATIVE_CACHE_TTL_MS
}
return OWNER_REPO_POSITIVE_CACHE_TTL_MS
}
export async function getOwnerRepoForRemote(
repoPath: string,
remoteName: string,
connectionId?: string | null,
localGitOptions: LocalGitExecOptions = {},
probeOptions: GitHubRemoteIdentityProbeOptions = {}
): Promise<OwnerRepo | null> {
const context = githubRepoContext(repoPath, connectionId, localGitOptions)
if (probeOptions.requireVerifiedSshProbe) {
requireReachableGitRoute(getConnectionExecutionHostId(context.connectionId))
}
const runtimeKey = context.connectionId
? `ssh:${context.connectionId}`
: `local:${context.wslDistro ?? 'host'}`
const cacheKey = `${runtimeKey}\0${context.repoPath}\0${remoteName}`
const now = Date.now()
pruneOwnerRepoCache(now)
const cached = ownerRepoCache.get(cacheKey)
if (cached && cached.expiresAt > now) {
// Revalidate signed hits so remote changes are immediately visible.
if (cached.configSignature !== undefined) {
const currentSignature = await readLocalGitConfigSignature(context)
if (currentSignature !== cached.configSignature) {
ownerRepoCache.delete(cacheKey)
} else {
return cached.value
}
} else {
return cached.value
}
}
if (cached && cached.expiresAt <= now) {
ownerRepoCache.delete(cacheKey)
}
const nextConfigSignature = await readLocalGitConfigSignature(context)
const refreshedNow = Date.now()
const refreshedCached = ownerRepoCache.get(cacheKey)
if (refreshedCached && refreshedCached.expiresAt > refreshedNow) {
return refreshedCached.value
}
// Why: startup can resolve issue sources, PR candidates, and repo metadata
// for the same repo concurrently. Coalesce missing-remote probes — but only
// onto one young enough to still answer, so a wedged probe cannot pin the
// repo's identity for the life of the process (P1-D).
const inFlightKey = `${cacheKey}\0${probeOptions.requireVerifiedSshProbe ? 'verified' : 'tolerant'}`
return runCoalescedProbe(ownerRepoInFlight, inFlightKey, () =>
resolveOwnerRepoForRemote(
context,
remoteName,
cacheKey,
nextConfigSignature,
probeOptions.requireVerifiedSshProbe === true
)
)
}
async function resolveOwnerRepoForRemote(
context: GitHubRepoContext,
remoteName: string,
cacheKey: string,
configSignature: string | undefined,
requireVerifiedSshProbe: boolean
): Promise<OwnerRepo | null> {
const now = Date.now()
try {
const remoteUrl = await getRemoteUrlForRepo(context, remoteName)
if (!remoteUrl) {
if (requireVerifiedSshProbe) {
requireReachableGitRoute(getConnectionExecutionHostId(context.connectionId))
}
// Empty remote URL is stable until git config changes.
ownerRepoCache.set(cacheKey, {
value: null,
expiresAt: now + getOwnerRepoCacheTtl(null, configSignature),
...(configSignature ? { configSignature } : {})
})
pruneOwnerRepoCache(now)
return null
}
// Why: PR mutations need the effective host behind an SSH alias.
const classification = await classifyGitHubOwnerRepoFromRemoteUrl(remoteUrl, context)
if (classification.kind === 'github') {
// Signed identities stay valid until Git config changes.
ownerRepoCache.set(cacheKey, {
value: classification.ownerRepo,
expiresAt: now + getOwnerRepoCacheTtl(classification.ownerRepo, configSignature),
...(configSignature ? { configSignature } : {})
})
pruneOwnerRepoCache(now)
return classification.ownerRepo
}
if (classification.kind === 'indeterminate') {
// Why: a failed ssh -G probe is not a stable "not GitHub" result.
if (requireVerifiedSshProbe && context.connectionId) {
throw new Error('Remote repository identity is unverifiable.')
}
return null
}
const stableConfigSignature = classification.cacheWithGitConfigSignature
? configSignature
: undefined
ownerRepoCache.set(cacheKey, {
value: null,
expiresAt: now + getOwnerRepoCacheTtl(null, stableConfigSignature),
...(stableConfigSignature ? { configSignature: stableConfigSignature } : {})
})
pruneOwnerRepoCache(now)
return null
} catch (error) {
// Why: only stable "no such remote" misses are safe to hold for minutes.
// Transient git lock/IO failures must retry on the next lookup.
if (!isStableMissingGitRemoteError(error)) {
if (requireVerifiedSshProbe && context.connectionId) {
throw error
}
return null
}
}
// Why: a missing remote is stable until `.git/config` changes.
// Holding that negative longer avoids Git process churn across PR polling.
ownerRepoCache.set(cacheKey, {
value: null,
expiresAt: now + getOwnerRepoCacheTtl(null, configSignature),
...(configSignature ? { configSignature } : {})
})
pruneOwnerRepoCache(now)
return null
}
You can’t perform that action at this time.
