Closed
pr view --json 对已合并 PR 返回 merged=false,与 state=merged 矛盾 #484
aflyingtoAdd on Jul 27Closed at Jul 29
aflyingto
Jul 27 Comments:
Jul 27 Comments:
Verification record
- Version/branch: GitCode CLI 0.8.0 (commit
6ba5d62); local main was aligned with origin/main atdb6744bduring reproduction - Command:
gitcode pr list -R infra-test/gctest1 --state merged --limit 5 --json - Actual result: merged PRs 13, 12, and 1 all returned
state="merged",merged=false, and a non-emptymerged_at - API fact: the upstream detail response uses
merged: null; decoding that into a Go bool produces the zero value false - Impact: text output is correct because it falls back to state, while machine-readable JSON is contradictory
- Conclusion: reproduced on the permitted infra-test repository; continue with the fix


aflyingto
Jul 27 Comments:
Jul 27 Comments:
Development progress
- Root cause: GitCode returns
merged: nullfor merged PRs; decoding into Go bool produced false - Main change: normalize merged from explicit flag,
state=merged, or non-emptymerged_atat API boundaries - Tests: unit, full race, system, core regression, and real read-only commands passed
- Real command repository:
infra-test/gctest1 - CI: https://github.com/gitcode-cli/cli/actions/runs/30264943095 (all jobs successful)
- Security: no token, authentication, permission, or write-path behavior changed
- Documentation: COMMANDS, AI-GUIDE, and REGRESSION updated
- Risk: high because this corrects a JSON contract; independent review and human confirmation remain required
- Associated PR: https://gitcode.com/gitcode-cli/cli/merge_requests/418


现象
gc pr view <n> --json对已合并 PR 返回state=merged但merged=false,两个字段自相矛盾。依赖merged布尔值判断合入状态的脚本或 AI 代理会误判为“未合并”。实测(
gitcode-cli/gc-api-doc三个已合并 PR):根因
不是字段映射错误,而是上游返回
null。直接调 API 可见:三个 PR 的
merged全部为null,说明这是 GitCode API 的一贯行为,而非个例。api/queries_pr.go:23将其声明为值类型:Merged bool `json:"merged"`Go 把 JSON
null反序列化进bool会保留零值,所以merged必然为false。权威信号实际在state和merged_at。影响范围
已修正的路径(有
State兜底,行为正确):pkg/output/prs.go:126、pkg/output/prs.go:148:pr.Merged || pr.State == "merged"gc pr view文本输出经State显示为merged(实测正确)gc pr list --state merged实测正确受影响的路径:
gc pr view --json:直接输出merged: false,无兜底,这是最容易被代理消费的出口pkg/cmd/pr/view/view.go:333:else if pr.Merged分支永远不成立,仅因前面的State分支才碰巧正确pkg/cmd/pr/merge/merge.go:161:if mergedPR != nil && mergedPR.Merged恒为假,result.Merged依赖硬编码的true兜底建议修复方向
Merged改为*bool,区分null与falseIsMerged()),以state == "merged"或merged_at != nil为准,供各调用点统一使用--json输出保证merged与state语义一致merged=null的反序列化测试用例具体取舍建议由维护者确认后再实施,尤其是
--json字段语义变更可能影响既有消费方。环境
6ba5d62)gitcode-cli/gc-api-doc(非 infra-test,因该现象需已合并 PR 才能复现;全部为只读查询,未产生写入)