Add mobile Tasks parity (#2452)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo Hong
2026-05-21 20:26:07 -07:00
committed by GitHub
co-authored by Orca
parent 3efc669bd4
commit b1973657ea
123 changed files with 26492 additions and 1461 deletions
@@ -1,3 +1,4 @@
/* eslint-disable max-lines -- Why: hosted review creation permutations share large mocks; splitting would hide branch-specific expectations. */
import { beforeEach, describe, expect, it, vi } from 'vitest'
const {
@@ -10,7 +11,8 @@ const {
getHostedReviewForBranchMock,
ghExecFileAsyncMock,
gitExecFileAsyncMock,
getUpstreamStatusMock
getUpstreamStatusMock,
getSshGitProviderMock
} = vi.hoisted(() => ({
createGitHubPullRequestMock: vi.fn(),
getRepoSlugMock: vi.fn(),
@@ -21,7 +23,8 @@ const {
getHostedReviewForBranchMock: vi.fn(),
ghExecFileAsyncMock: vi.fn(),
gitExecFileAsyncMock: vi.fn(),
getUpstreamStatusMock: vi.fn()
getUpstreamStatusMock: vi.fn(),
getSshGitProviderMock: vi.fn()
}))
vi.mock('../github/client', () => ({
@@ -56,6 +59,10 @@ vi.mock('../git/upstream', () => ({
getUpstreamStatus: getUpstreamStatusMock
}))
vi.mock('../providers/ssh-git-dispatch', () => ({
getSshGitProvider: getSshGitProviderMock
}))
vi.mock('./hosted-review', () => ({
getHostedReviewForBranch: getHostedReviewForBranchMock
}))
@@ -73,7 +80,8 @@ function resetMocks(): void {
getHostedReviewForBranchMock,
ghExecFileAsyncMock,
gitExecFileAsyncMock,
getUpstreamStatusMock
getUpstreamStatusMock,
getSshGitProviderMock
]) {
mock.mockReset()
}
@@ -184,6 +192,71 @@ describe('createHostedReview', () => {
expect(createGitHubPullRequestMock).toHaveBeenCalledOnce()
})
it('uses the SSH git provider for remote hosted-review preflight', async () => {
const remoteGit = {
exec: vi.fn(async (args: string[]) => {
if (args[0] === 'rev-parse' && args[1] === '--abbrev-ref' && args[2] === 'HEAD') {
return { stdout: 'feature\n', stderr: '' }
}
if (args[0] === 'status') {
return { stdout: '', stderr: '' }
}
if (args[0] === 'rev-parse' && args[2] === 'HEAD@{u}') {
return { stdout: 'origin/feature\n', stderr: '' }
}
if (args[0] === 'rev-list') {
return { stdout: '0 0\n', stderr: '' }
}
if (args[0] === 'log' && args.includes('--pretty=%s')) {
return { stdout: 'Feature title\n', stderr: '' }
}
if (args[0] === 'log') {
return { stdout: '- Feature title\n', stderr: '' }
}
return { stdout: '', stderr: '' }
})
}
getSshGitProviderMock.mockReturnValue(remoteGit)
await expect(
createHostedReview(
'/remote/repo',
{
provider: 'github',
base: 'main',
head: 'feature',
title: 'Feature'
},
'ssh-1'
)
).resolves.toEqual({
ok: true,
number: 12,
url: 'https://github.com/acme/orca/pull/12'
})
expect(remoteGit.exec).toHaveBeenCalledWith(
['rev-parse', '--abbrev-ref', 'HEAD'],
'/remote/repo'
)
expect(remoteGit.exec).toHaveBeenCalledWith(['status', '--porcelain'], '/remote/repo')
expect(getUpstreamStatusMock).not.toHaveBeenCalled()
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
['auth', 'status', '--hostname', 'github.com'],
{}
)
expect(createGitHubPullRequestMock).toHaveBeenCalledWith(
'/remote/repo',
{
provider: 'github',
base: 'main',
head: 'feature',
title: 'Feature'
},
'ssh-1'
)
})
it('returns the existing review instead of creating a duplicate', async () => {
getHostedReviewForBranchMock.mockResolvedValue({
provider: 'github',
@@ -287,6 +360,46 @@ describe('getHostedReviewCreationEligibility', () => {
})
})
it('resolves remote eligibility through SSH repo metadata', async () => {
const remoteGit = {
exec: vi.fn(async (args: string[]) => {
if (args[0] === 'log' && args.includes('--pretty=%s')) {
return { stdout: 'Remote title\n', stderr: '' }
}
if (args[0] === 'log') {
return { stdout: '- Remote title\n', stderr: '' }
}
return { stdout: '', stderr: '' }
})
}
getSshGitProviderMock.mockReturnValue(remoteGit)
await expect(
getHostedReviewCreationEligibility({
repoPath: '/remote/repo',
connectionId: 'ssh-1',
branch: 'feature/create-pr',
base: 'origin/main',
hasUncommittedChanges: false,
hasUpstream: true,
ahead: 0,
behind: 0
})
).resolves.toMatchObject({
provider: 'github',
canCreate: true,
title: 'Remote title',
body: '- Remote title'
})
expect(getProjectSlugMock).toHaveBeenCalledWith('/remote/repo', 'ssh-1')
expect(getRepoSlugMock).toHaveBeenCalledWith('/remote/repo', 'ssh-1')
expect(getHostedReviewForBranchMock).toHaveBeenCalledWith(
expect.objectContaining({ repoPath: '/remote/repo', connectionId: 'ssh-1' })
)
expect(remoteGit.exec).toHaveBeenCalledWith(['log', '-1', '--pretty=%s'], '/remote/repo')
})
it('offers push as the next action for authenticated branches with local-only commits', async () => {
await expect(
getHostedReviewCreationEligibility({
+140 -32
View File
@@ -17,12 +17,19 @@ import { getBitbucketRepoSlug } from '../bitbucket/client'
import { getGiteaRepoSlug } from '../gitea/client'
import { createGitHubPullRequest, getRepoSlug } from '../github/client'
import { acquire, ghExecFileAsync, gitExecFileAsync, release } from '../github/gh-utils'
import { isNoUpstreamError, normalizeGitErrorMessage } from '../../shared/git-remote-error'
import type { GitUpstreamStatus } from '../../shared/types'
import { gitOptionalLocksDisabledEnv } from '../git/runner'
import { resolveDefaultBaseRefViaExec } from '../git/repo'
import { getUpstreamStatus } from '../git/upstream'
import { getProjectSlug } from '../gitlab/client'
import { getSshGitProvider } from '../providers/ssh-git-dispatch'
import { getHostedReviewForBranch } from './hosted-review'
type HostedReviewCreationEligibilityInput = HostedReviewCreationEligibilityArgs & {
connectionId?: string | null
}
function stripRefPrefix(ref: string): string {
return normalizeHostedReviewHeadRef(ref)
}
@@ -36,11 +43,14 @@ function branchToTitle(branch: string): string {
.replace(/\b\w/g, (char) => char.toUpperCase())
}
async function detectHostedReviewProvider(repoPath: string): Promise<HostedReviewProvider> {
if (await getProjectSlug(repoPath)) {
async function detectHostedReviewProvider(
repoPath: string,
connectionId?: string | null
): Promise<HostedReviewProvider> {
if (await getProjectSlug(repoPath, connectionId)) {
return 'gitlab'
}
if (await getRepoSlug(repoPath)) {
if (await getRepoSlug(repoPath, connectionId)) {
return 'github'
}
if (await getBitbucketRepoSlug(repoPath)) {
@@ -55,10 +65,16 @@ async function detectHostedReviewProvider(repoPath: string): Promise<HostedRevie
return 'unsupported'
}
async function isGitHubAuthenticated(repoPath: string): Promise<boolean> {
async function isGitHubAuthenticated(
repoPath: string,
connectionId?: string | null
): Promise<boolean> {
await acquire()
try {
await ghExecFileAsync(['auth', 'status', '--hostname', 'github.com'], { cwd: repoPath })
await ghExecFileAsync(
['auth', 'status', '--hostname', 'github.com'],
connectionId ? {} : { cwd: repoPath }
)
return true
} catch {
return false
@@ -67,9 +83,33 @@ async function isGitHubAuthenticated(repoPath: string): Promise<boolean> {
}
}
async function getLatestCommitSubject(repoPath: string): Promise<string | null> {
async function runGitForHostedReview(
repoPath: string,
args: string[],
connectionId?: string | null
): Promise<{ stdout: string; stderr?: string }> {
if (connectionId) {
const provider = getSshGitProvider(connectionId)
if (!provider) {
throw new Error(
'Remote connection dropped. Click Reconnect on the SSH target before retrying.'
)
}
return provider.exec(args, repoPath)
}
return gitExecFileAsync(args, { cwd: repoPath })
}
async function getLatestCommitSubject(
repoPath: string,
connectionId?: string | null
): Promise<string | null> {
try {
const { stdout } = await gitExecFileAsync(['log', '-1', '--pretty=%s'], { cwd: repoPath })
const { stdout } = await runGitForHostedReview(
repoPath,
['log', '-1', '--pretty=%s'],
connectionId
)
const subject = stdout.trim()
return subject || null
} catch {
@@ -77,14 +117,19 @@ async function getLatestCommitSubject(repoPath: string): Promise<string | null>
}
}
async function getCommitSummaryBody(repoPath: string, base: string | null): Promise<string | null> {
async function getCommitSummaryBody(
repoPath: string,
base: string | null,
connectionId?: string | null
): Promise<string | null> {
if (!base) {
return null
}
try {
const { stdout } = await gitExecFileAsync(
const { stdout } = await runGitForHostedReview(
repoPath,
['log', '--pretty=format:- %s', '--max-count=20', `${base}..HEAD`],
{ cwd: repoPath }
connectionId
)
const body = stdout.trim()
return body || null
@@ -93,18 +138,34 @@ async function getCommitSummaryBody(repoPath: string, base: string | null): Prom
}
}
async function getDefaultBaseRef(repoPath: string): Promise<string | null> {
return resolveDefaultBaseRefViaExec((argv) => gitExecFileAsync(argv, { cwd: repoPath }))
async function getDefaultBaseRef(
repoPath: string,
connectionId?: string | null
): Promise<string | null> {
return resolveDefaultBaseRefViaExec((argv) => runGitForHostedReview(repoPath, argv, connectionId))
}
async function getCurrentBranch(repoPath: string): Promise<string> {
const { stdout } = await gitExecFileAsync(['rev-parse', '--abbrev-ref', 'HEAD'], {
cwd: repoPath
})
async function getCurrentBranch(repoPath: string, connectionId?: string | null): Promise<string> {
const { stdout } = await runGitForHostedReview(
repoPath,
['rev-parse', '--abbrev-ref', 'HEAD'],
connectionId
)
return stripRefPrefix(stdout.trim())
}
async function hasUncommittedChanges(repoPath: string): Promise<boolean> {
async function hasUncommittedChanges(
repoPath: string,
connectionId?: string | null
): Promise<boolean> {
if (connectionId) {
const { stdout } = await runGitForHostedReview(
repoPath,
['status', '--porcelain'],
connectionId
)
return stdout.trim().length > 0
}
const { stdout } = await gitExecFileAsync(['status', '--porcelain'], {
cwd: repoPath,
// Why: create-PR validation should not take Git's optional index lock while
@@ -114,6 +175,47 @@ async function hasUncommittedChanges(repoPath: string): Promise<boolean> {
return stdout.trim().length > 0
}
async function getHostedReviewUpstreamStatus(
repoPath: string,
connectionId?: string | null
): Promise<GitUpstreamStatus> {
if (!connectionId) {
return getUpstreamStatus(repoPath)
}
try {
const { stdout: upstreamStdout } = await runGitForHostedReview(
repoPath,
['rev-parse', '--abbrev-ref', 'HEAD@{u}'],
connectionId
)
const upstreamName = upstreamStdout.trim()
if (!upstreamName) {
return { hasUpstream: false, ahead: 0, behind: 0 }
}
const { stdout: countsStdout } = await runGitForHostedReview(
repoPath,
['rev-list', '--left-right', '--count', 'HEAD...@{u}'],
connectionId
)
const tokens = countsStdout.trim().split(/\s+/)
if (tokens.length !== 2) {
throw new Error(`Unexpected git rev-list output: ${JSON.stringify(countsStdout)}`)
}
const ahead = Number.parseInt(tokens[0]!, 10)
const behind = Number.parseInt(tokens[1]!, 10)
if (!Number.isFinite(ahead) || !Number.isFinite(behind) || ahead < 0 || behind < 0) {
throw new Error(`Unparseable git rev-list counts: ${JSON.stringify(countsStdout)}`)
}
return { hasUpstream: true, upstreamName, ahead, behind }
} catch (error) {
if (isNoUpstreamError(error)) {
return { hasUpstream: false, ahead: 0, behind: 0 }
}
throw new Error(normalizeGitErrorMessage(error, 'upstream'))
}
}
const blockedCreateResultByReason = {
auth_required: {
ok: false,
@@ -191,10 +293,11 @@ function blockedEligibilityToCreateResult(
async function validateCurrentBranchCanCreateReview(
repoPath: string,
connectionId: string | null | undefined,
input: CreateHostedReviewInput
): Promise<CreateHostedReviewResult | null> {
const requestedHead = input.head ? stripRefPrefix(input.head).trim() : ''
const currentBranch = await getCurrentBranch(repoPath)
const currentBranch = await getCurrentBranch(repoPath, connectionId)
if (requestedHead && requestedHead !== currentBranch) {
return {
ok: false,
@@ -205,8 +308,8 @@ async function validateCurrentBranchCanCreateReview(
try {
const [dirty, upstreamStatus] = await Promise.all([
hasUncommittedChanges(repoPath),
getUpstreamStatus(repoPath)
hasUncommittedChanges(repoPath, connectionId),
getHostedReviewUpstreamStatus(repoPath, connectionId)
])
const eligibility = await getHostedReviewCreationEligibility({
repoPath,
@@ -215,7 +318,8 @@ async function validateCurrentBranchCanCreateReview(
hasUncommittedChanges: dirty,
hasUpstream: upstreamStatus.hasUpstream,
ahead: upstreamStatus.ahead,
behind: upstreamStatus.behind
behind: upstreamStatus.behind,
connectionId
})
// Why: renderer eligibility can be stale by submit time; the main process
// is the last chance to avoid creating a PR from an out-of-date remote head.
@@ -232,11 +336,12 @@ async function validateCurrentBranchCanCreateReview(
}
export async function getHostedReviewCreationEligibility(
args: HostedReviewCreationEligibilityArgs
args: HostedReviewCreationEligibilityInput
): Promise<HostedReviewCreationEligibility> {
const branch = stripRefPrefix(args.branch).trim()
const provider = await detectHostedReviewProvider(args.repoPath)
const defaultBaseRef = args.base?.trim() || (await getDefaultBaseRef(args.repoPath))
const provider = await detectHostedReviewProvider(args.repoPath, args.connectionId)
const defaultBaseRef =
args.base?.trim() || (await getDefaultBaseRef(args.repoPath, args.connectionId))
const baseBranch = defaultBaseRef ? normalizeHostedReviewBaseRef(defaultBaseRef) : null
const review = await getHostedReviewForBranch({
repoPath: args.repoPath,
@@ -246,11 +351,13 @@ export async function getHostedReviewCreationEligibility(
linkedGitLabMR: args.linkedGitLabMR ?? null,
linkedBitbucketPR: args.linkedBitbucketPR ?? null,
linkedAzureDevOpsPR: args.linkedAzureDevOpsPR ?? null,
linkedGiteaPR: args.linkedGiteaPR ?? null
linkedGiteaPR: args.linkedGiteaPR ?? null,
connectionId: args.connectionId ?? null
})
const title = (await getLatestCommitSubject(args.repoPath)) ?? branchToTitle(branch)
const body = await getCommitSummaryBody(args.repoPath, defaultBaseRef ?? null)
const title =
(await getLatestCommitSubject(args.repoPath, args.connectionId)) ?? branchToTitle(branch)
const body = await getCommitSummaryBody(args.repoPath, defaultBaseRef ?? null, args.connectionId)
const baseResult = {
provider,
review: review ? { number: review.number, url: review.url } : null,
@@ -294,7 +401,7 @@ export async function getHostedReviewCreationEligibility(
if ((args.behind ?? 0) > 0) {
return { ...baseResult, canCreate: false, blockedReason: 'needs_sync', nextAction: 'sync' }
}
if (!(await isGitHubAuthenticated(args.repoPath))) {
if (!(await isGitHubAuthenticated(args.repoPath, args.connectionId))) {
return {
...baseResult,
canCreate: false,
@@ -310,7 +417,8 @@ export async function getHostedReviewCreationEligibility(
export async function createHostedReview(
repoPath: string,
input: CreateHostedReviewInput
input: CreateHostedReviewInput,
connectionId?: string | null
): Promise<CreateHostedReviewResult> {
if (input.provider !== 'github') {
return {
@@ -319,7 +427,7 @@ export async function createHostedReview(
error: 'Creating reviews for this provider is not supported yet.'
}
}
const provider = await detectHostedReviewProvider(repoPath)
const provider = await detectHostedReviewProvider(repoPath, connectionId)
if (provider !== 'github') {
return {
ok: false,
@@ -327,9 +435,9 @@ export async function createHostedReview(
error: 'Creating pull requests requires a GitHub remote.'
}
}
const blocked = await validateCurrentBranchCanCreateReview(repoPath, input)
const blocked = await validateCurrentBranchCanCreateReview(repoPath, connectionId, input)
if (blocked) {
return blocked
}
return createGitHubPullRequest(repoPath, input)
return createGitHubPullRequest(repoPath, input, connectionId)
}
@@ -82,7 +82,11 @@ describe('getHostedReviewForBranch', () => {
})
await expect(
getHostedReviewForBranch({ repoPath: '/repo', branch: 'refs/heads/feature' })
getHostedReviewForBranch({
repoPath: '/repo',
connectionId: 'ssh-1',
branch: 'refs/heads/feature'
})
).resolves.toEqual({
provider: 'gitlab',
number: 7,
@@ -93,6 +97,8 @@ describe('getHostedReviewForBranch', () => {
updatedAt: '2026-05-10T00:00:00.000Z',
mergeable: 'MERGEABLE'
})
expect(getProjectSlugMock).toHaveBeenCalledWith('/repo', 'ssh-1')
expect(getMergeRequestForBranchMock).toHaveBeenCalledWith('/repo', 'feature', null, 'ssh-1')
expect(getPRForBranchMock).not.toHaveBeenCalled()
})
+7 -3
View File
@@ -119,11 +119,15 @@ export async function getHostedReviewForBranch(input: {
// Why: branch review status is tied to the branch publishing remote.
// GitHub and GitLab task/project surfaces may use richer per-provider
// source preferences, but this core status should follow origin.
const gitlabProject = await getProjectSlug(input.repoPath)
const gitlabProject = await getProjectSlug(input.repoPath, input.connectionId)
if (gitlabProject) {
const mr =
(await getMergeRequestForBranch(input.repoPath, branchName, input.linkedGitLabMR ?? null)) ??
null
(await getMergeRequestForBranch(
input.repoPath,
branchName,
input.linkedGitLabMR ?? null,
input.connectionId
)) ?? null
return mr ? mapGitLabReview(mr) : null
}