return fmt.Errorf("createComment: %v", err)
}
- if err = issue.neuterCrossReferences(sess); err != nil {
- return err
- }
-
- if err = issue.addCrossReferences(sess, doer); err != nil {
+ if err = issue.addCrossReferences(sess, doer, true); err != nil {
return err
}
if err = updateIssueCols(sess, issue, "content"); err != nil {
return fmt.Errorf("UpdateIssueCols: %v", err)
}
- if err = issue.neuterCrossReferences(sess); err != nil {
- return err
- }
- if err = issue.addCrossReferences(sess, doer); err != nil {
+
+ if err = issue.addCrossReferences(sess, doer, true); err != nil {
return err
}
if err = opts.Issue.loadAttributes(e); err != nil {
return err
}
- return opts.Issue.addCrossReferences(e, doer)
+ return opts.Issue.addCrossReferences(e, doer, false)
}
// NewIssue creates new issue with labels for repository.
if err := updateIssue(sess, issue); err != nil {
return err
}
- if err := issue.neuterCrossReferences(sess); err != nil {
- return err
- }
if err := issue.loadPoster(sess); err != nil {
return err
}
- if err := issue.addCrossReferences(sess, issue.Poster); err != nil {
+ if err := issue.addCrossReferences(sess, issue.Poster, true); err != nil {
return err
}
return sess.Commit()
Doer *User
OrigIssue *Issue
OrigComment *Comment
+ RemoveOld bool
}
-func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
+func findOldCrossReferences(e Engine, issueID int64, commentID int64) ([]*Comment, error) {
active := make([]*Comment, 0, 10)
- sess := e.Where("`ref_action` IN (?, ?, ?)", references.XRefActionNone, references.XRefActionCloses, references.XRefActionReopens).
+ return active, e.Where("`ref_action` IN (?, ?, ?)", references.XRefActionNone, references.XRefActionCloses, references.XRefActionReopens).
And("`ref_issue_id` = ?", issueID).
- And("`ref_comment_id` = ?", commentID)
- if err := sess.Find(&active); err != nil || len(active) == 0 {
+ And("`ref_comment_id` = ?", commentID).
+ Find(&active)
+}
+
+func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
+ active, err := findOldCrossReferences(e, issueID, commentID)
+ if err != nil {
return err
}
ids := make([]int64, len(active))
for i, c := range active {
ids[i] = c.ID
}
+ return neuterCrossReferencesIds(e, ids)
+}
+
+func neuterCrossReferencesIds(e Engine, ids []int64) error {
_, err := e.In("id", ids).Cols("`ref_action`").Update(&Comment{RefAction: references.XRefActionNeutered})
return err
}
// \/ \/ \/
//
-func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) error {
+func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User, removeOld bool) error {
var commentType CommentType
if issue.IsPull {
commentType = CommentTypePullRef
Type: commentType,
Doer: doer,
OrigIssue: issue,
+ RemoveOld: removeOld,
}
return issue.createCrossReferences(e, ctx, issue.Title, issue.Content)
}
if err != nil {
return err
}
+ if ctx.RemoveOld {
+ var commentID int64
+ if ctx.OrigComment != nil {
+ commentID = ctx.OrigComment.ID
+ }
+ active, err := findOldCrossReferences(e, ctx.OrigIssue.ID, commentID)
+ if err != nil {
+ return err
+ }
+ ids := make([]int64, 0, len(active))
+ for _, c := range active {
+ found := false
+ for i, x := range xreflist {
+ if x.Issue.ID == c.IssueID && x.Action == c.RefAction {
+ found = true
+ xreflist = append(xreflist[:i], xreflist[i+1:]...)
+ break
+ }
+ }
+ if !found {
+ ids = append(ids, c.ID)
+ }
+ }
+ if len(ids) > 0 {
+ if err = neuterCrossReferencesIds(e, ids); err != nil {
+ return err
+ }
+ }
+ }
for _, xref := range xreflist {
var refCommentID int64
if ctx.OrigComment != nil {
return refIssue, refAction, nil
}
-func (issue *Issue) neuterCrossReferences(e Engine) error {
- return neuterCrossReferences(e, issue.ID, 0)
-}
-
// _________ __
// \_ ___ \ ____ _____ _____ ____ _____/ |_
// / \ \/ / _ \ / \ / \_/ __ \ / \ __\
// \/ \/ \/ \/ \/
//
-func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User) error {
+func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User, removeOld bool) error {
if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
return nil
}
Doer: doer,
OrigIssue: comment.Issue,
OrigComment: comment,
+ RemoveOld: removeOld,
}
return comment.Issue.createCrossReferences(e, ctx, "", comment.Content)
}
assert.NoError(t, err)
i, err = getIssueByID(sess, i.ID)
assert.NoError(t, err)
- assert.NoError(t, i.addCrossReferences(sess, d))
+ assert.NoError(t, i.addCrossReferences(sess, d, false))
assert.NoError(t, sess.Commit())
return i
}
assert.NoError(t, sess.Begin())
_, err := sess.Insert(c)
assert.NoError(t, err)
- assert.NoError(t, c.addCrossReferences(sess, d))
+ assert.NoError(t, c.addCrossReferences(sess, d, false))
assert.NoError(t, sess.Commit())
return c
}