|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package repo |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "html/template" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/db" |
| 12 | + user_model "code.gitea.io/gitea/models/user" |
| 13 | + "code.gitea.io/gitea/modules/timeutil" |
| 14 | + |
| 15 | + "xorm.io/builder" |
| 16 | +) |
| 17 | + |
| 18 | +// CommitComment represents a comment on a specific line in a commit diff |
| 19 | +type CommitComment struct { |
| 20 | + ID int64 `xorm:"pk autoincr"` |
| 21 | + RepoID int64 `xorm:"INDEX"` |
| 22 | + Repo *Repository `xorm:"-"` |
| 23 | + CommitSHA string `xorm:"VARCHAR(64) INDEX"` |
| 24 | + TreePath string `xorm:"VARCHAR(4000)"` // File path (same field name as issue comments) |
| 25 | + Line int64 // - previous line / + proposed line |
| 26 | + Content string `xorm:"LONGTEXT"` |
| 27 | + ContentVersion int `xorm:"NOT NULL DEFAULT 0"` |
| 28 | + RenderedContent template.HTML `xorm:"-"` |
| 29 | + PosterID int64 `xorm:"INDEX"` |
| 30 | + Poster *user_model.User `xorm:"-"` |
| 31 | + OriginalAuthor string |
| 32 | + OriginalAuthorID int64 |
| 33 | + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` |
| 34 | + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` |
| 35 | + Attachments []*Attachment `xorm:"-"` |
| 36 | + |
| 37 | + // Fields for template compatibility with PR comments |
| 38 | + Review any `xorm:"-"` // Always nil for commit comments |
| 39 | + Invalidated bool `xorm:"-"` // Always false for commit comments |
| 40 | + ResolveDoer any `xorm:"-"` // Always nil for commit comments |
| 41 | + Reactions any `xorm:"-"` // Reactions for this comment |
| 42 | +} |
| 43 | + |
| 44 | +// IsResolved returns false (commit comments don't support resolution) |
| 45 | +func (c *CommitComment) IsResolved() bool { |
| 46 | + return false |
| 47 | +} |
| 48 | + |
| 49 | +// HasOriginalAuthor returns if a comment was migrated and has an original author |
| 50 | +func (c *CommitComment) HasOriginalAuthor() bool { |
| 51 | + return c.OriginalAuthor != "" && c.OriginalAuthorID != 0 |
| 52 | +} |
| 53 | + |
| 54 | +func init() { |
| 55 | + db.RegisterModel(new(CommitComment)) |
| 56 | +} |
| 57 | + |
| 58 | +// ErrCommitCommentNotExist represents a "CommitCommentNotExist" kind of error. |
| 59 | +type ErrCommitCommentNotExist struct { |
| 60 | + ID int64 |
| 61 | +} |
| 62 | + |
| 63 | +// IsErrCommitCommentNotExist checks if an error is a ErrCommitCommentNotExist. |
| 64 | +func IsErrCommitCommentNotExist(err error) bool { |
| 65 | + _, ok := err.(ErrCommitCommentNotExist) |
| 66 | + return ok |
| 67 | +} |
| 68 | + |
| 69 | +func (err ErrCommitCommentNotExist) Error() string { |
| 70 | + return fmt.Sprintf("commit comment does not exist [id: %d]", err.ID) |
| 71 | +} |
| 72 | + |
| 73 | +// CreateCommitComment creates a new commit comment |
| 74 | +func CreateCommitComment(ctx context.Context, comment *CommitComment) error { |
| 75 | + return db.Insert(ctx, comment) |
| 76 | +} |
| 77 | + |
| 78 | +// GetCommitCommentByID returns a commit comment by ID |
| 79 | +func GetCommitCommentByID(ctx context.Context, id int64) (*CommitComment, error) { |
| 80 | + comment := new(CommitComment) |
| 81 | + has, err := db.GetEngine(ctx).ID(id).Get(comment) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } else if !has { |
| 85 | + return nil, ErrCommitCommentNotExist{id} |
| 86 | + } |
| 87 | + return comment, nil |
| 88 | +} |
| 89 | + |
| 90 | +// FindCommitCommentsOptions describes the conditions to find commit comments |
| 91 | +type FindCommitCommentsOptions struct { |
| 92 | + db.ListOptions |
| 93 | + RepoID int64 |
| 94 | + CommitSHA string |
| 95 | + Path string |
| 96 | + Line int64 |
| 97 | +} |
| 98 | + |
| 99 | +// ToConds implements FindOptions interface |
| 100 | +func (opts FindCommitCommentsOptions) ToConds() builder.Cond { |
| 101 | + cond := builder.NewCond() |
| 102 | + if opts.RepoID > 0 { |
| 103 | + cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) |
| 104 | + } |
| 105 | + if opts.CommitSHA != "" { |
| 106 | + cond = cond.And(builder.Eq{"commit_sha": opts.CommitSHA}) |
| 107 | + } |
| 108 | + if opts.Path != "" { |
| 109 | + cond = cond.And(builder.Eq{"tree_path": opts.Path}) |
| 110 | + } |
| 111 | + if opts.Line != 0 { |
| 112 | + cond = cond.And(builder.Eq{"line": opts.Line}) |
| 113 | + } |
| 114 | + return cond |
| 115 | +} |
| 116 | + |
| 117 | +// FindCommitComments returns commit comments based on options |
| 118 | +func FindCommitComments(ctx context.Context, opts FindCommitCommentsOptions) ([]*CommitComment, error) { |
| 119 | + comments := make([]*CommitComment, 0, 10) |
| 120 | + sess := db.GetEngine(ctx).Where(opts.ToConds()) |
| 121 | + if opts.Page > 0 { |
| 122 | + sess = db.SetSessionPagination(sess, &opts) |
| 123 | + } |
| 124 | + return comments, sess.Find(&comments) |
| 125 | +} |
| 126 | + |
| 127 | +// LoadPoster loads the poster user |
| 128 | +func (c *CommitComment) LoadPoster(ctx context.Context) error { |
| 129 | + if c.Poster != nil { |
| 130 | + return nil |
| 131 | + } |
| 132 | + var err error |
| 133 | + c.Poster, err = user_model.GetPossibleUserByID(ctx, c.PosterID) |
| 134 | + if err != nil { |
| 135 | + if user_model.IsErrUserNotExist(err) { |
| 136 | + c.PosterID = user_model.GhostUserID |
| 137 | + c.Poster = user_model.NewGhostUser() |
| 138 | + } |
| 139 | + } |
| 140 | + return err |
| 141 | +} |
| 142 | + |
| 143 | +// LoadRepo loads the repository |
| 144 | +func (c *CommitComment) LoadRepo(ctx context.Context) error { |
| 145 | + if c.Repo != nil { |
| 146 | + return nil |
| 147 | + } |
| 148 | + var err error |
| 149 | + c.Repo, err = GetRepositoryByID(ctx, c.RepoID) |
| 150 | + return err |
| 151 | +} |
| 152 | + |
| 153 | +// LoadAttachments loads attachments |
| 154 | +func (c *CommitComment) LoadAttachments(ctx context.Context) error { |
| 155 | + if len(c.Attachments) > 0 { |
| 156 | + return nil |
| 157 | + } |
| 158 | + var err error |
| 159 | + c.Attachments, err = GetAttachmentsByCommentID(ctx, c.ID) |
| 160 | + return err |
| 161 | +} |
| 162 | + |
| 163 | +// DiffSide returns "previous" if Line is negative and "proposed" if positive |
| 164 | +func (c *CommitComment) DiffSide() string { |
| 165 | + if c.Line < 0 { |
| 166 | + return "previous" |
| 167 | + } |
| 168 | + return "proposed" |
| 169 | +} |
| 170 | + |
| 171 | +// UnsignedLine returns the absolute value of the line number |
| 172 | +func (c *CommitComment) UnsignedLine() uint64 { |
| 173 | + if c.Line < 0 { |
| 174 | + return uint64(c.Line * -1) |
| 175 | + } |
| 176 | + return uint64(c.Line) |
| 177 | +} |
| 178 | + |
| 179 | +// HashTag returns unique hash tag for comment |
| 180 | +func (c *CommitComment) HashTag() string { |
| 181 | + return fmt.Sprintf("commitcomment-%d", c.ID) |
| 182 | +} |
| 183 | + |
| 184 | +// UpdateCommitComment updates a commit comment |
| 185 | +func UpdateCommitComment(ctx context.Context, comment *CommitComment) error { |
| 186 | + _, err := db.GetEngine(ctx).ID(comment.ID).AllCols().Update(comment) |
| 187 | + return err |
| 188 | +} |
| 189 | + |
| 190 | +// DeleteCommitComment deletes a commit comment |
| 191 | +func DeleteCommitComment(ctx context.Context, comment *CommitComment) error { |
| 192 | + _, err := db.GetEngine(ctx).ID(comment.ID).Delete(comment) |
| 193 | + return err |
| 194 | +} |
0 commit comments