From 59d91723a12291a9a982c0e2e6db01192b07340c Mon Sep 17 00:00:00 2001 From: Keith Rutkowski Date: Mon, 2 Apr 2018 10:19:20 -0400 Subject: [PATCH] Add the UpdateIssuesComment function The UpdateIssuesComment function handles referencing and manipulation of issues from comments, issues, and pull requests --- models/action.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/models/action.go b/models/action.go index b81e7b3d3..5747d3979 100644 --- a/models/action.go +++ b/models/action.go @@ -480,6 +480,67 @@ func findIssueReferencesInString(message string, repo *Repository) (map[int64]Ke return refs, nil } +// UpdateIssuesComment checks if issues are manipulated by a comment +func UpdateIssuesComment(doer *User, repo *Repository, commentIssue *Issue, comment *Comment, canOpenClose bool) error { + var refString string + if comment != nil { + refString = comment.Content + } else { + refString = commentIssue.Title + ": " + commentIssue.Content + } + + refs, err := findIssueReferencesInString(refString, repo) + if err != nil { + return err + } + + for id, mask := range refs { + issue, err := GetIssueByID(id) + if err != nil { + return err + } + if issue == nil || issue.ID == commentIssue.ID { + continue + } + + if (mask & KeywordsFoundReference) == KeywordsFoundReference { + uniqueID := fmt.Sprintf("%d", commentIssue.ID) + if comment != nil { + uniqueID += fmt.Sprintf("@%d", comment.ID) + } + + if comment != nil { + err = CreateCommentRefComment(doer, repo, issue, fmt.Sprintf(`%d`, comment.ID), base.EncodeSha1(uniqueID)) + } else if commentIssue.IsPull { + err = CreatePullRefComment(doer, repo, issue, fmt.Sprintf(`%d`, commentIssue.ID), base.EncodeSha1(uniqueID)) + } else { + err = CreateIssueRefComment(doer, repo, issue, fmt.Sprintf(`%d`, commentIssue.ID), base.EncodeSha1(uniqueID)) + } + if err != nil { + return err + } + } + + if canOpenClose { + // take no action if both KeywordsFoundClose and KeywordsFoundOpen are set + if (mask & (KeywordsFoundReopen | KeywordsFoundClose)) == KeywordsFoundClose { + if issue.RepoID == repo.ID && !issue.IsClosed { + if err = issue.ChangeStatus(doer, repo, true); err != nil { + return err + } + } + } else if (mask & (KeywordsFoundReopen | KeywordsFoundClose)) == KeywordsFoundReopen { + if issue.RepoID == repo.ID && issue.IsClosed { + if err = issue.ChangeStatus(doer, repo, false); err != nil { + return err + } + } + } + } + } + return nil +} + // UpdateIssuesCommit checks if issues are manipulated by commit message. func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, commitsAreMerged bool) error { // Commits are appended in the reverse order.