Customize Source Control AI prompts (#2916)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -1,19 +1,33 @@
|
||||
/* eslint-disable max-lines -- Why: create-PR tests share gh/SSH mocks across
|
||||
template, CLI, and error-path cases; keeping them together prevents drift. */
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { readFile } from 'fs/promises'
|
||||
|
||||
const { ghExecFileAsyncMock, getOwnerRepoMock, extractExecErrorMock, acquireMock, releaseMock } =
|
||||
vi.hoisted(() => ({
|
||||
ghExecFileAsyncMock: vi.fn(),
|
||||
getOwnerRepoMock: vi.fn(),
|
||||
extractExecErrorMock: vi.fn((error: unknown) => {
|
||||
const value = error as { stderr?: string; stdout?: string; message?: string }
|
||||
return {
|
||||
stderr: value?.stderr ?? value?.message ?? '',
|
||||
stdout: value?.stdout ?? ''
|
||||
}
|
||||
}),
|
||||
acquireMock: vi.fn(),
|
||||
releaseMock: vi.fn()
|
||||
}))
|
||||
const {
|
||||
ghExecFileAsyncMock,
|
||||
getOwnerRepoMock,
|
||||
extractExecErrorMock,
|
||||
acquireMock,
|
||||
releaseMock,
|
||||
getSshFilesystemProviderMock
|
||||
} = vi.hoisted(() => ({
|
||||
ghExecFileAsyncMock: vi.fn(),
|
||||
getOwnerRepoMock: vi.fn(),
|
||||
extractExecErrorMock: vi.fn((error: unknown) => {
|
||||
const value = error as { stderr?: string; stdout?: string; message?: string }
|
||||
return {
|
||||
stderr: value?.stderr ?? value?.message ?? '',
|
||||
stdout: value?.stdout ?? ''
|
||||
}
|
||||
}),
|
||||
acquireMock: vi.fn(),
|
||||
releaseMock: vi.fn(),
|
||||
getSshFilesystemProviderMock: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('../providers/ssh-filesystem-dispatch', () => ({
|
||||
getSshFilesystemProvider: getSshFilesystemProviderMock
|
||||
}))
|
||||
|
||||
vi.mock('./gh-utils', () => ({
|
||||
execFileAsync: vi.fn(),
|
||||
@@ -49,6 +63,7 @@ describe('createGitHubPullRequest', () => {
|
||||
extractExecErrorMock.mockClear()
|
||||
acquireMock.mockReset()
|
||||
releaseMock.mockReset()
|
||||
getSshFilesystemProviderMock.mockReset()
|
||||
acquireMock.mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
@@ -149,6 +164,76 @@ describe('createGitHubPullRequest', () => {
|
||||
expect(options).not.toHaveProperty('cwd')
|
||||
})
|
||||
|
||||
it.each([
|
||||
['.github/pull_request_template.md', ['.github/pull_request_template.md']],
|
||||
[
|
||||
'.github/PULL_REQUEST_TEMPLATE.md',
|
||||
['.github/pull_request_template.md', '.github/PULL_REQUEST_TEMPLATE.md']
|
||||
],
|
||||
[
|
||||
'docs/PULL_REQUEST_TEMPLATE.md',
|
||||
[
|
||||
'.github/pull_request_template.md',
|
||||
'.github/PULL_REQUEST_TEMPLATE.md',
|
||||
'pull_request_template.md',
|
||||
'PULL_REQUEST_TEMPLATE.md',
|
||||
'docs/pull_request_template.md',
|
||||
'docs/PULL_REQUEST_TEMPLATE.md'
|
||||
]
|
||||
]
|
||||
] as [string, string[]][])(
|
||||
'reads PR templates from the SSH filesystem provider at %s',
|
||||
async (relativeTemplatePath, expectedRelativeLookups) => {
|
||||
const templateBody = `Remote template body from ${relativeTemplatePath}`
|
||||
const readRemoteFile = vi.fn(async (path: string) => {
|
||||
if (path === `/remote/repo-root/${relativeTemplatePath}`) {
|
||||
return {
|
||||
content: templateBody,
|
||||
isBinary: false
|
||||
}
|
||||
}
|
||||
throw new Error('missing template')
|
||||
})
|
||||
getSshFilesystemProviderMock.mockReturnValue({ readFile: readRemoteFile })
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
const writtenBodies: string[] = []
|
||||
ghExecFileAsyncMock.mockImplementationOnce(async (args: string[]) => {
|
||||
const bodyPath = args[args.indexOf('--body-file') + 1]
|
||||
writtenBodies.push(await readFile(bodyPath, 'utf8'))
|
||||
return {
|
||||
stdout: JSON.stringify({
|
||||
number: 46,
|
||||
url: 'https://github.com/acme/widgets/pull/46'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
await expect(
|
||||
createGitHubPullRequest(
|
||||
'/remote/repo-root',
|
||||
{
|
||||
provider: 'github',
|
||||
base: 'main',
|
||||
head: 'feature/ssh-template',
|
||||
title: 'SSH Template PR',
|
||||
body: '',
|
||||
useTemplate: true
|
||||
},
|
||||
'ssh-1'
|
||||
)
|
||||
).resolves.toEqual({
|
||||
ok: true,
|
||||
number: 46,
|
||||
url: 'https://github.com/acme/widgets/pull/46'
|
||||
})
|
||||
|
||||
expect(readRemoteFile.mock.calls.map(([path]) => path)).toEqual(
|
||||
expectedRelativeLookups.map((relativeLookup) => `/remote/repo-root/${relativeLookup}`)
|
||||
)
|
||||
expect(writtenBodies).toEqual([templateBody])
|
||||
}
|
||||
)
|
||||
|
||||
it('falls back to parsing the PR URL for older gh output', async () => {
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
ghExecFileAsyncMock.mockResolvedValueOnce({
|
||||
|
||||
@@ -29,10 +29,12 @@ import {
|
||||
GITHUB_WORK_ITEMS_SSH_REMOTE_REQUIRED_MESSAGE,
|
||||
sortWorkItemsByUpdatedAt
|
||||
} from '../../shared/work-items'
|
||||
import { mkdtemp, rm, writeFile } from 'fs/promises'
|
||||
import { mkdtemp, readFile, rm, writeFile } from 'fs/promises'
|
||||
import { join } from 'path'
|
||||
import { tmpdir } from 'os'
|
||||
import { getPRConflictSummary } from './conflict-summary'
|
||||
import { getSshFilesystemProvider } from '../providers/ssh-filesystem-dispatch'
|
||||
import { joinWorktreeRelativePath } from '../runtime/runtime-relative-paths'
|
||||
import {
|
||||
execFileAsync,
|
||||
ghExecFileAsync,
|
||||
@@ -1391,6 +1393,41 @@ async function findOpenPRByHeadBase(args: {
|
||||
return { number: list[0].number, url: list[0].url }
|
||||
}
|
||||
|
||||
async function readPullRequestTemplate(
|
||||
repoPath: string,
|
||||
connectionId?: string | null
|
||||
): Promise<string> {
|
||||
const relativeCandidates = [
|
||||
'.github/pull_request_template.md',
|
||||
'.github/PULL_REQUEST_TEMPLATE.md',
|
||||
'pull_request_template.md',
|
||||
'PULL_REQUEST_TEMPLATE.md',
|
||||
'docs/pull_request_template.md',
|
||||
'docs/PULL_REQUEST_TEMPLATE.md'
|
||||
]
|
||||
const remoteProvider = connectionId ? getSshFilesystemProvider(connectionId) : undefined
|
||||
if (connectionId && !remoteProvider) {
|
||||
return ''
|
||||
}
|
||||
for (const relativeCandidate of relativeCandidates) {
|
||||
try {
|
||||
if (remoteProvider) {
|
||||
const result = await remoteProvider.readFile(
|
||||
joinWorktreeRelativePath(repoPath, relativeCandidate)
|
||||
)
|
||||
if (result.isBinary) {
|
||||
continue
|
||||
}
|
||||
return result.content
|
||||
}
|
||||
return await readFile(join(repoPath, relativeCandidate), 'utf8')
|
||||
} catch {
|
||||
// Try the next conventional PR template path.
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
export async function createGitHubPullRequest(
|
||||
repoPath: string,
|
||||
input: CreateHostedReviewInput,
|
||||
@@ -1435,7 +1472,11 @@ export async function createGitHubPullRequest(
|
||||
await acquire()
|
||||
const bodyPath = join(tempDir, 'body.md')
|
||||
try {
|
||||
await writeFile(bodyPath, input.body ?? '', 'utf8')
|
||||
const body =
|
||||
input.useTemplate && !input.body?.trim()
|
||||
? await readPullRequestTemplate(repoPath, connectionId)
|
||||
: (input.body ?? '')
|
||||
await writeFile(bodyPath, body, 'utf8')
|
||||
const createArgs = [
|
||||
'pr',
|
||||
'create',
|
||||
|
||||
Reference in New Issue
Block a user