Skip to content

Commit 3d3d6c4

Browse files
committed
Fix lint
1 parent e247bfe commit 3d3d6c4

File tree

4 files changed

+38
-64
lines changed

4 files changed

+38
-64
lines changed

models/issues/comment.go

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -589,26 +589,27 @@ func (c *Comment) LoadAttachments(ctx context.Context) error {
589589
return nil
590590
}
591591

592-
// UpdateAttachments update attachments by UUIDs for the comment
593-
func (c *Comment) UpdateAttachments(ctx context.Context, uuids []string) error {
594-
ctx, committer, err := db.TxContext(ctx)
595-
if err != nil {
596-
return err
592+
// UpdateCommentAttachments update attachments by UUIDs for the comment
593+
func UpdateCommentAttachments(ctx context.Context, c *Comment, uuids []string) error {
594+
if len(uuids) == 0 {
595+
return nil
597596
}
598-
defer committer.Close()
599597

600-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids)
601-
if err != nil {
602-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
603-
}
604-
for i := 0; i < len(attachments); i++ {
605-
attachments[i].IssueID = c.IssueID
606-
attachments[i].CommentID = c.ID
607-
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {
608-
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
598+
return db.WithTx(ctx, func(ctx context.Context) error {
599+
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids)
600+
if err != nil {
601+
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
609602
}
610-
}
611-
return committer.Commit()
603+
for i := 0; i < len(attachments); i++ {
604+
attachments[i].IssueID = c.IssueID
605+
attachments[i].CommentID = c.ID
606+
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {
607+
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
608+
}
609+
}
610+
c.Attachments = attachments
611+
return nil
612+
})
612613
}
613614

614615
// LoadAssigneeUserAndTeam if comment.Type is CommentTypeAssignees, then load assignees
@@ -875,7 +876,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
875876
// Check comment type.
876877
switch opts.Type {
877878
case CommentTypeCode:
878-
if err = updateAttachments(ctx, opts, comment); err != nil {
879+
if err = UpdateCommentAttachments(ctx, comment, opts.Attachments); err != nil {
879880
return err
880881
}
881882
if comment.ReviewID != 0 {
@@ -895,7 +896,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
895896
}
896897
fallthrough
897898
case CommentTypeReview:
898-
if err = updateAttachments(ctx, opts, comment); err != nil {
899+
if err = UpdateCommentAttachments(ctx, comment, opts.Attachments); err != nil {
899900
return err
900901
}
901902
case CommentTypeReopen, CommentTypeClose:
@@ -907,23 +908,6 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
907908
return UpdateIssueCols(ctx, opts.Issue, "updated_unix")
908909
}
909910

910-
func updateAttachments(ctx context.Context, opts *CreateCommentOptions, comment *Comment) error {
911-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, opts.Attachments)
912-
if err != nil {
913-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", opts.Attachments, err)
914-
}
915-
for i := range attachments {
916-
attachments[i].IssueID = opts.Issue.ID
917-
attachments[i].CommentID = comment.ID
918-
// No assign value could be 0, so ignore AllCols().
919-
if _, err = db.GetEngine(ctx).ID(attachments[i].ID).Update(attachments[i]); err != nil {
920-
return fmt.Errorf("update attachment [%d]: %w", attachments[i].ID, err)
921-
}
922-
}
923-
comment.Attachments = attachments
924-
return nil
925-
}
926-
927911
func createDeadlineComment(ctx context.Context, doer *user_model.User, issue *Issue, newDeadlineUnix timeutil.TimeStamp) (*Comment, error) {
928912
var content string
929913
var commentType CommentType

models/issues/issue_update.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,22 @@ func AddDeletePRBranchComment(ctx context.Context, doer *user_model.User, repo *
182182

183183
// UpdateIssueAttachments update attachments by UUIDs for the issue
184184
func UpdateIssueAttachments(ctx context.Context, issueID int64, uuids []string) (err error) {
185-
ctx, committer, err := db.TxContext(ctx)
186-
if err != nil {
187-
return err
188-
}
189-
defer committer.Close()
190-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids)
191-
if err != nil {
192-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
185+
if len(uuids) == 0 {
186+
return nil
193187
}
194-
for i := 0; i < len(attachments); i++ {
195-
attachments[i].IssueID = issueID
196-
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {
197-
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
188+
return db.WithTx(ctx, func(ctx context.Context) error {
189+
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids)
190+
if err != nil {
191+
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
198192
}
199-
}
200-
return committer.Commit()
193+
for i := 0; i < len(attachments); i++ {
194+
attachments[i].IssueID = issueID
195+
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {
196+
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
197+
}
198+
}
199+
return nil
200+
})
201201
}
202202

203203
// NewIssueOptions represents the options of a new issue.
@@ -293,19 +293,6 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
293293
return err
294294
}
295295

296-
if len(opts.Attachments) > 0 {
297-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, opts.Attachments)
298-
if err != nil {
299-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", opts.Attachments, err)
300-
}
301-
302-
for i := 0; i < len(attachments); i++ {
303-
attachments[i].IssueID = opts.Issue.ID
304-
if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
305-
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
306-
}
307-
}
308-
}
309296
if err = opts.Issue.LoadAttributes(ctx); err != nil {
310297
return err
311298
}

routers/web/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ func updateAttachments(ctx *context.Context, item any, files []string) error {
612612
case *issues_model.Issue:
613613
err = issues_model.UpdateIssueAttachments(ctx, content.ID, files)
614614
case *issues_model.Comment:
615-
err = content.UpdateAttachments(ctx, files)
615+
err = issues_model.UpdateCommentAttachments(ctx, content, files)
616616
default:
617617
return fmt.Errorf("unknown Type: %T", content)
618618
}

services/pull/pull.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
235235
notify_service.IssueChangeAssignee(ctx, issue.Poster, issue, assignee, false, assigneeCommentMap[assigneeID])
236236
}
237237
permDoer, err := access_model.GetUserRepoPermission(ctx, repo, issue.Poster)
238+
if err != nil {
239+
return err
240+
}
238241
for _, reviewer := range opts.Reviewers {
239242
if _, err = issue_service.ReviewRequest(ctx, pr.Issue, issue.Poster, &permDoer, reviewer, true); err != nil {
240243
return err

0 commit comments

Comments
 (0)