Add GitHub task close reason parity (#6141)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo Hong
2026-06-23 15:18:46 -07:00
committed by GitHub
co-authored by Orca
parent a2fd8c11c2
commit ce403c09d5
14 changed files with 2141 additions and 633 deletions
+48
View File
@@ -251,4 +251,52 @@ describe('issue source operations', () => {
{ cwd: '/repo-root' }
)
})
it('closes issues with completed, not planned, and duplicate reasons', async () => {
getIssueOwnerRepoMock.mockResolvedValue({ owner: 'stablyai', repo: 'orca' })
ghExecFileAsyncMock.mockResolvedValue({ stdout: '' })
await expect(
updateIssue('/repo-root', 924, { state: 'closed', stateReason: 'completed' })
).resolves.toEqual({ ok: true })
await expect(
updateIssue('/repo-root', 925, { state: 'closed', stateReason: 'not_planned' })
).resolves.toEqual({ ok: true })
await expect(
updateIssue('/repo-root', 926, {
state: 'closed',
stateReason: 'duplicate',
duplicateOf: 99
})
).resolves.toEqual({ ok: true })
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
1,
['issue', 'close', '924', '--repo', 'stablyai/orca', '--reason', 'completed'],
{ cwd: '/repo-root' }
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
['issue', 'close', '925', '--repo', 'stablyai/orca', '--reason', 'not planned'],
{ cwd: '/repo-root' }
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
3,
['issue', 'close', '926', '--repo', 'stablyai/orca', '--duplicate-of', '99'],
{ cwd: '/repo-root' }
)
})
it('reopens issues through gh issue reopen', async () => {
getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'stablyai', repo: 'orca' })
ghExecFileAsyncMock.mockResolvedValueOnce({ stdout: '' })
await expect(updateIssue('/repo-root', 924, { state: 'open' })).resolves.toEqual({
ok: true
})
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
['issue', 'reopen', '924', '--repo', 'stablyai/orca'],
{ cwd: '/repo-root' }
)
})
})
@@ -0,0 +1,121 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { acquireMock, ghExecFileAsyncMock, releaseMock, runRestMock } = vi.hoisted(() => ({
acquireMock: vi.fn(),
ghExecFileAsyncMock: vi.fn(),
releaseMock: vi.fn(),
runRestMock: vi.fn()
}))
vi.mock('./internals', () => ({
acquire: acquireMock,
release: releaseMock,
extractExecError: (err: unknown) => ({
stderr: err instanceof Error ? err.message : String(err),
stdout: ''
}),
ghExecFileAsync: ghExecFileAsyncMock,
rateLimitGuard: () => ({ blocked: false }),
noteRateLimitSpend: vi.fn(),
classifyProjectError: (stderr: string) => ({ type: 'unknown', message: stderr }),
rateLimitedError: () => ({ type: 'rate_limited', message: 'rate limited' }),
runGraphql: vi.fn(),
runRest: runRestMock,
validateSlugArgs: (owner: string, repo: string) =>
owner && repo ? { ok: true } : { ok: false, error: { type: 'validation_error' } },
assertPositiveInt: (value: number, name: string) =>
Number.isInteger(value) && value > 0
? { ok: true, value }
: { ok: false, error: { type: 'validation_error', message: `${name} invalid` } }
}))
import { updateIssueBySlug } from './mutations'
describe('updateIssueBySlug', () => {
beforeEach(() => {
acquireMock.mockReset().mockResolvedValue(undefined)
ghExecFileAsyncMock.mockReset().mockResolvedValue({ stdout: '', stderr: '' })
releaseMock.mockReset()
runRestMock.mockReset().mockResolvedValue({ ok: true, data: {} })
})
it('closes slug-addressed issues with completed and not planned reasons via gh issue close', async () => {
await expect(
updateIssueBySlug({
owner: 'acme',
repo: 'widgets',
number: 12,
updates: { state: 'closed', stateReason: 'completed' }
})
).resolves.toEqual({ ok: true })
await expect(
updateIssueBySlug({
owner: 'acme',
repo: 'widgets',
number: 13,
updates: { state: 'closed', stateReason: 'not_planned' }
})
).resolves.toEqual({ ok: true })
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
1,
['issue', 'close', '12', '--repo', 'acme/widgets', '--reason', 'completed'],
{ encoding: 'utf-8' }
)
expect(ghExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
['issue', 'close', '13', '--repo', 'acme/widgets', '--reason', 'not planned'],
{ encoding: 'utf-8' }
)
expect(runRestMock).not.toHaveBeenCalled()
})
it('closes slug-addressed duplicate issues with --duplicate-of', async () => {
await expect(
updateIssueBySlug({
owner: 'acme',
repo: 'widgets',
number: 12,
updates: { state: 'closed', stateReason: 'duplicate', duplicateOf: 99 }
})
).resolves.toEqual({ ok: true })
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
['issue', 'close', '12', '--repo', 'acme/widgets', '--duplicate-of', '99'],
{ encoding: 'utf-8' }
)
expect(runRestMock).not.toHaveBeenCalled()
})
it('rejects duplicate closes without a target before spawning gh', async () => {
const result = await updateIssueBySlug({
owner: 'acme',
repo: 'widgets',
number: 12,
updates: { state: 'closed', stateReason: 'duplicate' }
})
expect(result).toMatchObject({
ok: false,
error: { type: 'validation_error' }
})
expect(ghExecFileAsyncMock).not.toHaveBeenCalled()
expect(runRestMock).not.toHaveBeenCalled()
})
it('reopens slug-addressed issues via gh issue reopen', async () => {
await expect(
updateIssueBySlug({
owner: 'acme',
repo: 'widgets',
number: 12,
updates: { state: 'open' }
})
).resolves.toEqual({ ok: true })
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
['issue', 'reopen', '12', '--repo', 'acme/widgets'],
{ encoding: 'utf-8' }
)
})
})
+53 -16
View File
@@ -183,17 +183,63 @@ export async function updateIssueBySlug(
removeAssignees
} = args.updates
// Title / body / state go through PATCH /repos/{owner}/{repo}/issues/{n}.
if (duplicateOf !== undefined && (state !== 'closed' || stateReason !== 'duplicate')) {
return {
ok: false,
error: {
type: 'validation_error',
message: 'Duplicate target is only valid when closing as duplicate.'
}
}
}
if (state === 'closed' && stateReason === 'duplicate' && duplicateOf === undefined) {
return {
ok: false,
error: {
type: 'validation_error',
message: 'Duplicate target issue number is required.'
}
}
}
if (duplicateOf !== undefined) {
const duplicate = assertPositiveInt(duplicateOf, 'duplicateOf')
if (!duplicate.ok) {
return { ok: false, error: duplicate.error }
}
}
// Title/body go through PATCH /repos/{owner}/{repo}/issues/{n}.
// State uses gh issue close/reopen so duplicate closes can record a target.
// Labels/assignees go through their dedicated endpoints.
const base = `repos/${args.owner}/${args.repo}/issues/${args.number}`
if (state !== undefined) {
const stateArgs =
state === 'closed'
? ['issue', 'close', String(args.number), '--repo', `${args.owner}/${args.repo}`]
: ['issue', 'reopen', String(args.number), '--repo', `${args.owner}/${args.repo}`]
if (state === 'closed') {
if (stateReason === 'completed') {
stateArgs.push('--reason', 'completed')
} else if (stateReason === 'not_planned') {
stateArgs.push('--reason', 'not planned')
} else if (stateReason === 'duplicate') {
stateArgs.push('--duplicate-of', String(duplicateOf))
}
}
await acquire()
try {
await ghExecFileAsync(stateArgs, { encoding: 'utf-8' })
} catch (err) {
const { stderr, stdout } = extractExecError(err)
return { ok: false, error: classifyProjectError(stderr, stdout) }
} finally {
release()
}
}
// 1) PATCH body
if (
title !== undefined ||
body !== undefined ||
state !== undefined ||
stateReason !== undefined
) {
if (title !== undefined || body !== undefined) {
const patchArgs: string[] = ['-X', 'PATCH', base]
if (title !== undefined) {
patchArgs.push('--raw-field', `title=${title}`)
@@ -201,15 +247,6 @@ export async function updateIssueBySlug(
if (body !== undefined) {
patchArgs.push('--raw-field', `body=${body}`)
}
if (state !== undefined) {
patchArgs.push('--raw-field', `state=${state}`)
}
if (stateReason !== undefined) {
patchArgs.push('--raw-field', `state_reason=${stateReason}`)
}
if (duplicateOf !== undefined) {
patchArgs.push('--raw-field', `duplicate_of=${duplicateOf}`)
}
const r = await runRest<unknown>(patchArgs)
if (!r.ok) {
return { ok: false, error: r.error }