Skip to content

feat: add CreatePullRequestInlineComment for line-level PR comments#1

Merged
zy84338719 merged 1 commit into
masterfrom
feat/inline-comment-support
Jun 24, 2026
Merged

feat: add CreatePullRequestInlineComment for line-level PR comments#1
zy84338719 merged 1 commit into
masterfrom
feat/inline-comment-support

Conversation

@zy84338719

Copy link
Copy Markdown
Member

Problem

GitCode 的 PR comment API 接受 line + side 参数来实现行内评论(inline comment),但现有的 CreatePullRequestComment 只发送 position(diff position),导致评论无法在 GitCode Web UI 上显示为行内评论。

Solution

  • PullRequestComment 结构体新增 LineSide 字段
  • 新增 CreatePullRequestInlineCommentOptions 结构体(body/path/line/side/commit_id)
  • 新增 CreatePullRequestInlineComment 方法,发送正确的参数实现行内评论

Test

已验证 GitCode API 接受 line + side + path 参数并返回 201:

curl -X POST 'https://api.gitcode.com/api/v5/repos/yi-nology/iam-server/pulls/21/comments?access_token=...'
  -d '{"body":"test","path":"main.go","line":10,"side":"RIGHT"}'
# => 201 Created

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
Copilot AI review requested due to automatic review settings June 24, 2026 18:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 PullRequestComment with Line and Side fields.
  • Added CreatePullRequestInlineCommentOptions (body/path/line/side/commit_id).
  • Added Client.CreatePullRequestInlineComment to POST inline comment payloads to the PR comments endpoint.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pulls.go
Path string `json:"path"`
Position int `json:"position"`
Line int `json:"line"`
Side string `json:"side"`
Comment thread pulls.go
Comment on lines +79 to +85
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"`
}
Comment thread pulls.go
Comment on lines +258 to +262
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 zy84338719 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 方法中添加对必填字段(BodyPathLineSide)的基本校验,提前返回有意义的错误信息。

[frontend] 潜在问题:Line 字段类型为 int,零值为 0。当调用者不设置 Line(例如使用 position 方式定位评论)时,JSON 序列化会发送 "line": 0,这可能导致 GitHub API 返回错误(因为行号 0 是无效的)。

建议添加 omitempty tag,或者考虑使用 *int 指针类型来区分"未设置"和"设置为 0"的情况。同理,Side 字段也建议添加 omitempty,以保持与项目中其他 Options 结构体的一致性(如 CreatePullRequestOptions 中可选字段使用 omitempty)。

[backend] 潜在问题:LineSide 字段缺少 omitempty 标签。当调用方未设置这些字段时,JSON 序列化会将 Line: 0Side: "" 发送到 API,可能导致 API 返回错误(GitHub API 中 line 从 1 开始,side 必须是 "LEFT" 或 "RIGHT")。

如果这些字段是创建 inline comment 的必填项,建议在方法中添加参数校验(例如检查 Line > 0Side 为合法值、BodyPath 非空),以便在客户端提前返回有意义的错误信息,而不是依赖 API 端的报错。

如果这些字段是可选的,则应添加 omitempty 标签。

Comment thread pulls.go
UpdatedAt time.Time `json:"updated_at"`
}

type CreatePullRequestInlineCommentOptions struct {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 方法中添加对必填字段(BodyPathLineSide)的基本校验,提前返回有意义的错误信息。

[frontend] 潜在问题:Line 字段类型为 int,零值为 0。当调用者不设置 Line(例如使用 position 方式定位评论)时,JSON 序列化会发送 "line": 0,这可能导致 GitHub API 返回错误(因为行号 0 是无效的)。

建议添加 omitempty tag,或者考虑使用 *int 指针类型来区分"未设置"和"设置为 0"的情况。同理,Side 字段也建议添加 omitempty,以保持与项目中其他 Options 结构体的一致性(如 CreatePullRequestOptions 中可选字段使用 omitempty)。

[backend] 潜在问题:LineSide 字段缺少 omitempty 标签。当调用方未设置这些字段时,JSON 序列化会将 Line: 0Side: "" 发送到 API,可能导致 API 返回错误(GitHub API 中 line 从 1 开始,side 必须是 "LEFT" 或 "RIGHT")。

如果这些字段是创建 inline comment 的必填项,建议在方法中添加参数校验(例如检查 Line > 0Side 为合法值、BodyPath 非空),以便在客户端提前返回有意义的错误信息,而不是依赖 API 端的报错。

如果这些字段是可选的,则应添加 omitempty 标签。

@zy84338719 zy84338719 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 方法中添加对必填字段(BodyPathLineSide)的基本校验,提前返回有意义的错误信息。

[frontend] 潜在问题:Line 字段类型为 int,零值为 0。当调用者不设置 Line(例如使用 position 方式定位评论)时,JSON 序列化会发送 "line": 0,这可能导致 GitHub API 返回错误(因为行号 0 是无效的)。

建议添加 omitempty tag,或者考虑使用 *int 指针类型来区分"未设置"和"设置为 0"的情况。同理,Side 字段也建议添加 omitempty,以保持与项目中其他 Options 结构体的一致性(如 CreatePullRequestOptions 中可选字段使用 omitempty)。

[backend] 潜在问题:LineSide 字段缺少 omitempty 标签。当调用方未设置这些字段时,JSON 序列化会将 Line: 0Side: "" 发送到 API,可能导致 API 返回错误(GitHub API 中 line 从 1 开始,side 必须是 "LEFT" 或 "RIGHT")。

如果这些字段是创建 inline comment 的必填项,建议在方法中添加参数校验(例如检查 Line > 0Side 为合法值、BodyPath 非空),以便在客户端提前返回有意义的错误信息,而不是依赖 API 端的报错。

如果这些字段是可选的,则应添加 omitempty 标签。

Comment thread pulls.go
UpdatedAt time.Time `json:"updated_at"`
}

type CreatePullRequestInlineCommentOptions struct {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 方法中添加对必填字段(BodyPathLineSide)的基本校验,提前返回有意义的错误信息。

[frontend] 潜在问题:Line 字段类型为 int,零值为 0。当调用者不设置 Line(例如使用 position 方式定位评论)时,JSON 序列化会发送 "line": 0,这可能导致 GitHub API 返回错误(因为行号 0 是无效的)。

建议添加 omitempty tag,或者考虑使用 *int 指针类型来区分"未设置"和"设置为 0"的情况。同理,Side 字段也建议添加 omitempty,以保持与项目中其他 Options 结构体的一致性(如 CreatePullRequestOptions 中可选字段使用 omitempty)。

[backend] 潜在问题:LineSide 字段缺少 omitempty 标签。当调用方未设置这些字段时,JSON 序列化会将 Line: 0Side: "" 发送到 API,可能导致 API 返回错误(GitHub API 中 line 从 1 开始,side 必须是 "LEFT" 或 "RIGHT")。

如果这些字段是创建 inline comment 的必填项,建议在方法中添加参数校验(例如检查 Line > 0Side 为合法值、BodyPath 非空),以便在客户端提前返回有意义的错误信息,而不是依赖 API 端的报错。

如果这些字段是可选的,则应添加 omitempty 标签。

@zy84338719 zy84338719 merged commit 4a3b2a5 into master Jun 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants