Skip to content

Commit a6b087a

Browse files
committed
extract actions on new pull request from models to pulls service
1 parent c05b89a commit a6b087a

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

models/pull.go

Lines changed: 0 additions & 27 deletions
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
api "code.gitea.io/gitea/modules/structs"
2121
"code.gitea.io/gitea/modules/timeutil"
2222
milestone_service "code.gitea.io/gitea/services/milestone"
23+
pull_service "code.gitea.io/gitea/services/pulls"
2324
)
2425

2526
// ListPullRequests returns a list of all PRs
@@ -288,7 +289,7 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
288289
return
289290
}
290291

291-
if err := models.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil {
292+
if err := pull_service.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil {
292293
if models.IsErrUserDoesNotHaveAccessToRepo(err) {
293294
ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
294295
return

routers/repo/pull.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"code.gitea.io/gitea/modules/setting"
2727
"code.gitea.io/gitea/modules/util"
2828
"code.gitea.io/gitea/services/gitdiff"
29+
pull_service "code.gitea.io/gitea/services/pulls"
2930

3031
"github.com/unknwon/com"
3132
)
@@ -789,7 +790,7 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
789790
// FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
790791
// instead of 500.
791792

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

services/pulls/pulls.go

Lines changed: 49 additions & 0 deletions
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 pulls
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) (err error) {
17+
if err = models.NewPullRequest(repo, pull, labelIDs, uuids, pr, patch, assigneeIDs); err != nil {
18+
return
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+
}

0 commit comments

Comments
 (0)