Skip to content

Commit 6151e69

Browse files
CaiCandongGiteaBot
andauthored
Delete issue_service.CreateComment (#26298)
I noticed that `issue_service.CreateComment` adds transaction operations on `issues_model.CreateComment`, we can merge the two functions and we can avoid calling each other's methods in the `services` layer. Co-authored-by: Giteabot <[email protected]>
1 parent 2de0752 commit 6151e69

File tree

5 files changed

+17
-31
lines changed

5 files changed

+17
-31
lines changed

models/issues/comment.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,12 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) {
777777

778778
// CreateComment creates comment with context
779779
func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) {
780+
ctx, committer, err := db.TxContext(ctx)
781+
if err != nil {
782+
return nil, err
783+
}
784+
defer committer.Close()
785+
780786
e := db.GetEngine(ctx)
781787
var LabelID int64
782788
if opts.Label != nil {
@@ -832,7 +838,9 @@ func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment,
832838
if err = comment.AddCrossReferences(ctx, opts.Doer, false); err != nil {
833839
return nil, err
834840
}
835-
841+
if err = committer.Commit(); err != nil {
842+
return nil, err
843+
}
836844
return comment, nil
837845
}
838846

services/issue/comments.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,6 @@ import (
1515
"code.gitea.io/gitea/modules/timeutil"
1616
)
1717

18-
// CreateComment creates comment of issue or commit.
19-
func CreateComment(ctx context.Context, opts *issues_model.CreateCommentOptions) (comment *issues_model.Comment, err error) {
20-
ctx, committer, err := db.TxContext(ctx)
21-
if err != nil {
22-
return nil, err
23-
}
24-
defer committer.Close()
25-
26-
comment, err = issues_model.CreateComment(ctx, opts)
27-
if err != nil {
28-
return nil, err
29-
}
30-
31-
if err = committer.Commit(); err != nil {
32-
return nil, err
33-
}
34-
35-
return comment, nil
36-
}
37-
3818
// CreateRefComment creates a commit reference comment to issue.
3919
func CreateRefComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content, commitSHA string) error {
4020
if len(commitSHA) == 0 {
@@ -53,7 +33,7 @@ func CreateRefComment(ctx context.Context, doer *user_model.User, repo *repo_mod
5333
return nil
5434
}
5535

56-
_, err = CreateComment(ctx, &issues_model.CreateCommentOptions{
36+
_, err = issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
5737
Type: issues_model.CommentTypeCommitRef,
5838
Doer: doer,
5939
Repo: repo,
@@ -66,7 +46,7 @@ func CreateRefComment(ctx context.Context, doer *user_model.User, repo *repo_mod
6646

6747
// CreateIssueComment creates a plain issue comment.
6848
func CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content string, attachments []string) (*issues_model.Comment, error) {
69-
comment, err := CreateComment(ctx, &issues_model.CreateCommentOptions{
49+
comment, err := issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
7050
Type: issues_model.CommentTypeComment,
7151
Doer: doer,
7252
Repo: repo,

services/pull/comment.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
user_model "code.gitea.io/gitea/models/user"
1212
"code.gitea.io/gitea/modules/git"
1313
"code.gitea.io/gitea/modules/json"
14-
issue_service "code.gitea.io/gitea/services/issue"
1514
)
1615

1716
// getCommitIDsFromRepo get commit IDs from repo in between oldCommitID and newCommitID
@@ -90,7 +89,7 @@ func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *iss
9089

9190
ops.Content = string(dataJSON)
9291

93-
comment, err = issue_service.CreateComment(ctx, ops)
92+
comment, err = issues_model.CreateComment(ctx, ops)
9493

9594
return comment, err
9695
}

services/pull/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, pull *issu
125125
Content: string(dataJSON),
126126
}
127127

128-
_, _ = issue_service.CreateComment(ctx, ops)
128+
_, _ = issues_model.CreateComment(ctx, ops)
129129

130130
if !pr.IsWorkInProgress() {
131131
if err := issues_model.PullRequestCodeOwnersReview(ctx, pull, pr); err != nil {
@@ -231,7 +231,7 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer
231231
OldRef: oldBranch,
232232
NewRef: targetBranch,
233233
}
234-
if _, err = issue_service.CreateComment(ctx, options); err != nil {
234+
if _, err = issues_model.CreateComment(ctx, options); err != nil {
235235
return fmt.Errorf("CreateChangeTargetBranchComment: %w", err)
236236
}
237237

services/pull/review.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"code.gitea.io/gitea/modules/notification"
2121
"code.gitea.io/gitea/modules/setting"
2222
"code.gitea.io/gitea/modules/util"
23-
issue_service "code.gitea.io/gitea/services/issue"
2423
)
2524

2625
var notEnoughLines = regexp.MustCompile(`fatal: file .* has only \d+ lines?`)
@@ -248,7 +247,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo
248247
return nil, err
249248
}
250249
}
251-
return issue_service.CreateComment(ctx, &issues_model.CreateCommentOptions{
250+
return issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
252251
Type: issues_model.CommentTypeCode,
253252
Doer: doer,
254253
Repo: repo,
@@ -340,7 +339,7 @@ func DismissApprovalReviews(ctx context.Context, doer *user_model.User, pull *is
340339
return err
341340
}
342341

343-
comment, err := issue_service.CreateComment(ctx, &issues_model.CreateCommentOptions{
342+
comment, err := issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
344343
Doer: doer,
345344
Content: "New commits pushed, approval review dismissed automatically according to repository settings",
346345
Type: issues_model.CommentTypeDismissReview,
@@ -411,7 +410,7 @@ func DismissReview(ctx context.Context, reviewID, repoID int64, message string,
411410
return nil, err
412411
}
413412

414-
comment, err = issue_service.CreateComment(ctx, &issues_model.CreateCommentOptions{
413+
comment, err = issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
415414
Doer: doer,
416415
Content: message,
417416
Type: issues_model.CommentTypeDismissReview,

0 commit comments

Comments
 (0)