Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -1,8 +1,3 @@
|
||||
import {
|
||||
readFetchResponseJsonWithinLimit,
|
||||
readFetchResponseTextWithinLimit
|
||||
} from '../lib/fetch-response-body'
|
||||
|
||||
export class HostedReviewApiRequestError extends Error {
|
||||
readonly status: number | null
|
||||
readonly timedOut: boolean
|
||||
@@ -17,7 +12,7 @@ export class HostedReviewApiRequestError extends Error {
|
||||
|
||||
async function readResponseText(response: Response): Promise<string> {
|
||||
try {
|
||||
return await readFetchResponseTextWithinLimit(response)
|
||||
return await response.text()
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
@@ -36,7 +31,7 @@ export async function requestHostedReviewJson<T>(
|
||||
status: response.status
|
||||
})
|
||||
}
|
||||
return await readFetchResponseJsonWithinLimit<T>(response)
|
||||
return (await response.json()) as T
|
||||
} catch (error) {
|
||||
if (error instanceof HostedReviewApiRequestError) {
|
||||
throw error
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { mkdir, mkdtemp, rm, truncate, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { MAX_HOSTED_REVIEW_TEMPLATE_BYTES, readHostedReviewTemplate } from './pull-request-template'
|
||||
|
||||
describe('readHostedReviewTemplate', () => {
|
||||
const roots: string[] = []
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })))
|
||||
})
|
||||
|
||||
async function makeRepo(): Promise<string> {
|
||||
const root = await mkdtemp(join(tmpdir(), 'orca-review-template-'))
|
||||
roots.push(root)
|
||||
await mkdir(join(root, '.github'), { recursive: true })
|
||||
return root
|
||||
}
|
||||
|
||||
it('preserves the first conventional template under the limit', async () => {
|
||||
const repoPath = await makeRepo()
|
||||
await writeFile(join(repoPath, '.github', 'pull_request_template.md'), 'normal template\n')
|
||||
await writeFile(join(repoPath, 'PULL_REQUEST_TEMPLATE.md'), 'fallback\n')
|
||||
|
||||
await expect(readHostedReviewTemplate(repoPath)).resolves.toBe('normal template\n')
|
||||
})
|
||||
|
||||
it('skips an oversized sparse template and reads the next candidate', async () => {
|
||||
const repoPath = await makeRepo()
|
||||
const oversizedPath = join(repoPath, '.github', 'pull_request_template.md')
|
||||
await writeFile(oversizedPath, 'x')
|
||||
await truncate(oversizedPath, MAX_HOSTED_REVIEW_TEMPLATE_BYTES + 1)
|
||||
await writeFile(join(repoPath, '.github', 'PULL_REQUEST_TEMPLATE.md'), 'bounded fallback\n')
|
||||
|
||||
await expect(readHostedReviewTemplate(repoPath)).resolves.toBe('bounded fallback\n')
|
||||
})
|
||||
|
||||
it('accepts a template exactly at the byte limit', async () => {
|
||||
const repoPath = await makeRepo()
|
||||
const body = 'a'.repeat(MAX_HOSTED_REVIEW_TEMPLATE_BYTES)
|
||||
await writeFile(join(repoPath, '.github', 'pull_request_template.md'), body)
|
||||
|
||||
await expect(readHostedReviewTemplate(repoPath)).resolves.toHaveLength(
|
||||
MAX_HOSTED_REVIEW_TEMPLATE_BYTES
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -1,10 +1,8 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import type { HostedReviewProvider } from '../../shared/hosted-review'
|
||||
import { getSshFilesystemProvider } from '../providers/ssh-filesystem-dispatch'
|
||||
import { joinWorktreeRelativePath } from '../runtime/runtime-relative-paths'
|
||||
import { readNodeFileWithinLimit } from '../../shared/node-bounded-file-reader'
|
||||
|
||||
export const MAX_HOSTED_REVIEW_TEMPLATE_BYTES = 1024 * 1024
|
||||
|
||||
const PULL_REQUEST_TEMPLATE_CANDIDATES = [
|
||||
'.github/pull_request_template.md',
|
||||
@@ -30,11 +28,6 @@ function getTemplateCandidates(provider?: HostedReviewProvider | null): string[]
|
||||
if (provider === 'gitlab') {
|
||||
return [...MERGE_REQUEST_TEMPLATE_CANDIDATES, ...PULL_REQUEST_TEMPLATE_CANDIDATES]
|
||||
}
|
||||
if (provider === 'github') {
|
||||
return PULL_REQUEST_TEMPLATE_CANDIDATES.filter(
|
||||
(candidate) => !candidate.startsWith('.azuredevops/') && !candidate.startsWith('.gitea/')
|
||||
)
|
||||
}
|
||||
return PULL_REQUEST_TEMPLATE_CANDIDATES
|
||||
}
|
||||
|
||||
@@ -45,54 +38,27 @@ export async function readHostedPullRequestTemplate(
|
||||
return readHostedReviewTemplate(repoPath, connectionId)
|
||||
}
|
||||
|
||||
export async function readGitLabMergeRequestTemplate(
|
||||
repoPath: string,
|
||||
connectionId?: string | null
|
||||
): Promise<string> {
|
||||
return readHostedReviewTemplateCandidates(
|
||||
repoPath,
|
||||
connectionId,
|
||||
MERGE_REQUEST_TEMPLATE_CANDIDATES
|
||||
)
|
||||
}
|
||||
|
||||
export async function readHostedReviewTemplate(
|
||||
repoPath: string,
|
||||
connectionId?: string | null,
|
||||
provider?: HostedReviewProvider | null
|
||||
): Promise<string> {
|
||||
return readHostedReviewTemplateCandidates(repoPath, connectionId, getTemplateCandidates(provider))
|
||||
}
|
||||
|
||||
async function readHostedReviewTemplateCandidates(
|
||||
repoPath: string,
|
||||
connectionId: string | null | undefined,
|
||||
candidates: readonly string[]
|
||||
): Promise<string> {
|
||||
const remoteProvider = connectionId ? getSshFilesystemProvider(connectionId) : undefined
|
||||
if (connectionId && !remoteProvider) {
|
||||
return ''
|
||||
}
|
||||
for (const relativeCandidate of candidates) {
|
||||
for (const relativeCandidate of getTemplateCandidates(provider)) {
|
||||
try {
|
||||
if (remoteProvider) {
|
||||
const result = await remoteProvider.readFile(
|
||||
joinWorktreeRelativePath(repoPath, relativeCandidate)
|
||||
)
|
||||
if (
|
||||
result.isBinary ||
|
||||
Buffer.byteLength(result.content, 'utf8') > MAX_HOSTED_REVIEW_TEMPLATE_BYTES
|
||||
) {
|
||||
if (result.isBinary) {
|
||||
continue
|
||||
}
|
||||
return result.content
|
||||
}
|
||||
return (
|
||||
await readNodeFileWithinLimit(
|
||||
join(repoPath, relativeCandidate),
|
||||
MAX_HOSTED_REVIEW_TEMPLATE_BYTES
|
||||
)
|
||||
).buffer.toString('utf8')
|
||||
return await readFile(join(repoPath, relativeCandidate), 'utf8')
|
||||
} catch {
|
||||
// Try the next conventional hosted-review template path.
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
buildRepositoryRefCacheKey,
|
||||
REPOSITORY_REF_CACHE_KEY_MAX_BYTES,
|
||||
REPOSITORY_REF_CACHE_MAX_ENTRIES,
|
||||
REPOSITORY_REF_CACHE_VALUE_MAX_BYTES,
|
||||
RepositoryRefCache
|
||||
} from './repository-ref-cache'
|
||||
|
||||
describe('repository ref cache bounds', () => {
|
||||
it('admits an exact-boundary UTF-8 key and rejects one byte over', () => {
|
||||
expect(
|
||||
buildRepositoryRefCacheKey(['a'.repeat(REPOSITORY_REF_CACHE_KEY_MAX_BYTES - 1), ''])
|
||||
).not.toBeNull()
|
||||
expect(
|
||||
buildRepositoryRefCacheKey(['a'.repeat(REPOSITORY_REF_CACHE_KEY_MAX_BYTES), ''])
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('measures multibyte keys by bytes', () => {
|
||||
expect(
|
||||
buildRepositoryRefCacheKey(['😀'.repeat(REPOSITORY_REF_CACHE_KEY_MAX_BYTES / 4)])
|
||||
).not.toBeNull()
|
||||
expect(
|
||||
buildRepositoryRefCacheKey(['😀'.repeat(REPOSITORY_REF_CACHE_KEY_MAX_BYTES / 4 + 1)])
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('retains exact-boundary values and skips oversized values', () => {
|
||||
const cache = new RepositoryRefCache<{ value: string }>()
|
||||
const exactKey = buildRepositoryRefCacheKey(['exact'])
|
||||
const oversizedKey = buildRepositoryRefCacheKey(['oversized'])
|
||||
const exact = 'a'.repeat(REPOSITORY_REF_CACHE_VALUE_MAX_BYTES)
|
||||
const oversized = 'a'.repeat(REPOSITORY_REF_CACHE_VALUE_MAX_BYTES + 1)
|
||||
|
||||
cache.remember(exactKey, { value: exact }, [exact])
|
||||
cache.remember(oversizedKey, { value: oversized }, [oversized])
|
||||
|
||||
expect(cache.get(exactKey)).toEqual({ found: true, value: { value: exact } })
|
||||
expect(cache.get(oversizedKey)).toEqual({ found: false })
|
||||
})
|
||||
|
||||
it('bounds entry count and refreshes hits before LRU eviction', () => {
|
||||
const cache = new RepositoryRefCache<{ value: number }>()
|
||||
for (let index = 0; index < REPOSITORY_REF_CACHE_MAX_ENTRIES; index += 1) {
|
||||
cache.remember(`key-${index}`, { value: index }, [])
|
||||
}
|
||||
expect(cache.get('key-0')).toEqual({ found: true, value: { value: 0 } })
|
||||
cache.remember('new-key', { value: -1 }, [])
|
||||
|
||||
expect(cache.size).toBe(REPOSITORY_REF_CACHE_MAX_ENTRIES)
|
||||
expect(cache.get('key-0').found).toBe(true)
|
||||
expect(cache.get('key-1')).toEqual({ found: false })
|
||||
})
|
||||
|
||||
it('never retains an inadmissible key', () => {
|
||||
const cache = new RepositoryRefCache<{ value: number }>()
|
||||
cache.remember(null, { value: 1 }, [])
|
||||
expect(cache.size).toBe(0)
|
||||
})
|
||||
})
|
||||
@@ -1,71 +0,0 @@
|
||||
import { measureUtf8ByteLength } from '../../shared/utf8-byte-limits'
|
||||
|
||||
export const REPOSITORY_REF_CACHE_MAX_ENTRIES = 512
|
||||
export const REPOSITORY_REF_CACHE_KEY_MAX_BYTES = 4 * 1024
|
||||
export const REPOSITORY_REF_CACHE_VALUE_MAX_BYTES = 16 * 1024
|
||||
|
||||
export type RepositoryRefCacheLookup<T> = { found: true; value: T | null } | { found: false }
|
||||
|
||||
export function buildRepositoryRefCacheKey(parts: readonly string[]): string | null {
|
||||
let remainingBytes = REPOSITORY_REF_CACHE_KEY_MAX_BYTES - Math.max(0, parts.length - 1)
|
||||
if (remainingBytes < 0) {
|
||||
return null
|
||||
}
|
||||
for (const part of parts) {
|
||||
const measured = measureUtf8ByteLength(part, { stopAfterBytes: remainingBytes })
|
||||
if (measured.exceededLimit) {
|
||||
return null
|
||||
}
|
||||
remainingBytes -= measured.byteLength
|
||||
}
|
||||
return parts.join('\0')
|
||||
}
|
||||
|
||||
export class RepositoryRefCache<T> {
|
||||
private readonly entries = new Map<string, T | null>()
|
||||
|
||||
clear(): void {
|
||||
this.entries.clear()
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this.entries.size
|
||||
}
|
||||
|
||||
get(cacheKey: string | null): RepositoryRefCacheLookup<T> {
|
||||
if (cacheKey === null || !this.entries.has(cacheKey)) {
|
||||
return { found: false }
|
||||
}
|
||||
const value = this.entries.get(cacheKey) ?? null
|
||||
this.entries.delete(cacheKey)
|
||||
this.entries.set(cacheKey, value)
|
||||
return { found: true, value }
|
||||
}
|
||||
|
||||
remember(cacheKey: string | null, value: T | null, retainedStrings: readonly string[]): void {
|
||||
if (cacheKey === null || !fitsValueBudget(retainedStrings)) {
|
||||
return
|
||||
}
|
||||
this.entries.delete(cacheKey)
|
||||
this.entries.set(cacheKey, value)
|
||||
while (this.entries.size > REPOSITORY_REF_CACHE_MAX_ENTRIES) {
|
||||
const oldestKey = this.entries.keys().next().value
|
||||
if (oldestKey === undefined) {
|
||||
return
|
||||
}
|
||||
this.entries.delete(oldestKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function fitsValueBudget(values: readonly string[]): boolean {
|
||||
let remainingBytes = REPOSITORY_REF_CACHE_VALUE_MAX_BYTES
|
||||
for (const value of values) {
|
||||
const measured = measureUtf8ByteLength(value, { stopAfterBytes: remainingBytes })
|
||||
if (measured.exceededLimit) {
|
||||
return false
|
||||
}
|
||||
remainingBytes -= measured.byteLength
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user