Skip to content

Commit eb11ca6

Browse files
authored
Extract actions on new pull request from models to pulls service and move code.gitea.io/gitea/modules/pull to code.gitea.io/gitea/services/pull (#8218)
* extract actions on new pull request from models to pulls service * improve code * move code.gitea.io/gitea/modules/pull to code.gitea.io/gitea/services/pull * fix fmt * Rename pulls.go to pull.go
1 parent d958b9d commit eb11ca6

File tree

9 files changed

+56
-36
lines changed

9 files changed

+56
-36
lines changed

models/pull.go

-27
Original file line numberDiff line numberDiff line change
@@ -700,33 +700,6 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
700700
return fmt.Errorf("Commit: %v", err)
701701
}
702702

703-
if err = NotifyWatchers(&Action{
704-
ActUserID: pull.Poster.ID,
705-
ActUser: pull.Poster,
706-
OpType: ActionCreatePullRequest,
707-
Content: fmt.Sprintf("%d|%s", pull.Index, pull.Title),
708-
RepoID: repo.ID,
709-
Repo: repo,
710-
IsPrivate: repo.IsPrivate,
711-
}); err != nil {
712-
log.Error("NotifyWatchers: %v", err)
713-
}
714-
715-
pr.Issue = pull
716-
pull.PullRequest = pr
717-
mode, _ := AccessLevel(pull.Poster, repo)
718-
if err = PrepareWebhooks(repo, HookEventPullRequest, &api.PullRequestPayload{
719-
Action: api.HookIssueOpened,
720-
Index: pull.Index,
721-
PullRequest: pr.APIFormat(),
722-
Repository: repo.APIFormat(mode),
723-
Sender: pull.Poster.APIFormat(),
724-
}); err != nil {
725-
log.Error("PrepareWebhooks: %v", err)
726-
} else {
727-
go HookQueue.Add(repo.ID)
728-
}
729-
730703
return nil
731704
}
732705

routers/api/v1/repo/pull.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import (
1515
"code.gitea.io/gitea/modules/git"
1616
"code.gitea.io/gitea/modules/log"
1717
"code.gitea.io/gitea/modules/notification"
18-
"code.gitea.io/gitea/modules/pull"
19-
pull_service "code.gitea.io/gitea/modules/pull"
2018
api "code.gitea.io/gitea/modules/structs"
2119
"code.gitea.io/gitea/modules/timeutil"
2220
milestone_service "code.gitea.io/gitea/services/milestone"
21+
pull_service "code.gitea.io/gitea/services/pull"
2322
)
2423

2524
// ListPullRequests returns a list of all PRs
@@ -288,7 +287,7 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
288287
return
289288
}
290289

291-
if err := models.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil {
290+
if err := pull_service.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil {
292291
if models.IsErrUserDoesNotHaveAccessToRepo(err) {
293292
ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
294293
return
@@ -602,7 +601,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
602601
message += "\n\n" + form.MergeMessageField
603602
}
604603

605-
if err := pull.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
604+
if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
606605
if models.IsErrInvalidMergeStyle(err) {
607606
ctx.Status(405)
608607
return

routers/repo/pull.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import (
2121
"code.gitea.io/gitea/modules/git"
2222
"code.gitea.io/gitea/modules/log"
2323
"code.gitea.io/gitea/modules/notification"
24-
"code.gitea.io/gitea/modules/pull"
25-
pull_service "code.gitea.io/gitea/modules/pull"
2624
"code.gitea.io/gitea/modules/setting"
2725
"code.gitea.io/gitea/modules/util"
2826
"code.gitea.io/gitea/services/gitdiff"
27+
pull_service "code.gitea.io/gitea/services/pull"
2928

3029
"github.com/unknwon/com"
3130
)
@@ -676,7 +675,7 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
676675
return
677676
}
678677

679-
if err = pull.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
678+
if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
680679
if models.IsErrInvalidMergeStyle(err) {
681680
ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option"))
682681
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
@@ -789,7 +788,7 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
789788
// FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
790789
// instead of 500.
791790

792-
if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch, assigneeIDs); err != nil {
791+
if err := pull_service.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch, assigneeIDs); err != nil {
793792
if models.IsErrUserDoesNotHaveAccessToRepo(err) {
794793
ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
795794
return

routers/repo/pull_review.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"code.gitea.io/gitea/modules/context"
1313
"code.gitea.io/gitea/modules/log"
1414
"code.gitea.io/gitea/modules/notification"
15-
pull_service "code.gitea.io/gitea/modules/pull"
1615
comment_service "code.gitea.io/gitea/services/comments"
16+
pull_service "code.gitea.io/gitea/services/pull"
1717
)
1818

1919
// CreateCodeComment will create a code comment including an pending review if required
File renamed without changes.
File renamed without changes.
File renamed without changes.

services/pull/pull.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package pull
6+
7+
import (
8+
"fmt"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/log"
12+
api "code.gitea.io/gitea/modules/structs"
13+
)
14+
15+
// NewPullRequest creates new pull request with labels for repository.
16+
func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, patch []byte, assigneeIDs []int64) error {
17+
if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr, patch, assigneeIDs); err != nil {
18+
return err
19+
}
20+
21+
if err := models.NotifyWatchers(&models.Action{
22+
ActUserID: pull.Poster.ID,
23+
ActUser: pull.Poster,
24+
OpType: models.ActionCreatePullRequest,
25+
Content: fmt.Sprintf("%d|%s", pull.Index, pull.Title),
26+
RepoID: repo.ID,
27+
Repo: repo,
28+
IsPrivate: repo.IsPrivate,
29+
}); err != nil {
30+
log.Error("NotifyWatchers: %v", err)
31+
}
32+
33+
pr.Issue = pull
34+
pull.PullRequest = pr
35+
mode, _ := models.AccessLevel(pull.Poster, repo)
36+
if err := models.PrepareWebhooks(repo, models.HookEventPullRequest, &api.PullRequestPayload{
37+
Action: api.HookIssueOpened,
38+
Index: pull.Index,
39+
PullRequest: pr.APIFormat(),
40+
Repository: repo.APIFormat(mode),
41+
Sender: pull.Poster.APIFormat(),
42+
}); err != nil {
43+
log.Error("PrepareWebhooks: %v", err)
44+
} else {
45+
go models.HookQueue.Add(repo.ID)
46+
}
47+
48+
return nil
49+
}
File renamed without changes.

0 commit comments

Comments
 (0)