Skip to content

Commit fac6ef5

Browse files
committed
Merge branch 'main' into lunny/fix_issue_comment_num
2 parents d867475 + a871688 commit fac6ef5

File tree

5 files changed

+47
-51
lines changed

5 files changed

+47
-51
lines changed

models/issues/comment.go

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -607,26 +607,26 @@ func (c *Comment) LoadAttachments(ctx context.Context) error {
607607
return nil
608608
}
609609

610-
// UpdateAttachments update attachments by UUIDs for the comment
611-
func (c *Comment) UpdateAttachments(ctx context.Context, uuids []string) error {
612-
ctx, committer, err := db.TxContext(ctx)
613-
if err != nil {
614-
return err
615-
}
616-
defer committer.Close()
617-
618-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids)
619-
if err != nil {
620-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
610+
// UpdateCommentAttachments update attachments by UUIDs for the comment
611+
func UpdateCommentAttachments(ctx context.Context, c *Comment, uuids []string) error {
612+
if len(uuids) == 0 {
613+
return nil
621614
}
622-
for i := 0; i < len(attachments); i++ {
623-
attachments[i].IssueID = c.IssueID
624-
attachments[i].CommentID = c.ID
625-
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {
626-
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
615+
return db.WithTx(ctx, func(ctx context.Context) error {
616+
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids)
617+
if err != nil {
618+
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
627619
}
628-
}
629-
return committer.Commit()
620+
for i := 0; i < len(attachments); i++ {
621+
attachments[i].IssueID = c.IssueID
622+
attachments[i].CommentID = c.ID
623+
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {
624+
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
625+
}
626+
}
627+
c.Attachments = attachments
628+
return nil
629+
})
630630
}
631631

632632
// LoadAssigneeUserAndTeam if comment.Type is CommentTypeAssignees, then load assignees
@@ -893,7 +893,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
893893
// Check comment type.
894894
switch opts.Type {
895895
case CommentTypeCode:
896-
if err = updateAttachments(ctx, opts, comment); err != nil {
896+
if err = UpdateCommentAttachments(ctx, comment, opts.Attachments); err != nil {
897897
return err
898898
}
899899
if comment.ReviewID != 0 {
@@ -913,7 +913,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
913913
}
914914
fallthrough
915915
case CommentTypeReview:
916-
if err = updateAttachments(ctx, opts, comment); err != nil {
916+
if err = UpdateCommentAttachments(ctx, comment, opts.Attachments); err != nil {
917917
return err
918918
}
919919
case CommentTypeReopen, CommentTypeClose:
@@ -925,23 +925,6 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
925925
return UpdateIssueCols(ctx, opts.Issue, "updated_unix")
926926
}
927927

928-
func updateAttachments(ctx context.Context, opts *CreateCommentOptions, comment *Comment) error {
929-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, opts.Attachments)
930-
if err != nil {
931-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", opts.Attachments, err)
932-
}
933-
for i := range attachments {
934-
attachments[i].IssueID = opts.Issue.ID
935-
attachments[i].CommentID = comment.ID
936-
// No assign value could be 0, so ignore AllCols().
937-
if _, err = db.GetEngine(ctx).ID(attachments[i].ID).Update(attachments[i]); err != nil {
938-
return fmt.Errorf("update attachment [%d]: %w", attachments[i].ID, err)
939-
}
940-
}
941-
comment.Attachments = attachments
942-
return nil
943-
}
944-
945928
func createDeadlineComment(ctx context.Context, doer *user_model.User, issue *Issue, newDeadlineUnix timeutil.TimeStamp) (*Comment, error) {
946929
var content string
947930
var commentType CommentType

models/issues/comment_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ func TestCreateComment(t *testing.T) {
4545
unittest.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix))
4646
}
4747

48+
func Test_UpdateCommentAttachment(t *testing.T) {
49+
assert.NoError(t, unittest.PrepareTestDatabase())
50+
51+
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 1})
52+
attachment := repo_model.Attachment{
53+
Name: "test.txt",
54+
}
55+
assert.NoError(t, db.Insert(db.DefaultContext, &attachment))
56+
57+
err := issues_model.UpdateCommentAttachments(db.DefaultContext, comment, []string{attachment.UUID})
58+
assert.NoError(t, err)
59+
60+
attachment2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: attachment.ID})
61+
assert.EqualValues(t, attachment.Name, attachment2.Name)
62+
assert.EqualValues(t, comment.ID, attachment2.CommentID)
63+
assert.EqualValues(t, comment.IssueID, attachment2.IssueID)
64+
}
65+
4866
func TestFetchCodeComments(t *testing.T) {
4967
assert.NoError(t, unittest.PrepareTestDatabase())
5068

models/issues/issue_update.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -405,19 +405,10 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
405405
return err
406406
}
407407

408-
if len(opts.Attachments) > 0 {
409-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, opts.Attachments)
410-
if err != nil {
411-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", opts.Attachments, err)
412-
}
413-
414-
for i := 0; i < len(attachments); i++ {
415-
attachments[i].IssueID = opts.Issue.ID
416-
if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
417-
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
418-
}
419-
}
408+
if err := UpdateIssueAttachments(ctx, opts.Issue.ID, opts.Attachments); err != nil {
409+
return err
420410
}
411+
421412
if err = opts.Issue.LoadAttributes(ctx); err != nil {
422413
return err
423414
}

routers/web/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func updateAttachments(ctx *context.Context, item any, files []string) error {
602602
case *issues_model.Issue:
603603
err = issues_model.UpdateIssueAttachments(ctx, content.ID, files)
604604
case *issues_model.Comment:
605-
err = content.UpdateAttachments(ctx, files)
605+
err = issues_model.UpdateCommentAttachments(ctx, content, files)
606606
default:
607607
return fmt.Errorf("unknown Type: %T", content)
608608
}

services/pull/merge_squash.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package pull
55

66
import (
77
"fmt"
8+
"strings"
89

910
repo_model "code.gitea.io/gitea/models/repo"
1011
user_model "code.gitea.io/gitea/models/user"
@@ -65,7 +66,10 @@ func doMergeStyleSquash(ctx *mergeContext, message string) error {
6566

6667
if setting.Repository.PullRequest.AddCoCommitterTrailers && ctx.committer.String() != sig.String() {
6768
// add trailer
68-
message += fmt.Sprintf("\nCo-authored-by: %s\nCo-committed-by: %s\n", sig.String(), sig.String())
69+
if !strings.Contains(message, fmt.Sprintf("Co-authored-by: %s", sig.String())) {
70+
message += fmt.Sprintf("\nCo-authored-by: %s", sig.String())
71+
}
72+
message += fmt.Sprintf("\nCo-committed-by: %s\n", sig.String())
6973
}
7074
cmdCommit := git.NewCommand(ctx, "commit").
7175
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email).

0 commit comments

Comments
 (0)