fix: apply CodeRabbit auto-fixes

This commit is contained in:
Chris Scott committed 2026-10-08 16:47:24 -04:00
1 parent 88b3543dc1
commit 4119fec60a
5 files changed
+181 -38

No files matched your search

+7 -4
View File
@@ -940,14 +940,17 @@ export class ChangeWalkthroughService {
const target = repo
? { id: repo.id, repoUrl: repo.repoUrl, fullPath: repo.fullPath }
: { fullPath: directory }
const ref = await fetchPullRequestRef(
const refs = await fetchPullRequestRef(
directory,
session.id,
source,
this.gitAuthService.getGitEnvironment(true),
(remote, refspec, timeoutMs) =>
this.gitService.fetchRemoteRef(target, remote, refspec, this.db, timeoutMs),
(remote, refspecs, timeoutMs) =>
this.gitService.fetchRemoteRef(target, remote, refspecs, this.db, timeoutMs),
)
this.recordPullRequestRef(session.id, directory, ref)
for (const ref of refs) {
this.recordPullRequestRef(session.id, directory, ref)
}
}
private async runGenerate(
+2 -2
View File
@@ -741,14 +741,14 @@ export class GitService {
async fetchRemoteRef(
repo: { id?: number; repoUrl?: string; fullPath: string },
remote: string,
refspec: string,
refspecs: string[],
database: Database,
timeoutMs?: number,
): Promise<void> {
await this.withRemoteAuth(
repo,
async (env) => {
await executeCommand(['git', '-C', repo.fullPath, 'fetch', '--no-tags', remote, refspec], {
await executeCommand(['git', '-C', repo.fullPath, 'fetch', '--no-tags', remote, ...refspecs], {
env,
silent: true,
timeout: timeoutMs,
+59 -17
View File
@@ -29,45 +29,87 @@ const PULL_REQUEST_REMOTE_PREFERENCE = ['upstream', 'origin']
type PullRequestSource = Extract<GitWalkthroughSource, { kind: 'pullRequest' }>
export interface PullRequestRefs {
remote: string
baseBranch: string
headRef: string
baseRef: string
}
export interface PullRequestFetchPlan {
remote: string
refspec: string
ref: string
refspecs: string[]
headRef: string
baseRef: string
}
export type PullRequestFetchRunner = (
remote: string,
refspec: string,
refspecs: string[],
timeoutMs: number,
) => Promise<void>
export function walkthroughPullRequestRef(number: number): string {
return `refs/ocm-walkthrough/pr/${number}`
export async function resolvePullRequestRefs(
directory: string,
sessionId: string,
source: PullRequestSource,
gitEnv?: Record<string, string>,
): Promise<PullRequestRefs> {
const remotes = (await tryGit(directory, ['remote'], gitEnv))?.split('\n').map((line) => line.trim()) ?? []
const remote = PULL_REQUEST_REMOTE_PREFERENCE.find((candidate) => remotes.includes(candidate)) ?? 'origin'
const baseBranch = source.base ?? await resolveRemoteDefaultBranch(directory, remote, gitEnv)
return {
remote,
baseBranch,
headRef: `refs/ocm-walkthrough/${sessionId}/pr/${source.number}/head`,
baseRef: `refs/ocm-walkthrough/${sessionId}/pr/${source.number}/base/${baseBranch}`,
}
}
export async function planPullRequestFetch(
directory: string,
sessionId: string,
source: PullRequestSource,
gitEnv?: Record<string, string>,
): Promise<PullRequestFetchPlan> {
const remotes = (await tryGit(directory, ['remote'], gitEnv))?.split('\n').map((line) => line.trim()) ?? []
const remote = PULL_REQUEST_REMOTE_PREFERENCE.find((candidate) => remotes.includes(candidate)) ?? 'origin'
const ref = walkthroughPullRequestRef(source.number)
return { remote, refspec: `+refs/pull/${source.number}/head:${ref}`, ref }
const { remote, baseBranch, headRef, baseRef } = await resolvePullRequestRefs(directory, sessionId, source, gitEnv)
return {
remote,
refspecs: [
`+refs/pull/${source.number}/head:${headRef}`,
`+refs/heads/${baseBranch}:${baseRef}`,
],
headRef,
baseRef,
}
}
export async function fetchPullRequestRef(
directory: string,
sessionId: string,
source: PullRequestSource,
gitEnv: Record<string, string> | undefined,
runFetch: PullRequestFetchRunner,
): Promise<string> {
const plan = await planPullRequestFetch(directory, source, gitEnv)
): Promise<string[]> {
const plan = await planPullRequestFetch(directory, sessionId, source, gitEnv)
await mapGitError(
() => runFetch(plan.remote, plan.refspec, GIT_FETCH_TIMEOUT_MS),
() => runFetch(plan.remote, plan.refspecs, GIT_FETCH_TIMEOUT_MS),
'Failed to fetch the pull request',
)
return plan.ref
return [plan.headRef, plan.baseRef]
}
async function resolveRemoteDefaultBranch(
directory: string,
remote: string,
gitEnv?: Record<string, string>,
): Promise<string> {
const prefix = `${remote}/`
const ref = (await tryGit(directory, ['rev-parse', '--abbrev-ref', `${remote}/HEAD`], gitEnv))?.trim()
if (ref && ref.startsWith(prefix)) {
return ref.slice(prefix.length)
}
return resolveDefaultBranch(directory, gitEnv ?? {})
}
export async function deletePullRequestRef(
@@ -96,11 +138,12 @@ export async function readWalkthroughChanges({
if (source.kind === 'session') {
return readSessionChanges(client, session.id)
}
return readGitChanges(session.location.directory, source, gitEnv)
return readGitChanges(session.location.directory, session.id, source, gitEnv)
}
async function readGitChanges(
directory: string,
sessionId: string,
source: GitWalkthroughSource,
gitEnv?: Record<string, string>,
): Promise<FileDiffInfo[]> {
@@ -119,9 +162,8 @@ async function readGitChanges(
return collectChanges(repoRoot, [`${base}...HEAD`], false, gitEnv)
}
case 'pullRequest': {
const base = await resolveBase(repoRoot, source.base, gitEnv)
const ref = walkthroughPullRequestRef(source.number)
return collectChanges(repoRoot, [`${base}...${ref}`], false, gitEnv)
const { headRef, baseRef } = await resolvePullRequestRefs(repoRoot, sessionId, source, gitEnv)
return collectChanges(repoRoot, [`${baseRef}...${headRef}`], false, gitEnv)
}
}
}
@@ -255,8 +255,8 @@ function refExists(repo: string, ref: string): boolean {
}
}
function sessionAt(directory: string): SessionInfo {
return { id: SESSION_ID, title: 'Title', location: { directory } } as SessionInfo
function sessionAt(directory: string, id: string = SESSION_ID): SessionInfo {
return { id, title: 'Title', location: { directory } } as SessionInfo
}
function replyForPrompt(prompt: string): string {
@@ -1982,15 +1982,19 @@ describe('ChangeWalkthroughService', () => {
it('fetches once during generation, reads the ref on GET, and deletes it with the session', async () => {
const repo = createPullRequestRepo()
sessions[SESSION_ID] = { changes: threeHunks, info: sessionAt(repo) }
fetchRemoteRef.mockImplementation(async (target: { fullPath: string }, remote: string, refspec: string) => {
git(['fetch', remote, refspec], target.fullPath)
fetchRemoteRef.mockImplementation(async (target: { fullPath: string }, remote: string, refspecs: string[]) => {
git(['fetch', remote, ...refspecs], target.fullPath)
})
fake.setGenerateImpl(async (prompt) => replyForPrompt(prompt))
const headRef = `refs/ocm-walkthrough/${SESSION_ID}/pr/1/head`
const baseRef = `refs/ocm-walkthrough/${SESSION_ID}/pr/1/base/main`
await service.generate(SESSION_ID, { source: { kind: 'pullRequest', number: 1, base: 'main' } })
expect(fetchRemoteRef).toHaveBeenCalledTimes(1)
expect(refExists(repo, 'refs/ocm-walkthrough/pr/1')).toBe(true)
expect(refExists(repo, headRef)).toBe(true)
expect(refExists(repo, baseRef)).toBe(true)
fetchRemoteRef.mockClear()
const state = await service.getState(SESSION_ID, { kind: 'pullRequest', number: 1, base: 'main' })
@@ -2002,10 +2006,43 @@ describe('ChangeWalkthroughService', () => {
service.handleEvent(sessionEvent('session.deleted', SESSION_ID))
await vi.waitFor(() => {
expect(refExists(repo, 'refs/ocm-walkthrough/pr/1')).toBe(false)
expect(refExists(repo, headRef)).toBe(false)
expect(refExists(repo, baseRef)).toBe(false)
})
})
it('keeps another session refs when one session is deleted', async () => {
const repo = createPullRequestRepo()
const secondSessionId = 'ses_walkthrough_2'
sessions[SESSION_ID] = { changes: threeHunks, info: sessionAt(repo) }
sessions[secondSessionId] = { changes: threeHunks, info: sessionAt(repo, secondSessionId) }
fetchRemoteRef.mockImplementation(async (target: { fullPath: string }, remote: string, refspecs: string[]) => {
git(['fetch', remote, ...refspecs], target.fullPath)
})
fake.setGenerateImpl(async (prompt) => replyForPrompt(prompt))
await service.generate(SESSION_ID, { source: { kind: 'pullRequest', number: 1, base: 'main' } })
await service.generate(secondSessionId, { source: { kind: 'pullRequest', number: 1, base: 'main' } })
const firstHead = `refs/ocm-walkthrough/${SESSION_ID}/pr/1/head`
const firstBase = `refs/ocm-walkthrough/${SESSION_ID}/pr/1/base/main`
const secondHead = `refs/ocm-walkthrough/${secondSessionId}/pr/1/head`
const secondBase = `refs/ocm-walkthrough/${secondSessionId}/pr/1/base/main`
expect(firstHead).not.toBe(secondHead)
expect(refExists(repo, firstHead)).toBe(true)
expect(refExists(repo, secondHead)).toBe(true)
service.handleEvent(sessionEvent('session.deleted', SESSION_ID))
await vi.waitFor(() => {
expect(refExists(repo, firstHead)).toBe(false)
expect(refExists(repo, firstBase)).toBe(false)
})
expect(refExists(repo, secondHead)).toBe(true)
expect(refExists(repo, secondBase)).toBe(true)
})
it('prefers the upstream remote when generating', async () => {
const repo = createPullRequestRepo()
const originUrl = git(['remote', 'get-url', 'origin'], repo)
@@ -13,11 +13,11 @@ import { ChangeWalkthroughError } from '../../src/services/change-walkthrough-er
import {
planPullRequestFetch,
readWalkthroughChanges,
walkthroughPullRequestRef,
} from '../../src/services/walkthrough-sources'
import type { OpenCodeClient } from '../../src/services/opencode/client'
import { cloneOrigin, createCommittedRepo, createOrigin, git, uniqueName } from '../helpers/git-fixtures'
const TEST_SESSION_ID = 'ses_test'
const workspaceRoot = mkdtempSync(path.join(tmpdir(), 'walkthrough-sources-'))
process.env.GIT_CONFIG_NOSYSTEM = '1'
process.env.GIT_CONFIG_GLOBAL = '/dev/null'
@@ -33,7 +33,7 @@ function repoPath(prefix: string): string {
}
function sessionAt(directory: string): SessionInfo {
return { id: 'ses_test', title: 'Test', location: { directory } } as SessionInfo
return { id: TEST_SESSION_ID, title: 'Test', location: { directory } } as SessionInfo
}
function readAt(directory: string, source: WalkthroughSource): Promise<FileDiffInfo[]> {
@@ -139,14 +139,51 @@ describe('readWalkthroughChanges', () => {
git(['add', 'pr.txt'], clone)
git(['commit', '-m', 'pr'], clone)
git(['push', 'origin', 'HEAD:refs/pull/1/head'], clone)
git(['fetch', 'origin', `+refs/pull/1/head:${walkthroughPullRequestRef(1)}`], clone)
const changes = await readAt(clone, { kind: 'pullRequest', number: 1, base: 'main' })
const source = { kind: 'pullRequest', number: 1, base: 'main' } as const
const plan = await planPullRequestFetch(clone, TEST_SESSION_ID, source)
git(['fetch', 'origin', ...plan.refspecs], clone)
const changes = await readAt(clone, source)
expect(changes.map((entry) => entry.file)).toEqual(['pr.txt'])
expect(changes[0]?.status).toBe('added')
})
it('diffs a pull request against the base from the remote that supplies the head', async () => {
const upstream = repoPath('pr-fork-upstream')
const upstreamWork = repoPath('pr-fork-upstream-work')
createOrigin(upstream, upstreamWork)
writeFileSync(path.join(upstreamWork, 'upstream.txt'), 'upstream\n')
git(['add', 'upstream.txt'], upstreamWork)
git(['commit', '-m', 'upstream advance'], upstreamWork)
git(['push', 'origin', 'main'], upstreamWork)
const origin = repoPath('pr-fork-origin')
const originWork = repoPath('pr-fork-origin-work')
createOrigin(origin, originWork)
const clone = repoPath('pr-fork-clone')
cloneOrigin(origin, clone)
git(['remote', 'add', 'upstream', upstream], clone)
git(['fetch', 'upstream'], clone)
git(['checkout', '-b', 'feature', 'upstream/main'], clone)
writeFileSync(path.join(clone, 'pr.txt'), 'pr\n')
git(['add', 'pr.txt'], clone)
git(['commit', '-m', 'pr'], clone)
git(['push', 'upstream', 'HEAD:refs/pull/1/head'], clone)
const source = { kind: 'pullRequest', number: 1, base: 'main' } as const
const plan = await planPullRequestFetch(clone, TEST_SESSION_ID, source)
expect(plan.remote).toBe('upstream')
git(['fetch', 'upstream', ...plan.refspecs], clone)
const changes = await readAt(clone, source)
expect(changes.map((entry) => entry.file)).toEqual(['pr.txt'])
})
it('does not fetch the pull request ref when reading changes', async () => {
const origin = repoPath('pr-no-fetch-origin')
const work = repoPath('pr-no-fetch-work')
@@ -172,11 +209,15 @@ describe('readWalkthroughChanges', () => {
git(['remote', 'add', 'origin', 'https://example.com/base.git'], repo)
git(['remote', 'add', 'upstream', 'https://example.com/fork.git'], repo)
const plan = await planPullRequestFetch(repo, { kind: 'pullRequest', number: 7, base: 'main' })
const plan = await planPullRequestFetch(repo, TEST_SESSION_ID, { kind: 'pullRequest', number: 7, base: 'main' })
expect(plan.remote).toBe('upstream')
expect(plan.refspec).toBe('+refs/pull/7/head:refs/ocm-walkthrough/pr/7')
expect(plan.ref).toBe('refs/ocm-walkthrough/pr/7')
expect(plan.refspecs).toEqual([
`+refs/pull/7/head:refs/ocm-walkthrough/${TEST_SESSION_ID}/pr/7/head`,
`+refs/heads/main:refs/ocm-walkthrough/${TEST_SESSION_ID}/pr/7/base/main`,
])
expect(plan.headRef).toBe(`refs/ocm-walkthrough/${TEST_SESSION_ID}/pr/7/head`)
expect(plan.baseRef).toBe(`refs/ocm-walkthrough/${TEST_SESSION_ID}/pr/7/base/main`)
})
it('falls back to the origin remote when no upstream exists', async () => {
@@ -184,10 +225,30 @@ describe('readWalkthroughChanges', () => {
createCommittedRepo(repo)
git(['remote', 'add', 'origin', 'https://example.com/base.git'], repo)
const plan = await planPullRequestFetch(repo, { kind: 'pullRequest', number: 3, base: 'main' })
const plan = await planPullRequestFetch(repo, TEST_SESSION_ID, { kind: 'pullRequest', number: 3, base: 'main' })
expect(plan.remote).toBe('origin')
expect(plan.refspec).toBe('+refs/pull/3/head:refs/ocm-walkthrough/pr/3')
expect(plan.refspecs).toEqual([
`+refs/pull/3/head:refs/ocm-walkthrough/${TEST_SESSION_ID}/pr/3/head`,
`+refs/heads/main:refs/ocm-walkthrough/${TEST_SESSION_ID}/pr/3/base/main`,
])
})
it('resolves the default base from the selected remote HEAD when no base is given', async () => {
const repo = repoPath('pr-default-base')
createCommittedRepo(repo)
git(['remote', 'add', 'origin', 'https://example.com/base.git'], repo)
git(['remote', 'add', 'upstream', 'https://example.com/fork.git'], repo)
git(['update-ref', 'refs/remotes/upstream/trunk', 'HEAD'], repo)
git(['symbolic-ref', 'refs/remotes/upstream/HEAD', 'refs/remotes/upstream/trunk'], repo)
const plan = await planPullRequestFetch(repo, TEST_SESSION_ID, { kind: 'pullRequest', number: 9 })
expect(plan.remote).toBe('upstream')
expect(plan.refspecs).toEqual([
`+refs/pull/9/head:refs/ocm-walkthrough/${TEST_SESSION_ID}/pr/9/head`,
`+refs/heads/trunk:refs/ocm-walkthrough/${TEST_SESSION_ID}/pr/9/base/trunk`,
])
})
it('excludes untracked files from staged and branch sources', async () => {