Skip to content

Commit c66c9da

Browse files
authored
Move issue change status from models to service (#8691)
1 parent 495d5e4 commit c66c9da

File tree

6 files changed

+68
-54
lines changed

6 files changed

+68
-54
lines changed

models/issue.go

-39
Original file line numberDiff line numberDiff line change
@@ -674,45 +674,6 @@ func (issue *Issue) ChangeStatus(doer *User, isClosed bool) (err error) {
674674
if err = sess.Commit(); err != nil {
675675
return fmt.Errorf("Commit: %v", err)
676676
}
677-
sess.Close()
678-
679-
mode, _ := AccessLevel(issue.Poster, issue.Repo)
680-
if issue.IsPull {
681-
if err = issue.loadPullRequest(sess); err != nil {
682-
return err
683-
}
684-
// Merge pull request calls issue.changeStatus so we need to handle separately.
685-
apiPullRequest := &api.PullRequestPayload{
686-
Index: issue.Index,
687-
PullRequest: issue.PullRequest.APIFormat(),
688-
Repository: issue.Repo.APIFormat(mode),
689-
Sender: doer.APIFormat(),
690-
}
691-
if isClosed {
692-
apiPullRequest.Action = api.HookIssueClosed
693-
} else {
694-
apiPullRequest.Action = api.HookIssueReOpened
695-
}
696-
err = PrepareWebhooks(issue.Repo, HookEventPullRequest, apiPullRequest)
697-
} else {
698-
apiIssue := &api.IssuePayload{
699-
Index: issue.Index,
700-
Issue: issue.APIFormat(),
701-
Repository: issue.Repo.APIFormat(mode),
702-
Sender: doer.APIFormat(),
703-
}
704-
if isClosed {
705-
apiIssue.Action = api.HookIssueClosed
706-
} else {
707-
apiIssue.Action = api.HookIssueReOpened
708-
}
709-
err = PrepareWebhooks(issue.Repo, HookEventIssues, apiIssue)
710-
}
711-
if err != nil {
712-
log.Error("PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err)
713-
} else {
714-
go HookQueue.Add(issue.Repo.ID)
715-
}
716677

717678
return nil
718679
}

modules/notification/webhook/webhook.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *mo
157157
}
158158
} else {
159159
mode, _ := models.AccessLevelUnit(doer, issue.Repo, models.UnitTypeIssues)
160-
161160
apiIssue := &api.IssuePayload{
162161
Index: issue.Index,
163162
Issue: issue.APIFormat(),
@@ -221,3 +220,45 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(doer *models.User, issue *model
221220
go models.HookQueue.Add(issue.RepoID)
222221
}
223222
}
223+
224+
func (m *webhookNotifier) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, isClosed bool) {
225+
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
226+
var err error
227+
if issue.IsPull {
228+
if err = issue.LoadPullRequest(); err != nil {
229+
log.Error("LoadPullRequest: %v", err)
230+
return
231+
}
232+
// Merge pull request calls issue.changeStatus so we need to handle separately.
233+
apiPullRequest := &api.PullRequestPayload{
234+
Index: issue.Index,
235+
PullRequest: issue.PullRequest.APIFormat(),
236+
Repository: issue.Repo.APIFormat(mode),
237+
Sender: doer.APIFormat(),
238+
}
239+
if isClosed {
240+
apiPullRequest.Action = api.HookIssueClosed
241+
} else {
242+
apiPullRequest.Action = api.HookIssueReOpened
243+
}
244+
err = models.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, apiPullRequest)
245+
} else {
246+
apiIssue := &api.IssuePayload{
247+
Index: issue.Index,
248+
Issue: issue.APIFormat(),
249+
Repository: issue.Repo.APIFormat(mode),
250+
Sender: doer.APIFormat(),
251+
}
252+
if isClosed {
253+
apiIssue.Action = api.HookIssueClosed
254+
} else {
255+
apiIssue.Action = api.HookIssueReOpened
256+
}
257+
err = models.PrepareWebhooks(issue.Repo, models.HookEventIssues, apiIssue)
258+
}
259+
if err != nil {
260+
log.Error("PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err)
261+
} else {
262+
go models.HookQueue.Add(issue.Repo.ID)
263+
}
264+
}

