Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add multi-line comment support in api #25523

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions models/issues/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ type Comment struct {
TreePath string
Content string `xorm:"LONGTEXT"`
RenderedContent string `xorm:"-"`
StartLine int64 // - previous line / + proposed line
IsMultiLine bool `xorm:"NOT NULL DEFAULT false"`

// Path represents the 4 lines of code cemented by this comment
Patch string `xorm:"-"`
Expand Down Expand Up @@ -724,6 +726,14 @@ func (c *Comment) UnsignedLine() uint64 {
return uint64(c.Line)
}

// UnsignedStartLine returns the start line of the code comment without + or -
func (c *Comment) UnsignedStartLine() uint64 {
if c.StartLine < 0 {
return uint64(c.StartLine * -1)
}
return uint64(c.StartLine)
}

// CodeCommentLink returns the url to a comment in code
func (c *Comment) CodeCommentLink() string {
err := c.LoadIssue(db.DefaultContext)
Expand Down Expand Up @@ -800,6 +810,8 @@ func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment,
CommitID: opts.CommitID,
CommitSHA: opts.CommitSHA,
Line: opts.LineNum,
StartLine: opts.StartLineNum,
IsMultiLine: opts.IsMultiLine,
Content: opts.Content,
OldTitle: opts.OldTitle,
NewTitle: opts.NewTitle,
Expand Down Expand Up @@ -976,6 +988,8 @@ type CreateCommentOptions struct {
CommitSHA string
Patch string
LineNum int64
StartLineNum int64
IsMultiLine bool
TreePath string
ReviewID int64
Content string
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ var migrations = []Migration{
NewMigration("Drop deleted branch table", v1_21.DropDeletedBranchTable),
// v270 -> v271
NewMigration("Fix PackageProperty typo", v1_21.FixPackagePropertyTypo),
// v271 -> v272
NewMigration("Add start_line and is_multi_line Column in comment table", v1_21.AddStartLineAndIsMultiLineToComment),
}

// GetCurrentDBVersion returns the current db version
Expand Down
17 changes: 17 additions & 0 deletions models/migrations/v1_21/v271.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_21 //nolint

import (
"xorm.io/xorm"
)

func AddStartLineAndIsMultiLineToComment(x *xorm.Engine) error {
type Comment struct {
StartLine int64 // - previous line / + proposed line
IsMultiLine bool `xorm:"NOT NULL DEFAULT false"`
}

return x.Sync(new(Comment))
}
16 changes: 14 additions & 2 deletions modules/structs/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ type PullReviewComment struct {

HTMLURL string `json:"html_url"`
HTMLPullURL string `json:"pull_request_url"`

// The line of the blob to which the comment applies. The last line of the range for a multi-line comment.
Line uint64 `json:"line"`
// The side of the diff to which the comment applies. The side of the last line of the range for a multi-line comment.
Side string `json:"side"`
// The first line of the range for a multi-line comment.
StartLine uint64 `json:"start_line,omitempty"`
}

// CreatePullReviewOptions are options to create a pull review
Expand All @@ -83,10 +90,15 @@ type CreatePullReviewComment struct {
// the tree path
Path string `json:"path"`
Body string `json:"body"`
// if comment to old file line or 0
// if comment to old file line or 0 (This parameter is deprecated. Suggest use line and side instead)
OldLineNum int64 `json:"old_position"`
// if comment to new file line or 0
// if comment to new file line or 0 (This parameter is deprecated. Suggest use line and side instead)
NewLineNum int64 `json:"new_position"`
// The line of the blob in the pull request diff that the comment applies to. For a multi-line comment, the last line of the range that your comment applies to.
Line *int64 `json:"line"`
// Can be LEFT or RIGHT. Use LEFT for deletions that appear in red. Use RIGHT for additions that appear in green.
Side *string `json:"side"`
StartLine *int64 `json:"start_line"`
}

// SubmitPullReviewOptions are options to submit a pending pull review
Expand Down
18 changes: 18 additions & 0 deletions routers/api/v1/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,29 @@ func CreatePullReview(ctx *context.APIContext) {
line = c.OldLineNum * -1
}

if c.Side != nil && c.Line != nil && *c.Line > 0 {
line = *c.Line
if strings.ToUpper(*c.Side) == "LEFT" {
line *= -1
}
}

var startLine int64
isMultiLine := c.Side != nil && c.StartLine != nil && *c.StartLine > 0
if isMultiLine {
startLine = *c.StartLine
if strings.ToUpper(*c.Side) == "LEFT" {
startLine *= -1
}
}

if _, err := pull_service.CreateCodeComment(ctx,
ctx.Doer,
ctx.Repo.GitRepo,
pr.Issue,
line,
startLine,
isMultiLine,
c.Body,
c.Path,
true, // pending review
Expand Down
2 changes: 2 additions & 0 deletions routers/web/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func CreateCodeComment(ctx *context.Context) {
ctx.Repo.GitRepo,
issue,
signedLine,
0,
false,
form.Content,
form.TreePath,
!form.SingleReview,
Expand Down
6 changes: 6 additions & 0 deletions services/convert/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,16 @@ func ToPullReviewCommentList(ctx context.Context, review *issues_model.Review, d
}

if comment.Line < 0 {
apiComment.Side = "LEFT"
apiComment.OldLineNum = comment.UnsignedLine()
} else {
apiComment.Side = "RIGHT"
apiComment.LineNum = comment.UnsignedLine()
}
if comment.IsMultiLine {
apiComment.StartLine = comment.UnsignedStartLine()
}
apiComment.Line = comment.UnsignedLine()
apiComments = append(apiComments, apiComment)
}
}
Expand Down
2 changes: 2 additions & 0 deletions services/mailer/incoming/incoming_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (h *ReplyHandler) Handle(ctx context.Context, content *MailContent, doer *u
nil,
issue,
comment.Line,
0,
false,
content.Content,
comment.TreePath,
false, // not pending review but a single review
Expand Down
45 changes: 31 additions & 14 deletions services/pull/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func InvalidateCodeComments(ctx context.Context, prs issues_model.PullRequestLis
}

// CreateCodeComment creates a comment on the code line
func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.Repository, issue *issues_model.Issue, line int64, content, treePath string, pendingReview bool, replyReviewID int64, latestCommitID string) (*issues_model.Comment, error) {
func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.Repository, issue *issues_model.Issue, line, startLine int64, isMultiLine bool, content, treePath string, pendingReview bool, replyReviewID int64, latestCommitID string) (*issues_model.Comment, error) {
var (
existsReview bool
err error
Expand Down Expand Up @@ -103,7 +103,9 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
content,
treePath,
line,
startLine,
replyReviewID,
isMultiLine,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -143,7 +145,9 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
content,
treePath,
line,
startLine,
review.ID,
isMultiLine,
)
if err != nil {
return nil, err
Expand All @@ -162,7 +166,7 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
}

// createCodeComment creates a plain code comment at the specified line / path
func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content, treePath string, line, reviewID int64) (*issues_model.Comment, error) {
func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content, treePath string, line, startLine, reviewID int64, isMultiLine bool) (*issues_model.Comment, error) {
var commitID, patch string
if err := issue.LoadPullRequest(ctx); err != nil {
return nil, fmt.Errorf("LoadPullRequest: %w", err)
Expand All @@ -177,6 +181,10 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo
}
defer closer.Close()

if isMultiLine && (startLine*line < 0 || startLine > line) {
return nil, fmt.Errorf("not supported multi line format: %d, %d", startLine, line)
}

invalidated := false
head := pr.GetGitRefName()
if line > 0 {
Expand Down Expand Up @@ -242,24 +250,33 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo
_ = writer.Close()
}()

patch, err = git.CutDiffAroundLine(reader, int64((&issues_model.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
var cutLineNum int
if isMultiLine {
cutLineNum = int(line - startLine + 1)
} else {
cutLineNum = setting.UI.CodeCommentLines
}

patch, err = git.CutDiffAroundLine(reader, int64((&issues_model.Comment{Line: line}).UnsignedLine()), line < 0, cutLineNum)
if err != nil {
log.Error("Error whilst generating patch: %v", err)
return nil, err
}
}
return issue_service.CreateComment(ctx, &issues_model.CreateCommentOptions{
Type: issues_model.CommentTypeCode,
Doer: doer,
Repo: repo,
Issue: issue,
Content: content,
LineNum: line,
TreePath: treePath,
CommitSHA: commitID,
ReviewID: reviewID,
Patch: patch,
Invalidated: invalidated,
Type: issues_model.CommentTypeCode,
Doer: doer,
Repo: repo,
Issue: issue,
Content: content,
LineNum: line,
StartLineNum: startLine,
IsMultiLine: isMultiLine,
TreePath: treePath,
CommitSHA: commitID,
ReviewID: reviewID,
Patch: patch,
Invalidated: invalidated,
})
}

Expand Down
37 changes: 35 additions & 2 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.