feat: add CreatePullRequestInlineComment for line-level PR comments#1
Conversation
GitCode's PR comment API accepts and parameters for inline (line-level) comments, but the existing CreatePullRequestComment only sends (diff position), which doesn't produce inline comments on the GitCode web UI. Changes: - Add Line and Side fields to PullRequestComment struct - Add CreatePullRequestInlineCommentOptions with body/path/line/side/commit_id - Add CreatePullRequestInlineComment method that sends the correct parameters for GitCode inline comments
There was a problem hiding this comment.
Pull request overview
This PR adds first-class support for GitCode “inline” pull request comments by sending line + side (instead of only position), so comments render correctly at the file/line level in the GitCode Web UI.
Changes:
- Extended
PullRequestCommentwithLineandSidefields. - Added
CreatePullRequestInlineCommentOptions(body/path/line/side/commit_id). - Added
Client.CreatePullRequestInlineCommentto POST inline comment payloads to the PR comments endpoint.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Path string `json:"path"` | ||
| Position int `json:"position"` | ||
| Line int `json:"line"` | ||
| Side string `json:"side"` |
| type CreatePullRequestInlineCommentOptions struct { | ||
| Body string `json:"body"` | ||
| Path string `json:"path"` | ||
| Line int `json:"line"` | ||
| Side string `json:"side"` | ||
| CommitID string `json:"commit_id,omitempty"` | ||
| } |
| func (c *Client) CreatePullRequestInlineComment(ctx context.Context, owner, repo string, number int, opts CreatePullRequestInlineCommentOptions) (*PullRequestComment, error) { | ||
| var comment PullRequestComment | ||
| err := c.doRequest(ctx, http.MethodPost, fmt.Sprintf("/repos/%s/%s/pulls/%d/comments", owner, repo, number), opts, &comment) | ||
| if err != nil { | ||
| return nil, err |
zy84338719
left a comment
There was a problem hiding this comment.
AI Code Review
Review completed with 1 findings: 0 blockers, 1 should-fix, 0 suggestions
Findings: 0 critical, 1 major, 0 minor, 0 info
[MAJOR] 潜在问题:Line 字段类型为 int,零值为 0,且 JSON tag 中没有 omitempty。这意味着如果调用者未设置 Line,请求体中会发送 "line": 0,这在 GitHub API 中是无效的(行
File: pulls.go (line 79)
[security] 潜在问题:Line 字段类型为 int,零值为 0,且 JSON tag 中没有 omitempty。这意味着如果调用者未设置 Line,请求体中会发送 "line": 0,这在 GitHub API 中是无效的(行号从 1 开始),可能导致 API 返回不明确的错误。
建议:可以考虑将
Line改为指针类型*int,这样可以区分"未设置"和"设置为 0"的情况,或者在CreatePullRequestInlineComment方法中添加对必填字段(Body、Path、Line、Side)的基本校验,提前返回有意义的错误信息。[frontend] 潜在问题:
Line字段类型为int,零值为0。当调用者不设置Line(例如使用position方式定位评论)时,JSON 序列化会发送"line": 0,这可能导致 GitHub API 返回错误(因为行号 0 是无效的)。建议添加
omitemptytag,或者考虑使用*int指针类型来区分"未设置"和"设置为 0"的情况。同理,Side字段也建议添加omitempty,以保持与项目中其他 Options 结构体的一致性(如CreatePullRequestOptions中可选字段使用omitempty)。[backend] 潜在问题:
Line和Side字段缺少omitempty标签。当调用方未设置这些字段时,JSON 序列化会将Line: 0和Side: ""发送到 API,可能导致 API 返回错误(GitHub API 中 line 从 1 开始,side 必须是 "LEFT" 或 "RIGHT")。如果这些字段是创建 inline comment 的必填项,建议在方法中添加参数校验(例如检查
Line > 0、Side为合法值、Body和Path非空),以便在客户端提前返回有意义的错误信息,而不是依赖 API 端的报错。如果这些字段是可选的,则应添加
omitempty标签。
| UpdatedAt time.Time `json:"updated_at"` | ||
| } | ||
|
|
||
| type CreatePullRequestInlineCommentOptions struct { |
There was a problem hiding this comment.
[MAJOR] 潜在问题:Line 字段类型为 int,零值为 0,且 JSON tag 中没有 omitempty。这意味着如果调用者未设置 Line,请求体中会发送 "line": 0,这在 GitHub API 中是无效的(行
[security] 潜在问题:Line 字段类型为 int,零值为 0,且 JSON tag 中没有 omitempty。这意味着如果调用者未设置 Line,请求体中会发送 "line": 0,这在 GitHub API 中是无效的(行号从 1 开始),可能导致 API 返回不明确的错误。
建议:可以考虑将 Line 改为指针类型 *int,这样可以区分"未设置"和"设置为 0"的情况,或者在 CreatePullRequestInlineComment 方法中添加对必填字段(Body、Path、Line、Side)的基本校验,提前返回有意义的错误信息。
[frontend] 潜在问题:Line 字段类型为 int,零值为 0。当调用者不设置 Line(例如使用 position 方式定位评论)时,JSON 序列化会发送 "line": 0,这可能导致 GitHub API 返回错误(因为行号 0 是无效的)。
建议添加 omitempty tag,或者考虑使用 *int 指针类型来区分"未设置"和"设置为 0"的情况。同理,Side 字段也建议添加 omitempty,以保持与项目中其他 Options 结构体的一致性(如 CreatePullRequestOptions 中可选字段使用 omitempty)。
[backend] 潜在问题:Line 和 Side 字段缺少 omitempty 标签。当调用方未设置这些字段时,JSON 序列化会将 Line: 0 和 Side: "" 发送到 API,可能导致 API 返回错误(GitHub API 中 line 从 1 开始,side 必须是 "LEFT" 或 "RIGHT")。
如果这些字段是创建 inline comment 的必填项,建议在方法中添加参数校验(例如检查 Line > 0、Side 为合法值、Body 和 Path 非空),以便在客户端提前返回有意义的错误信息,而不是依赖 API 端的报错。
如果这些字段是可选的,则应添加 omitempty 标签。
zy84338719
left a comment
There was a problem hiding this comment.
AI Code Review
Review completed with 1 findings: 0 blockers, 1 should-fix, 0 suggestions
Findings: 0 critical, 1 major, 0 minor, 0 info
[MAJOR] 潜在问题:Line 字段类型为 int,零值为 0,且 JSON tag 中没有 omitempty。这意味着如果调用者未设置 Line,请求体中会发送 "line": 0,这在 GitHub API 中是无效的(行
File: pulls.go (line 79)
[security] 潜在问题:Line 字段类型为 int,零值为 0,且 JSON tag 中没有 omitempty。这意味着如果调用者未设置 Line,请求体中会发送 "line": 0,这在 GitHub API 中是无效的(行号从 1 开始),可能导致 API 返回不明确的错误。
建议:可以考虑将
Line改为指针类型*int,这样可以区分"未设置"和"设置为 0"的情况,或者在CreatePullRequestInlineComment方法中添加对必填字段(Body、Path、Line、Side)的基本校验,提前返回有意义的错误信息。[frontend] 潜在问题:
Line字段类型为int,零值为0。当调用者不设置Line(例如使用position方式定位评论)时,JSON 序列化会发送"line": 0,这可能导致 GitHub API 返回错误(因为行号 0 是无效的)。建议添加
omitemptytag,或者考虑使用*int指针类型来区分"未设置"和"设置为 0"的情况。同理,Side字段也建议添加omitempty,以保持与项目中其他 Options 结构体的一致性(如CreatePullRequestOptions中可选字段使用omitempty)。[backend] 潜在问题:
Line和Side字段缺少omitempty标签。当调用方未设置这些字段时,JSON 序列化会将Line: 0和Side: ""发送到 API,可能导致 API 返回错误(GitHub API 中 line 从 1 开始,side 必须是 "LEFT" 或 "RIGHT")。如果这些字段是创建 inline comment 的必填项,建议在方法中添加参数校验(例如检查
Line > 0、Side为合法值、Body和Path非空),以便在客户端提前返回有意义的错误信息,而不是依赖 API 端的报错。如果这些字段是可选的,则应添加
omitempty标签。
| UpdatedAt time.Time `json:"updated_at"` | ||
| } | ||
|
|
||
| type CreatePullRequestInlineCommentOptions struct { |
There was a problem hiding this comment.
[MAJOR] 潜在问题:Line 字段类型为 int,零值为 0,且 JSON tag 中没有 omitempty。这意味着如果调用者未设置 Line,请求体中会发送 "line": 0,这在 GitHub API 中是无效的(行
[security] 潜在问题:Line 字段类型为 int,零值为 0,且 JSON tag 中没有 omitempty。这意味着如果调用者未设置 Line,请求体中会发送 "line": 0,这在 GitHub API 中是无效的(行号从 1 开始),可能导致 API 返回不明确的错误。
建议:可以考虑将 Line 改为指针类型 *int,这样可以区分"未设置"和"设置为 0"的情况,或者在 CreatePullRequestInlineComment 方法中添加对必填字段(Body、Path、Line、Side)的基本校验,提前返回有意义的错误信息。
[frontend] 潜在问题:Line 字段类型为 int,零值为 0。当调用者不设置 Line(例如使用 position 方式定位评论)时,JSON 序列化会发送 "line": 0,这可能导致 GitHub API 返回错误(因为行号 0 是无效的)。
建议添加 omitempty tag,或者考虑使用 *int 指针类型来区分"未设置"和"设置为 0"的情况。同理,Side 字段也建议添加 omitempty,以保持与项目中其他 Options 结构体的一致性(如 CreatePullRequestOptions 中可选字段使用 omitempty)。
[backend] 潜在问题:Line 和 Side 字段缺少 omitempty 标签。当调用方未设置这些字段时,JSON 序列化会将 Line: 0 和 Side: "" 发送到 API,可能导致 API 返回错误(GitHub API 中 line 从 1 开始,side 必须是 "LEFT" 或 "RIGHT")。
如果这些字段是创建 inline comment 的必填项,建议在方法中添加参数校验(例如检查 Line > 0、Side 为合法值、Body 和 Path 非空),以便在客户端提前返回有意义的错误信息,而不是依赖 API 端的报错。
如果这些字段是可选的,则应添加 omitempty 标签。
Problem
GitCode 的 PR comment API 接受
line+side参数来实现行内评论(inline comment),但现有的CreatePullRequestComment只发送position(diff position),导致评论无法在 GitCode Web UI 上显示为行内评论。Solution
PullRequestComment结构体新增Line和Side字段CreatePullRequestInlineCommentOptions结构体(body/path/line/side/commit_id)CreatePullRequestInlineComment方法,发送正确的参数实现行内评论Test
已验证 GitCode API 接受
line+side+path参数并返回 201: