From f694bb45d79dcc093bc6332eabb3af063bc6b088 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Oct 2019 16:36:25 +0800 Subject: [PATCH] Move issue change content from models to service (#8711) * Move issue change content from models to service * fix lint --- models/issue.go | 43 +------------------------ modules/notification/webhook/webhook.go | 38 ++++++++++++++++++++++ routers/repo/issue.go | 2 +- services/issue/content.go | 23 +++++++++++++ 4 files changed, 63 insertions(+), 43 deletions(-) create mode 100644 services/issue/content.go diff --git a/models/issue.go b/models/issue.go index b4bd190aa..17205cc2f 100644 --- a/models/issue.go +++ b/models/issue.go @@ -750,7 +750,6 @@ func (issue *Issue) UpdateAttachments(uuids []string) (err error) { // ChangeContent changes issue content, as the given user. func (issue *Issue) ChangeContent(doer *User, content string) (err error) { - oldContent := issue.Content issue.Content = content sess := x.NewSession() @@ -769,47 +768,7 @@ func (issue *Issue) ChangeContent(doer *User, content string) (err error) { return err } - if err = sess.Commit(); err != nil { - return err - } - sess.Close() - - mode, _ := AccessLevel(issue.Poster, issue.Repo) - if issue.IsPull { - issue.PullRequest.Issue = issue - err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{ - Action: api.HookIssueEdited, - Index: issue.Index, - Changes: &api.ChangesPayload{ - Body: &api.ChangesFromPayload{ - From: oldContent, - }, - }, - PullRequest: issue.PullRequest.APIFormat(), - Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), - }) - } else { - err = PrepareWebhooks(issue.Repo, HookEventIssues, &api.IssuePayload{ - Action: api.HookIssueEdited, - Index: issue.Index, - Changes: &api.ChangesPayload{ - Body: &api.ChangesFromPayload{ - From: oldContent, - }, - }, - Issue: issue.APIFormat(), - Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), - }) - } - if err != nil { - log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err) - } else { - go HookQueue.Add(issue.RepoID) - } - - return nil + return sess.Commit() } // GetTasks returns the amount of tasks in the issues content diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index bd7c8b29d..a2af152ed 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -277,3 +277,41 @@ func (m *webhookNotifier) NotifyNewIssue(issue *models.Issue) { go models.HookQueue.Add(issue.RepoID) } } + +func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) { + mode, _ := models.AccessLevel(issue.Poster, issue.Repo) + var err error + if issue.IsPull { + issue.PullRequest.Issue = issue + err = models.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{ + Action: api.HookIssueEdited, + Index: issue.Index, + Changes: &api.ChangesPayload{ + Body: &api.ChangesFromPayload{ + From: oldContent, + }, + }, + PullRequest: issue.PullRequest.APIFormat(), + Repository: issue.Repo.APIFormat(mode), + Sender: doer.APIFormat(), + }) + } else { + err = models.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ + Action: api.HookIssueEdited, + Index: issue.Index, + Changes: &api.ChangesPayload{ + Body: &api.ChangesFromPayload{ + From: oldContent, + }, + }, + Issue: issue.APIFormat(), + Repository: issue.Repo.APIFormat(mode), + Sender: doer.APIFormat(), + }) + } + if err != nil { + log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err) + } else { + go models.HookQueue.Add(issue.RepoID) + } +} diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 12ff0a054..ac405a1c2 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -1066,7 +1066,7 @@ func UpdateIssueContent(ctx *context.Context) { } content := ctx.Query("content") - if err := issue.ChangeContent(ctx.User, content); err != nil { + if err := issue_service.ChangeContent(issue, ctx.User, content); err != nil { ctx.ServerError("ChangeContent", err) return } diff --git a/services/issue/content.go b/services/issue/content.go new file mode 100644 index 000000000..1081e30b5 --- /dev/null +++ b/services/issue/content.go @@ -0,0 +1,23 @@ +// 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 issue + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/notification" +) + +// ChangeContent changes issue content, as the given user. +func ChangeContent(issue *models.Issue, doer *models.User, content string) (err error) { + oldContent := issue.Content + + if err := issue.ChangeContent(doer, content); err != nil { + return err + } + + notification.NotifyIssueChangeContent(doer, issue, oldContent) + + return nil +}