From 46fc36c3a63b5501ca154df885ae5446ebb66220 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 14 May 2014 10:55:36 -0400 Subject: [PATCH] =?UTF-8?q?Finish=20change=20issue=E2=80=99s=20milestone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/web.go | 1 + models/issue.go | 57 ++++++++++++++++++++++++++ routers/repo/issue.go | 86 +++++++++++++++++++++++++++++++++++++-- templates/issue/view.tmpl | 40 +++++++++++------- 4 files changed, 166 insertions(+), 18 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index a79384412d..5a0bd167e1 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -185,6 +185,7 @@ func runWeb(*cli.Context) { r.Post("/issues/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost) r.Post("/issues/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue) r.Post("/issues/:index/assignee", repo.UpdateAssignee) + r.Post("/issues/:index/milestone", repo.UpdateIssueMilestone) r.Get("/issues/milestones", repo.Milestones) r.Get("/issues/milestones/new", repo.NewMilestone) r.Post("/issues/milestones/new", bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost) diff --git a/models/issue.go b/models/issue.go index e708e54311..26380b6033 100644 --- a/models/issue.go +++ b/models/issue.go @@ -446,6 +446,18 @@ func NewMilestone(m *Milestone) (err error) { return sess.Commit() } +// GetMilestoneById returns the milestone by given ID. +func GetMilestoneById(id int64) (*Milestone, error) { + m := &Milestone{Id: id} + has, err := orm.Get(m) + if err != nil { + return nil, err + } else if !has { + return nil, ErrMilestoneNotExist + } + return m, nil +} + // GetMilestoneByIndex returns the milestone of given repository and index. func GetMilestoneByIndex(repoId, idx int64) (*Milestone, error) { m := &Milestone{RepoId: repoId, Index: idx} @@ -502,6 +514,51 @@ func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) { return sess.Commit() } +// ChangeMilestoneAssign changes assignment of milestone for issue. +func ChangeMilestoneAssign(oldMid, mid int64, isIssueClosed bool) (err error) { + sess := orm.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + + if oldMid > 0 { + m, err := GetMilestoneById(oldMid) + if err != nil { + return err + } + + m.NumIssues-- + if isIssueClosed { + m.NumClosedIssues-- + } + if m.NumIssues > 0 { + m.Completeness = m.NumClosedIssues * 100 / m.NumIssues + } else { + m.Completeness = 0 + } + if _, err = sess.Id(m.Id).Update(m); err != nil { + sess.Rollback() + return err + } + } + + m, err := GetMilestoneById(mid) + if err != nil { + return err + } + m.NumIssues++ + if isIssueClosed { + m.NumClosedIssues++ + } + m.Completeness = m.NumClosedIssues * 100 / m.NumIssues + if _, err = sess.Id(m.Id).Update(m); err != nil { + sess.Rollback() + return err + } + return sess.Commit() +} + // DeleteMilestone deletes a milestone. func DeleteMilestone(m *Milestone) (err error) { sess := orm.NewSession() diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 3e19724c38..2346539d0c 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -53,11 +53,17 @@ func Issues(ctx *middleware.Context) { filterMode = models.FM_MENTION } - mid, _ := base.StrTo(ctx.Query("milestone")).Int64() + midx, _ := base.StrTo(ctx.Query("milestone")).Int64() + mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, midx) + if err != nil { + ctx.Handle(500, "issue.Issues(GetMilestoneByIndex): %v", err) + return + } + page, _ := base.StrTo(ctx.Query("page")).Int() // Get issues. - issues, err := models.GetIssues(assigneeId, ctx.Repo.Repository.Id, posterId, mid, page, + issues, err := models.GetIssues(assigneeId, ctx.Repo.Repository.Id, posterId, mile.Id, page, isShowClosed, ctx.Query("labels"), ctx.Query("sortType")) if err != nil { ctx.Handle(500, "issue.Issues(GetIssues): %v", err) @@ -240,12 +246,37 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { return } - us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) + // Get assigned milestone. + if issue.MilestoneId > 0 { + ctx.Data["Milestone"], err = models.GetMilestoneById(issue.MilestoneId) + if err != nil { + if err == models.ErrMilestoneNotExist { + log.Warn("issue.ViewIssue(GetMilestoneById): %v", err) + } else { + ctx.Handle(500, "issue.ViewIssue(GetMilestoneById)", err) + return + } + } + } + + // Get all milestones. + ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, false) + if err != nil { + ctx.Handle(500, "issue.ViewIssue(GetMilestones.1): %v", err) + return + } + ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, true) + if err != nil { + ctx.Handle(500, "issue.ViewIssue(GetMilestones.2): %v", err) + return + } + + // Get all collaborators. + ctx.Data["Collaborators"], err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) if err != nil { ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err) return } - ctx.Data["Collaborators"] = us if ctx.IsSigned { // Update issue-user. @@ -331,6 +362,52 @@ func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat }) } +func UpdateIssueMilestone(ctx *middleware.Context) { + if !ctx.Repo.IsOwner { + ctx.Error(403) + return + } + + issueId, err := base.StrTo(ctx.Query("issue")).Int64() + if err != nil { + ctx.Error(404) + return + } + + issue, err := models.GetIssueById(issueId) + if err != nil { + if err == models.ErrIssueNotExist { + ctx.Handle(404, "issue.UpdateIssueMilestone(GetIssueById)", err) + } else { + ctx.Handle(500, "issue.UpdateIssueMilestone(GetIssueById)", err) + } + return + } + + oldMid := issue.MilestoneId + mid, _ := base.StrTo(ctx.Query("milestone")).Int64() + if oldMid == mid { + ctx.JSON(200, map[string]interface{}{ + "ok": true, + }) + return + } + + // Not check for invalid milestone id and give responsibility to owners. + issue.MilestoneId = mid + if err = models.ChangeMilestoneAssign(oldMid, mid, issue.IsClosed); err != nil { + ctx.Handle(500, "issue.UpdateIssueMilestone(ChangeMilestoneAssign)", err) + return + } else if err = models.UpdateIssue(issue); err != nil { + ctx.Handle(500, "issue.UpdateIssueMilestone(UpdateIssue)", err) + return + } + + ctx.JSON(200, map[string]interface{}{ + "ok": true, + }) +} + func UpdateAssignee(ctx *middleware.Context) { if !ctx.Repo.IsOwner { ctx.Error(403) @@ -580,6 +657,7 @@ func UpdateMilestone(ctx *middleware.Context, params martini.Params) { } case "close": if !mile.IsClosed { + mile.ClosedDate = time.Now() if err = models.ChangeMilestoneStatus(mile, true); err != nil { ctx.Handle(500, "issue.UpdateMilestone(ChangeMilestoneStatus)", err) return diff --git a/templates/issue/view.tmpl b/templates/issue/view.tmpl index 653f21088f..22c0e2d8a8 100644 --- a/templates/issue/view.tmpl +++ b/templates/issue/view.tmpl @@ -100,7 +100,7 @@
-
+

Milestone

-

 

-

Milestone name

+ {{if .Milestone}} +

 

+

{{.Milestone.Name}}

+ {{else}}

No milestone

+ {{end}}
+
{{if .IsRepositoryOwner}}