From 05885364473e8efe841ad527b588178f277f27d6 Mon Sep 17 00:00:00 2001 From: Konrad Langenberg Date: Sat, 26 Aug 2017 18:58:35 +0200 Subject: [PATCH] Init: * Added models * Added UI * Added Routes Signed-off-by: kolaente --- models/issue_dependency_add.go | 79 +++++++++++++++++++ routers/repo/issue_dependency_add.go | 38 +++++++++ routers/routes/routes.go | 8 +- .../repo/issue/view_content/sidebar.tmpl | 37 +++++++++ 4 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 models/issue_dependency_add.go create mode 100644 routers/repo/issue_dependency_add.go diff --git a/models/issue_dependency_add.go b/models/issue_dependency_add.go new file mode 100644 index 000000000..04db222ee --- /dev/null +++ b/models/issue_dependency_add.go @@ -0,0 +1,79 @@ +// Copyright 2017 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 ( + "time" +) + +// IssueDependency is connection request for receiving issue notification. +type IssueDependency struct { + ID int64 `xorm:"pk autoincr"` + UserID int64 `xorm:"UNIQUE(watch) NOT NULL"` + IssueID int64 `xorm:"UNIQUE(watch) NOT NULL"` + DependencyID int64 `xorm:"UNIQUE(watch) NOT NULL"` + Created time.Time `xorm:"-"` + CreatedUnix int64 `xorm:"NOT NULL"` + Updated time.Time `xorm:"-"` + UpdatedUnix int64 `xorm:"NOT NULL"` +} + +// BeforeInsert is invoked from XORM before inserting an object of this type. +func (iw *IssueDependency) BeforeInsert() { + var ( + t = time.Now() + u = t.Unix() + ) + iw.Created = t + iw.CreatedUnix = u + iw.Updated = t + iw.UpdatedUnix = u +} + +// BeforeUpdate is invoked from XORM before updating an object of this type. +func (iw *IssueDependency) BeforeUpdate() { + var ( + t = time.Now() + u = t.Unix() + ) + iw.Updated = t + iw.UpdatedUnix = u +} + +// CreateOrUpdateIssueDependency sets or updates a dependency for an issue +func CreateOrUpdateIssueDependency(userID, issueID int64, dep int64) error { + id, exists, err := getIssueWatch(x, userID, issueID) + if err != nil { + return err + } + + if !exists { + id = &IssueWatch{ + UserID: userID, + IssueID: issueID, + IsWatching: isWatching, + } + + if _, err := x.Insert(iw); err != nil { + return err + } + } else { + iw.IsWatching = isWatching + + if _, err := x.Id(iw.ID).Cols("is_watching", "updated_unix").Update(iw); err != nil { + return err + } + } + return nil +} + +// +func getIssueDep(e Engine, issueID int64) (Dependencies []*IssueDependency, err error) { + id = new(IssueDependency) + err = e. + Where("issue_id = ?", issueID). + Find(&Dependencies) + return +} diff --git a/routers/repo/issue_dependency_add.go b/routers/repo/issue_dependency_add.go new file mode 100644 index 000000000..384d04aca --- /dev/null +++ b/routers/repo/issue_dependency_add.go @@ -0,0 +1,38 @@ +// Copyright 2017 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 repo + +import ( + "fmt" + "net/http" + "strconv" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" +) + +// IssueWatch sets issue watching +func AddDependency(c *context.Context) { + dep, err := strconv.ParseInt(c.Req.PostForm.Get("newDependency"), 10, 64) + if err != nil { + c.Handle(http.StatusInternalServerError, "issue ID is not int", err) + return + } + + issueIndex := c.ParamsInt64("index") + issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, issueIndex) + if err != nil { + c.Handle(http.StatusInternalServerError, "GetIssueByIndex", err) + return + } + + if err := models.CreateOrUpdateIssueDependency(c.User.ID, issue.ID, dep); err != nil { + c.Handle(http.StatusInternalServerError, "CreateOrUpdateIssueWatch", err) + return + } + + url := fmt.Sprintf("%s/issues/%d", c.Repo.RepoLink, issueIndex) + c.Redirect(url, http.StatusSeeOther) +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index a0684c847..888ea2c92 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -485,6 +485,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/content", repo.UpdateIssueContent) m.Post("/watch", repo.IssueWatch) m.Combo("/comments").Post(bindIgnErr(auth.CreateCommentForm{}), repo.NewComment) + m.Post("/addDependency", repo.AddDependency) m.Group("/times", func() { m.Post("/add", bindIgnErr(auth.AddTimeManuallyForm{}), repo.AddTimeManually) m.Group("/stopwatch", func() { @@ -611,7 +612,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/*", repo.WikiRaw) }, repo.MustEnableWiki, context.CheckUnit(models.UnitTypeWiki), context.CheckUnit(models.UnitTypeWiki)) - m.Get("/archive/*", repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode), repo.Download) + m.Get("/archive/*", repo.MustBeNotBare, repo.Download, context.CheckUnit(models.UnitTypeCode)) m.Group("/pulls/:index", func() { m.Get("/commits", context.RepoRef(), repo.ViewPullCommits) @@ -631,11 +632,10 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/src/*", repo.SetEditorconfigIfExists, repo.Home) m.Get("/forks", repo.Forks) }, context.RepoRef(), context.CheckUnit(models.UnitTypeCode)) - m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", - repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode), repo.RawDiff) + m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", repo.MustBeNotBare, repo.RawDiff, context.CheckUnit(models.UnitTypeCode)) m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.SetEditorconfigIfExists, - repo.SetDiffViewStyle, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode), repo.CompareDiff) + repo.SetDiffViewStyle, repo.MustBeNotBare, repo.CompareDiff, context.CheckUnit(models.UnitTypeCode)) }, ignSignIn, context.RepoAssignment(), context.UnitTypes(), context.LoadRepoUnits()) m.Group("/:username/:reponame", func() { m.Get("/stars", repo.Stars) diff --git a/templates/repo/issue/view_content/sidebar.tmpl b/templates/repo/issue/view_content/sidebar.tmpl index 079c6f52a..4fa89bbdd 100644 --- a/templates/repo/issue/view_content/sidebar.tmpl +++ b/templates/repo/issue/view_content/sidebar.tmpl @@ -190,5 +190,42 @@ {{end}} {{end}} + +
+ +
+ Dependencies +
+ This issue currently doesn't have any dependencies. +
+
+ +
+
+ + + + +