mirror of
https://github.com/stablyai/orca.git
synced 2026-11-03 02:27:43 +00:00
GitHub issue close/reopen swallowed every gh failure mentioning "already"
(e.g. "API rate limit already exceeded"), and GitLab MR close/reopen did the
same for glab. gh exits 0 when the issue is already in the target state, so
every gh failure is now reported. For glab, only its fixed no-op refusals
("this merge request has been closed" / "this merge request is already open")
count as success; "has already been merged" and all other errors surface.
Fixes #28766
Co-authored-by: Neil <neil@stably.ai>
130 lines
3.9 KiB
TypeScript
130 lines
3.9 KiB
TypeScript
import type { GitHubIssueUpdate } from '../../shared/issue-mutation-types'
|
|
import type { IssueSourcePreference } from '../../shared/repo-types'
|
|
import type { LocalGitExecOptions } from './gh-utils'
|
|
import {
|
|
resolveGitHubRepoExecution,
|
|
resolveIssueGitHubApiRepositorySource,
|
|
type GitHubApiRepository
|
|
} from './github-api-repository'
|
|
import { acquire, classifyGhError, ghExecFileAsync, release } from './gh-utils'
|
|
|
|
export async function updateIssue(
|
|
repoPath: string,
|
|
issueNumber: number,
|
|
updates: GitHubIssueUpdate,
|
|
connectionId?: string | null,
|
|
localGitOptions: LocalGitExecOptions = {},
|
|
preference?: IssueSourcePreference,
|
|
ownerRepoOverride?: GitHubApiRepository | null
|
|
): Promise<{ ok: true } | { ok: false; error: string }> {
|
|
const { ownerRepo, ghOptions } = await resolveGitHubRepoExecution(
|
|
repoPath,
|
|
ownerRepoOverride ??
|
|
(async () =>
|
|
(
|
|
await resolveIssueGitHubApiRepositorySource(
|
|
repoPath,
|
|
preference,
|
|
connectionId,
|
|
localGitOptions
|
|
)
|
|
).source),
|
|
connectionId,
|
|
localGitOptions
|
|
)
|
|
if (!ownerRepo) {
|
|
return { ok: false, error: 'Could not resolve GitHub owner/repo for this repository' }
|
|
}
|
|
|
|
const repo = `${ownerRepo.owner}/${ownerRepo.repo}`
|
|
const errors: string[] = []
|
|
|
|
if (updates.state) {
|
|
await acquire()
|
|
try {
|
|
if (updates.state === 'closed') {
|
|
const closeArgs = ['issue', 'close', String(issueNumber), '--repo', repo]
|
|
if (updates.stateReason === 'completed') {
|
|
closeArgs.push('--reason', 'completed')
|
|
} else if (updates.stateReason === 'not_planned') {
|
|
closeArgs.push('--reason', 'not planned')
|
|
} else if (updates.stateReason === 'duplicate' && updates.duplicateOf) {
|
|
closeArgs.push('--duplicate-of', String(updates.duplicateOf))
|
|
}
|
|
await ghExecFileAsync(closeArgs, ghOptions)
|
|
} else {
|
|
await ghExecFileAsync(['issue', 'reopen', String(issueNumber), '--repo', repo], ghOptions)
|
|
}
|
|
} catch (err) {
|
|
// Why: gh exits 0 when the issue is already in the target state, so any failure is real.
|
|
const stderr = err instanceof Error ? err.message : String(err)
|
|
errors.push(classifyGhError(stderr).message)
|
|
} finally {
|
|
release()
|
|
}
|
|
}
|
|
|
|
if (updates.body !== undefined) {
|
|
await acquire()
|
|
try {
|
|
await ghExecFileAsync(
|
|
[
|
|
'api',
|
|
'-X',
|
|
'PATCH',
|
|
`repos/${ownerRepo.owner}/${ownerRepo.repo}/issues/${issueNumber}`,
|
|
'--input',
|
|
'-'
|
|
],
|
|
{ ...ghOptions, stdin: JSON.stringify({ body: updates.body }) }
|
|
)
|
|
} catch (err) {
|
|
const stderr = err instanceof Error ? err.message : String(err)
|
|
errors.push(classifyGhError(stderr).message)
|
|
} finally {
|
|
release()
|
|
}
|
|
}
|
|
|
|
const editArgs: string[] = ['issue', 'edit', String(issueNumber), '--repo', repo]
|
|
let hasEditArgs = false
|
|
|
|
if (updates.title) {
|
|
editArgs.push('--title', updates.title)
|
|
hasEditArgs = true
|
|
}
|
|
for (const label of updates.addLabels ?? []) {
|
|
editArgs.push('--add-label', label)
|
|
hasEditArgs = true
|
|
}
|
|
for (const label of updates.removeLabels ?? []) {
|
|
editArgs.push('--remove-label', label)
|
|
hasEditArgs = true
|
|
}
|
|
for (const assignee of updates.addAssignees ?? []) {
|
|
editArgs.push('--add-assignee', assignee)
|
|
hasEditArgs = true
|
|
}
|
|
for (const assignee of updates.removeAssignees ?? []) {
|
|
editArgs.push('--remove-assignee', assignee)
|
|
hasEditArgs = true
|
|
}
|
|
|
|
if (hasEditArgs) {
|
|
await acquire()
|
|
try {
|
|
await ghExecFileAsync(editArgs, ghOptions)
|
|
} catch (err) {
|
|
const stderr = err instanceof Error ? err.message : String(err)
|
|
errors.push(classifyGhError(stderr).message)
|
|
} finally {
|
|
release()
|
|
}
|
|
}
|
|
|
|
if (errors.length > 0) {
|
|
return { ok: false, error: errors.join('; ') }
|
|
}
|
|
return { ok: true }
|
|
}
|