routers/api/v1/repo/issue.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
254254
notification.NotifyNewIssue(issue)
255255

256256
if form.Closed {
257-
if err := issue.ChangeStatus(ctx.User, true); err != nil {
257+
if err := issue_service.ChangeStatus(issue, ctx.User, true); err != nil {
258258
if models.IsErrDependenciesLeft(err) {
259259
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
260260
return
@@ -381,16 +381,14 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
381381
return
382382
}
383383
if form.State != nil {
384-
if err = issue.ChangeStatus(ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
384+
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
385385
if models.IsErrDependenciesLeft(err) {
386386
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
387387
return
388388
}
389389
ctx.Error(500, "ChangeStatus", err)
390390
return
391391
}
392-
393-
notification.NotifyIssueChangeStatus(ctx.User, issue, api.StateClosed == api.StateType(*form.State))
394392
}
395393

396394
// Refetch from database to assign some automatic values

routers/api/v1/repo/pull.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -448,16 +448,14 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
448448
return
449449
}
450450
if form.State != nil {
451-
if err = issue.ChangeStatus(ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
451+
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
452452
if models.IsErrDependenciesLeft(err) {
453453
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
454454
return
455455
}
456456
ctx.Error(500, "ChangeStatus", err)
457457
return
458458
}
459-
460-
notification.NotifyIssueChangeStatus(ctx.User, issue, api.StateClosed == api.StateType(*form.State))
461459
}
462460

463461
// Refetch from database

routers/repo/issue.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ func UpdateIssueStatus(ctx *context.Context) {
11851185
}
11861186
for _, issue := range issues {
11871187
if issue.IsClosed != isClosed {
1188-
if err := issue.ChangeStatus(ctx.User, isClosed); err != nil {
1188+
if err := issue_service.ChangeStatus(issue, ctx.User, isClosed); err != nil {
11891189
if models.IsErrDependenciesLeft(err) {
11901190
ctx.JSON(http.StatusPreconditionFailed, map[string]interface{}{
11911191
"error": "cannot close this issue because it still has open dependencies",
@@ -1195,8 +1195,6 @@ func UpdateIssueStatus(ctx *context.Context) {
11951195
ctx.ServerError("ChangeStatus", err)
11961196
return
11971197
}
1198-
1199-
notification.NotifyIssueChangeStatus(ctx.User, issue, isClosed)
12001198
}
12011199
}
12021200
ctx.JSON(200, map[string]interface{}{
@@ -1286,7 +1284,7 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
12861284
ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index))
12871285
} else {
12881286
isClosed := form.Status == "close"
1289-
if err := issue.ChangeStatus(ctx.User, isClosed); err != nil {
1287+
if err := issue_service.ChangeStatus(issue, ctx.User, isClosed); err != nil {
12901288
log.Error("ChangeStatus: %v", err)
12911289

12921290
if models.IsErrDependenciesLeft(err) {
@@ -1300,15 +1298,12 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
13001298
return
13011299
}
13021300
} else {
1303-
13041301
if err := stopTimerIfAvailable(ctx.User, issue); err != nil {
13051302
ctx.ServerError("CreateOrStopIssueStopwatch", err)
13061303
return
13071304
}
13081305

13091306
log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed)
1310-
1311-
notification.NotifyIssueChangeStatus(ctx.User, issue, isClosed)
13121307
}
13131308
}
13141309
}

services/issue/status.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 issue
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/notification"
10+
)
11+
12+
// ChangeStatus changes issue status to open or closed.
13+
func ChangeStatus(issue *models.Issue, doer *models.User, isClosed bool) (err error) {
14+
err = issue.ChangeStatus(doer, isClosed)
15+
if err != nil {
16+
return
17+
}
18+
19+
notification.NotifyIssueChangeStatus(doer, issue, isClosed)
20+
return nil
21+
}

0 commit comments

Comments
 (0)