From a6b087a8e338bd7cce4f39ce343f8070ae57ade3 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 18 Sep 2019 08:46:02 +0800 Subject: [PATCH 1/5] extract actions on new pull request from models to pulls service --- models/pull.go | 27 -------------------- routers/api/v1/repo/pull.go | 3 ++- routers/repo/pull.go | 3 ++- services/pulls/pulls.go | 49 +++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 services/pulls/pulls.go diff --git a/models/pull.go b/models/pull.go index 2f7218f4157e3..60ccb144f510a 100644 --- a/models/pull.go +++ b/models/pull.go @@ -700,33 +700,6 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str return fmt.Errorf("Commit: %v", err) } - if err = NotifyWatchers(&Action{ - ActUserID: pull.Poster.ID, - ActUser: pull.Poster, - OpType: ActionCreatePullRequest, - Content: fmt.Sprintf("%d|%s", pull.Index, pull.Title), - RepoID: repo.ID, - Repo: repo, - IsPrivate: repo.IsPrivate, - }); err != nil { - log.Error("NotifyWatchers: %v", err) - } - - pr.Issue = pull - pull.PullRequest = pr - mode, _ := AccessLevel(pull.Poster, repo) - if err = PrepareWebhooks(repo, HookEventPullRequest, &api.PullRequestPayload{ - Action: api.HookIssueOpened, - Index: pull.Index, - PullRequest: pr.APIFormat(), - Repository: repo.APIFormat(mode), - Sender: pull.Poster.APIFormat(), - }); err != nil { - log.Error("PrepareWebhooks: %v", err) - } else { - go HookQueue.Add(repo.ID) - } - return nil } diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 0f9eab2f5029d..7fbe391f746f9 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -20,6 +20,7 @@ import ( api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" milestone_service "code.gitea.io/gitea/services/milestone" + pull_service "code.gitea.io/gitea/services/pulls" ) // ListPullRequests returns a list of all PRs @@ -288,7 +289,7 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption return } - if err := models.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil { + if err := pull_service.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil { if models.IsErrUserDoesNotHaveAccessToRepo(err) { ctx.Error(400, "UserDoesNotHaveAccessToRepo", err) return diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 180d592e3dbf8..1ee7623d751ee 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -26,6 +26,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/gitdiff" + pull_service "code.gitea.io/gitea/services/pulls" "github.com/unknwon/com" ) @@ -789,7 +790,7 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt // instead of 500. - if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch, assigneeIDs); err != nil { + if err := pull_service.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch, assigneeIDs); err != nil { if models.IsErrUserDoesNotHaveAccessToRepo(err) { ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error()) return diff --git a/services/pulls/pulls.go b/services/pulls/pulls.go new file mode 100644 index 0000000000000..1f22b6103fb05 --- /dev/null +++ b/services/pulls/pulls.go @@ -0,0 +1,49 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package pulls + +import ( + "fmt" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" + api "code.gitea.io/gitea/modules/structs" +) + +// NewPullRequest creates new pull request with labels for repository. +func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, patch []byte, assigneeIDs []int64) (err error) { + if err = models.NewPullRequest(repo, pull, labelIDs, uuids, pr, patch, assigneeIDs); err != nil { + return + } + + if err = models.NotifyWatchers(&models.Action{ + ActUserID: pull.Poster.ID, + ActUser: pull.Poster, + OpType: models.ActionCreatePullRequest, + Content: fmt.Sprintf("%d|%s", pull.Index, pull.Title), + RepoID: repo.ID, + Repo: repo, + IsPrivate: repo.IsPrivate, + }); err != nil { + log.Error("NotifyWatchers: %v", err) + } + + pr.Issue = pull + pull.PullRequest = pr + mode, _ := models.AccessLevel(pull.Poster, repo) + if err = models.PrepareWebhooks(repo, models.HookEventPullRequest, &api.PullRequestPayload{ + Action: api.HookIssueOpened, + Index: pull.Index, + PullRequest: pr.APIFormat(), + Repository: repo.APIFormat(mode), + Sender: pull.Poster.APIFormat(), + }); err != nil { + log.Error("PrepareWebhooks: %v", err) + } else { + go models.HookQueue.Add(repo.ID) + } + + return nil +} \ No newline at end of file From 7dfe9199cf5effbde9bee400de4969cf480f6678 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Sep 2019 09:11:57 +0800 Subject: [PATCH 2/5] improve code --- services/pulls/pulls.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/services/pulls/pulls.go b/services/pulls/pulls.go index 1f22b6103fb05..c3d196d4199d2 100644 --- a/services/pulls/pulls.go +++ b/services/pulls/pulls.go @@ -13,12 +13,12 @@ import ( ) // NewPullRequest creates new pull request with labels for repository. -func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, patch []byte, assigneeIDs []int64) (err error) { - if err = models.NewPullRequest(repo, pull, labelIDs, uuids, pr, patch, assigneeIDs); err != nil { - return +func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, patch []byte, assigneeIDs []int64) error { + if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr, patch, assigneeIDs); err != nil { + return err } - if err = models.NotifyWatchers(&models.Action{ + if err := models.NotifyWatchers(&models.Action{ ActUserID: pull.Poster.ID, ActUser: pull.Poster, OpType: models.ActionCreatePullRequest, @@ -33,7 +33,7 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 pr.Issue = pull pull.PullRequest = pr mode, _ := models.AccessLevel(pull.Poster, repo) - if err = models.PrepareWebhooks(repo, models.HookEventPullRequest, &api.PullRequestPayload{ + if err := models.PrepareWebhooks(repo, models.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueOpened, Index: pull.Index, PullRequest: pr.APIFormat(), @@ -46,4 +46,4 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 } return nil -} \ No newline at end of file +} From 38f9deff94c4107391bd9c0304ff81252ed81843 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Sep 2019 16:54:44 +0800 Subject: [PATCH 3/5] move code.gitea.io/gitea/modules/pull to code.gitea.io/gitea/services/pull --- routers/api/v1/repo/pull.go | 6 ++---- routers/repo/pull.go | 6 ++---- routers/repo/pull_review.go | 2 +- {modules => services}/pull/commit_status.go | 0 {modules => services}/pull/lfs.go | 0 {modules => services}/pull/merge.go | 0 services/{pulls => pull}/pulls.go | 2 +- {modules => services}/pull/review.go | 0 8 files changed, 6 insertions(+), 10 deletions(-) rename {modules => services}/pull/commit_status.go (100%) rename {modules => services}/pull/lfs.go (100%) rename {modules => services}/pull/merge.go (100%) rename services/{pulls => pull}/pulls.go (99%) rename {modules => services}/pull/review.go (100%) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 7fbe391f746f9..978c8a3f1f491 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -15,12 +15,10 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" - "code.gitea.io/gitea/modules/pull" - pull_service "code.gitea.io/gitea/modules/pull" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" milestone_service "code.gitea.io/gitea/services/milestone" - pull_service "code.gitea.io/gitea/services/pulls" + pull_service "code.gitea.io/gitea/services/pull" ) // ListPullRequests returns a list of all PRs @@ -603,7 +601,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) { message += "\n\n" + form.MergeMessageField } - if err := pull.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { + if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { if models.IsErrInvalidMergeStyle(err) { ctx.Status(405) return diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 1ee7623d751ee..72d2ffcaa7d66 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -21,12 +21,10 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" - "code.gitea.io/gitea/modules/pull" - pull_service "code.gitea.io/gitea/modules/pull" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/gitdiff" - pull_service "code.gitea.io/gitea/services/pulls" + pull_service "code.gitea.io/gitea/services/pull" "github.com/unknwon/com" ) @@ -677,7 +675,7 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) { return } - if err = pull.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { + if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { if models.IsErrInvalidMergeStyle(err) { ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option")) ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index)) diff --git a/routers/repo/pull_review.go b/routers/repo/pull_review.go index d4e3a3326a1b3..b4de120833095 100644 --- a/routers/repo/pull_review.go +++ b/routers/repo/pull_review.go @@ -12,7 +12,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" - pull_service "code.gitea.io/gitea/modules/pull" + pull_service "code.gitea.io/gitea/services/pull" comment_service "code.gitea.io/gitea/services/comments" ) diff --git a/modules/pull/commit_status.go b/services/pull/commit_status.go similarity index 100% rename from modules/pull/commit_status.go rename to services/pull/commit_status.go diff --git a/modules/pull/lfs.go b/services/pull/lfs.go similarity index 100% rename from modules/pull/lfs.go rename to services/pull/lfs.go diff --git a/modules/pull/merge.go b/services/pull/merge.go similarity index 100% rename from modules/pull/merge.go rename to services/pull/merge.go diff --git a/services/pulls/pulls.go b/services/pull/pulls.go similarity index 99% rename from services/pulls/pulls.go rename to services/pull/pulls.go index c3d196d4199d2..0dbd8fcd1a148 100644 --- a/services/pulls/pulls.go +++ b/services/pull/pulls.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package pulls +package pull import ( "fmt" diff --git a/modules/pull/review.go b/services/pull/review.go similarity index 100% rename from modules/pull/review.go rename to services/pull/review.go From 1b5e0221309083df2ccd750e6b6de38cd563c3d4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Sep 2019 16:58:00 +0800 Subject: [PATCH 4/5] fix fmt --- routers/repo/pull_review.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/pull_review.go b/routers/repo/pull_review.go index b4de120833095..5eb0dfe9a73d8 100644 --- a/routers/repo/pull_review.go +++ b/routers/repo/pull_review.go @@ -12,8 +12,8 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" - pull_service "code.gitea.io/gitea/services/pull" comment_service "code.gitea.io/gitea/services/comments" + pull_service "code.gitea.io/gitea/services/pull" ) // CreateCodeComment will create a code comment including an pending review if required From 496eefc4cf0198265125ca7f5f238e06ef331178 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Wed, 25 Sep 2019 11:59:07 +0300 Subject: [PATCH 5/5] Rename pulls.go to pull.go --- services/pull/{pulls.go => pull.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename services/pull/{pulls.go => pull.go} (100%) diff --git a/services/pull/pulls.go b/services/pull/pull.go similarity index 100% rename from services/pull/pulls.go rename to services/pull/pull.go