From ad0fcd4dc5bfff6fd0e1f57465b25e0036febc1e Mon Sep 17 00:00:00 2001 From: Konrad Langenberg Date: Mon, 4 Sep 2017 23:32:34 +0200 Subject: [PATCH] Beautify @JonasFranzDEV Signed-off-by: Konrad --- models/issue_comment.go | 10 +- models/issue_dependency.go | 126 ++++++------------ routers/repo/issue_dependency_add.go | 13 +- routers/repo/issue_dependency_remove.go | 12 +- .../repo/issue/view_content/comments.tmpl | 7 +- 5 files changed, 69 insertions(+), 99 deletions(-) diff --git a/models/issue_comment.go b/models/issue_comment.go index 1f0a2022f..3bcb15694 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -88,7 +88,8 @@ type Comment struct { OldAssignee *User `xorm:"-"` OldTitle string NewTitle string - DependentIssue int64 + DependentIssueID int64 + DependentIssue *Issue `xorm:"-"` CommitID int64 Line int64 @@ -323,6 +324,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err OldTitle: opts.OldTitle, NewTitle: opts.NewTitle, DependentIssue: opts.DependentIssue, + DependentIssueID: opts.DependentIssue.ID, } //fmt.Println(comment) @@ -506,7 +508,7 @@ func createDeleteBranchComment(e *xorm.Session, doer *User, repo *Repository, is } // Creates issue dependency comment -func createIssueDependencyComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, dependantIssue int64, added bool) (*Comment, error) { +func createIssueDependencyComment(e *xorm.Session, doer *User, issue *Issue, dependantIssue *Issue, added bool) (*Comment, error) { cType := CommentTypeAddedDependency if !added { cType = CommentTypeRemovedDependency @@ -515,7 +517,7 @@ func createIssueDependencyComment(e *xorm.Session, doer *User, repo *Repository, return createComment(e, &CreateCommentOptions{ Type: cType, Doer: doer, - Repo: repo, + Repo: issue.Repo, Issue: issue, DependentIssue: dependantIssue, Content: issue.Title, @@ -529,6 +531,7 @@ type CreateCommentOptions struct { Repo *Repository Issue *Issue Label *Label + DependentIssue *Issue OldMilestoneID int64 MilestoneID int64 @@ -541,7 +544,6 @@ type CreateCommentOptions struct { LineNum int64 Content string Attachments []string // UUIDs of attachments - DependentIssue int64 } // CreateComment creates comment of issue or commit. diff --git a/models/issue_dependency.go b/models/issue_dependency.go index bd230ffd5..0947f607f 100644 --- a/models/issue_dependency.go +++ b/models/issue_dependency.go @@ -10,14 +10,14 @@ import ( // 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"` + 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"` + 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. @@ -43,7 +43,7 @@ func (iw *IssueDependency) BeforeUpdate() { } // CreateIssueDependency creates a new dependency for an issue -func CreateIssueDependency(userID, issueID int64, depID int64) (err error, exists bool, depExists bool) { +func CreateIssueDependency(user *User, issue, dep *Issue) (err error, exists bool, depExists bool) { sess := x.NewSession() // TODO: Move this to the appropriate place @@ -53,71 +53,41 @@ func CreateIssueDependency(userID, issueID int64, depID int64) (err error, exist } // Check if it aleready exists - exists, err = issueDepExists(x, issueID, depID) + exists, err = issueDepExists(x, issue.ID, dep.ID) if err != nil { return err, exists, false } // If it not exists, create it, otherwise show an error message if !exists { - // Check if the other issue exists - var issue = Issue{} - issueExists, err := x.Id(depID).Get(&issue) + newId := new(IssueDependency) + newId.UserID = user.ID + newId.IssueID = issue.ID + newId.DependencyID = dep.ID - if issueExists { - newId := new(IssueDependency) - newId.UserID = userID - newId.IssueID = issueID - newId.DependencyID = depID - - if _, err := x.Insert(newId); err != nil { - return err, exists, false - } - - user, err := getUserByID(x, userID) - if err != nil { - return err, exists, false - } - - // Add comment referencing the new dependency - - repo, err := getRepositoryByID(x, issue.RepoID) - - if err != nil { - return err, exists, false - } - - - _, err = createIssueDependencyComment(sess, user, repo, &issue, depID, true) - - if err != nil { - return err, exists, false - } - - // Create a new comment for the dependent issue - depIssue, err := getIssueByID(x, issueID) - if err != nil { - return err, exists, false - } - - repo, err = getRepositoryByID(x, depIssue.RepoID) - if err != nil { - return err, exists, false - } - - _, err = createIssueDependencyComment(sess, user, repo, depIssue, issueID, true) - - if err != nil { - return err, exists, false - } + if _, err := x.Insert(newId); err != nil { + return err, exists, false + } + + // Add comment referencing the new dependency + _, err = createIssueDependencyComment(sess, user, issue, dep, true) + + if err != nil { + return err, exists, false + } + + // Create a new comment for the dependent issue + _, err = createIssueDependencyComment(sess, user, dep, issue, true) + + if err != nil { + return err, exists, false } - return err, exists, true } return nil, exists, false } // Removes a dependency from an issue -func RemoveIssueDependency(userID, issueID int64, depID int64, depType int64) (err error) { +func RemoveIssueDependency(user *User, issue *Issue, dep *Issue, depType int64) (err error) { sess := x.NewSession() // TODO: Same as above @@ -127,7 +97,7 @@ func RemoveIssueDependency(userID, issueID int64, depID int64, depType int64) (e } // Check if it exists - exists, err := issueDepExists(x, issueID, depID) + exists, err := issueDepExists(x, issue.ID, dep.ID) if err != nil { return err } @@ -135,47 +105,29 @@ func RemoveIssueDependency(userID, issueID int64, depID int64, depType int64) (e // If it exists, remove it, otherwise show an error message if exists { - if depType == 1{ - _, err := x.Delete(&IssueDependency{IssueID: issueID, DependencyID: depID}) + if depType == 1 { + _, err := x.Delete(&IssueDependency{IssueID: issue.ID, DependencyID: dep.ID}) if err != nil { return err } } - if depType == 2{ - _, err := x.Delete(&IssueDependency{IssueID: depID, DependencyID: issueID}) + if depType == 2 { + _, err := x.Delete(&IssueDependency{IssueID: dep.ID, DependencyID: issue.ID}) if err != nil { return err } } // Add comment referencing the removed dependency - issue, err := getIssueByID(x, depID) - - if err != nil { - return err - } - - user, _ := getUserByID(x, userID) - - repo, _ := getRepositoryByID(x, issue.RepoID) - - _, err = createIssueDependencyComment(sess, user, repo, issue, depID, false) + _, err = createIssueDependencyComment(sess, user, issue, dep, false) if err != nil { return err } // Create a new comment for the dependent issue - depIssue, err := getIssueByID(x, issueID) - - if err != nil { - return err - } - - repo, _ = getRepositoryByID(x, depIssue.RepoID) - - _, err = createIssueDependencyComment(sess, user, repo, depIssue, issueID, false) + _, err = createIssueDependencyComment(sess, user, dep, issue, false) if err != nil { return err @@ -202,12 +154,12 @@ func issueDepExists(e Engine, issueID int64, depID int64) (exists bool, err erro } // check if issue can be closed -func IssueNoDependenciesLeft(issueID int64) bool{ +func IssueNoDependenciesLeft(issueID int64) bool { var issueDeps []IssueDependency err := x.Where("issue_id = ?", issueID).Find(&issueDeps) - for _, issueDep := range issueDeps{ + for _, issueDep := range issueDeps { issueDetails, _ := getIssueByID(x, issueDep.DependencyID) if !issueDetails.IsClosed { return false diff --git a/routers/repo/issue_dependency_add.go b/routers/repo/issue_dependency_add.go index 3facf63bd..8d4bf6b99 100644 --- a/routers/repo/issue_dependency_add.go +++ b/routers/repo/issue_dependency_add.go @@ -15,7 +15,7 @@ import ( // Adds new dependencies func AddDependency(c *context.Context) { - dep, err := strconv.ParseInt(c.Req.PostForm.Get("newDependency"), 10, 64) + depID, err := strconv.ParseInt(c.Req.PostForm.Get("newDependency"), 10, 64) if err != nil { c.Handle(http.StatusBadRequest, "issue ID is not int", err) return @@ -28,12 +28,19 @@ func AddDependency(c *context.Context) { return } + // Dependency + dep, err := models.GetIssueByID(depID) + if err != nil { + c.Handle(http.StatusInternalServerError, "GetIssueByID", err) + return + } + // Check if issue and dependency is the same - if dep == issueIndex{ + if dep.Index == issueIndex{ c.Flash.Error("You cannot make an issue depend on itself!") } else { - err, exists, depExists := models.CreateIssueDependency(c.User.ID, issue.ID, dep) + err, exists, depExists := models.CreateIssueDependency(c.User, issue, dep) if err != nil { c.Handle(http.StatusInternalServerError, "CreateOrUpdateIssueDependency", err) return diff --git a/routers/repo/issue_dependency_remove.go b/routers/repo/issue_dependency_remove.go index 7abe925e6..2885673c8 100644 --- a/routers/repo/issue_dependency_remove.go +++ b/routers/repo/issue_dependency_remove.go @@ -15,7 +15,7 @@ import ( // IssueWatch sets issue watching func RemoveDependency(c *context.Context) { - dep, err := strconv.ParseInt(c.Req.PostForm.Get("removeDependencyID"), 10, 64) + depID, err := strconv.ParseInt(c.Req.PostForm.Get("removeDependencyID"), 10, 64) if err != nil { c.Handle(http.StatusBadRequest, "issue ID is not int", err) return @@ -41,9 +41,15 @@ func RemoveDependency(c *context.Context) { return } - err = models.RemoveIssueDependency(c.User.ID, issue.ID, dep, depType) + // Dependency + dep, err := models.GetIssueByID(depID) + if err != nil { + c.Handle(http.StatusInternalServerError, "GetIssueByID", err) + return + } + + err = models.RemoveIssueDependency(c.User, issue, dep, depType) if err != nil { - panic(err) c.Handle(http.StatusInternalServerError, "CreateOrUpdateIssueDependency", err) return } diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 59dda9a57..ff30fc536 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -152,7 +152,10 @@ @@ -168,7 +171,7 @@