Skip to content

Commit

Permalink
move some milestone functions to a standalone package (#8213)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Sep 18, 2019
1 parent 99f3ee3 commit 2945473
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 44 deletions.
41 changes: 0 additions & 41 deletions models/issue_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package models
import (
"fmt"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
Expand Down Expand Up @@ -394,46 +393,6 @@ func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err
if err = sess.Commit(); err != nil {
return fmt.Errorf("Commit: %v", err)
}

var hookAction api.HookIssueAction
if issue.MilestoneID > 0 {
hookAction = api.HookIssueMilestoned
} else {
hookAction = api.HookIssueDemilestoned
}

if err = issue.LoadAttributes(); err != nil {
return err
}

mode, _ := AccessLevel(doer, issue.Repo)
if issue.IsPull {
err = issue.PullRequest.LoadIssue()
if err != nil {
log.Error("LoadIssue: %v", err)
return
}
err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
Action: hookAction,
Index: issue.Index,
PullRequest: issue.PullRequest.APIFormat(),
Repository: issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
})
} else {
err = PrepareWebhooks(issue.Repo, HookEventIssues, &api.IssuePayload{
Action: hookAction,
Index: issue.Index,
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
}

Expand Down
3 changes: 2 additions & 1 deletion routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
milestone_service "code.gitea.io/gitea/services/milestone"
)

// ListIssues list the issues of a repository
Expand Down Expand Up @@ -345,7 +346,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
issue.MilestoneID != *form.Milestone {
oldMilestoneID := issue.MilestoneID
issue.MilestoneID = *form.Milestone
if err = models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
if err = milestone_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
ctx.Error(500, "ChangeMilestoneAssign", err)
return
}
Expand Down
3 changes: 2 additions & 1 deletion routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"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"
)

// ListPullRequests returns a list of all PRs
Expand Down Expand Up @@ -402,7 +403,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
issue.MilestoneID != form.Milestone {
oldMilestoneID := issue.MilestoneID
issue.MilestoneID = form.Milestone
if err = models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
if err = milestone_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
ctx.Error(500, "ChangeMilestoneAssign", err)
return
}
Expand Down
3 changes: 2 additions & 1 deletion routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
milestone_service "code.gitea.io/gitea/services/milestone"

"github.com/unknwon/com"
)
Expand Down Expand Up @@ -1086,7 +1087,7 @@ func UpdateIssueMilestone(ctx *context.Context) {
continue
}
issue.MilestoneID = milestoneID
if err := models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
if err := milestone_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
ctx.ServerError("ChangeMilestoneAssign", err)
return
}
Expand Down
59 changes: 59 additions & 0 deletions services/milestone/milestone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 models

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"
)

// ChangeMilestoneAssign changes assignment of milestone for issue.
func ChangeMilestoneAssign(issue *models.Issue, doer *models.User, oldMilestoneID int64) (err error) {
if err = models.ChangeMilestoneAssign(issue, doer, oldMilestoneID); err != nil {
return
}

var hookAction api.HookIssueAction
if issue.MilestoneID > 0 {
hookAction = api.HookIssueMilestoned
} else {
hookAction = api.HookIssueDemilestoned
}

if err = issue.LoadAttributes(); err != nil {
return err
}

mode, _ := models.AccessLevel(doer, issue.Repo)
if issue.IsPull {
err = issue.PullRequest.LoadIssue()
if err != nil {
log.Error("LoadIssue: %v", err)
return
}
err = models.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
Action: hookAction,
Index: issue.Index,
PullRequest: issue.PullRequest.APIFormat(),
Repository: issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
})
} else {
err = models.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
Action: hookAction,
Index: issue.Index,
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)
}
return nil
}

0 comments on commit 2945473

Please sign in to comment.