diff --git a/cmd/web.go b/cmd/web.go index a7938441..5a0bd167 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 e708e543..7dd69267 100644 --- a/models/issue.go +++ b/models/issue.go @@ -167,6 +167,8 @@ type IssueUser struct { Uid int64 // User ID. IssueId int64 RepoId int64 + MilestoneId int64 + Labels string `xorm:"TEXT"` IsRead bool IsAssigned bool IsMentioned bool @@ -446,6 +448,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 +516,53 @@ 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 + } + } + + if mid > 0 { + 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/models/repo.go b/models/repo.go index 4382bd7d..0594c6c6 100644 --- a/models/repo.go +++ b/models/repo.go @@ -676,10 +676,6 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) { sess.Rollback() return err } - if _, err = sess.Delete(&Issue{RepoId: repoId}); err != nil { - sess.Rollback() - return err - } if _, err = sess.Delete(&IssueUser{RepoId: repoId}); err != nil { sess.Rollback() return err @@ -688,6 +684,28 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) { sess.Rollback() return err } + if _, err = sess.Delete(&Release{RepoId: repoId}); err != nil { + sess.Rollback() + return err + } + + // Delete comments. + if err = orm.Iterate(&Issue{RepoId: repoId}, func(idx int, bean interface{}) error { + issue := bean.(*Issue) + if _, err = sess.Delete(&Comment{IssueId: issue.Id}); err != nil { + sess.Rollback() + return err + } + return nil + }); err != nil { + sess.Rollback() + return err + } + + if _, err = sess.Delete(&Issue{RepoId: repoId}); err != nil { + sess.Rollback() + return err + } rawSql := "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" if _, err = sess.Exec(rawSql, userId); err != nil { diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 3e19724c..db0eff9c 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -53,7 +53,18 @@ func Issues(ctx *middleware.Context) { filterMode = models.FM_MENTION } - mid, _ := base.StrTo(ctx.Query("milestone")).Int64() + var mid int64 + midx, _ := base.StrTo(ctx.Query("milestone")).Int64() + if midx > 0 { + mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, midx) + if err != nil { + ctx.Handle(500, "issue.Issues(GetMilestoneByIndex): %v", err) + return + } + mid = mile.Id + } + fmt.Println(mid) + page, _ := base.StrTo(ctx.Query("page")).Int() // Get issues. @@ -114,6 +125,19 @@ func CreateIssue(ctx *middleware.Context, params martini.Params) { ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = false + var err error + // 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 + } + us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) if err != nil { ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err) @@ -128,6 +152,19 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C ctx.Data["IsRepoToolbarIssues"] = true ctx.Data["IsRepoToolbarIssuesList"] = false + var err error + // 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 + } + us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/")) if err != nil { ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err) @@ -240,12 +277,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 +393,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 +688,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/create.tmpl b/templates/issue/create.tmpl index 4b43116c..34cecc78 100644 --- a/templates/issue/create.tmpl +++ b/templates/issue/create.tmpl @@ -48,25 +48,33 @@
Nothing to show
+ {{else}}Milestone name
-due to 3 days later
-Milestone name
-due to 3 days later
+ {{range .OpenMilestones}} +{{.Name}}
+Nothing to show
+ {{else}}Milestone name
-closed 3 days ago
+ {{range .ClosedMilestones}} +{{.Name}}
+Closed {{TimeSince .ClosedDate}}
Edit {{if .IsClosed}} diff --git a/templates/issue/view.tmpl b/templates/issue/view.tmpl index d252bd7f..18ec5faf 100644 --- a/templates/issue/view.tmpl +++ b/templates/issue/view.tmpl @@ -100,7 +100,7 @@