Skip to content

Commit 2406094

Browse files
lafrikslunny
authored andcommitted
Issue content should not be updated when closing with comment (#2833)
1 parent 8798cf4 commit 2406094

File tree

5 files changed

+78
-25
lines changed

5 files changed

+78
-25
lines changed

integrations/issue_test.go

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestNoLoginViewIssue(t *testing.T) {
107107
MakeRequest(t, req, http.StatusOK)
108108
}
109109

110-
func testNewIssue(t *testing.T, session *TestSession, user, repo, title string) {
110+
func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content string) string {
111111

112112
req := NewRequest(t, "GET", path.Join(user, repo, "issues", "new"))
113113
resp := session.MakeRequest(t, req, http.StatusOK)
@@ -116,17 +116,70 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title string)
116116
link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action")
117117
assert.True(t, exists, "The template has changed")
118118
req = NewRequestWithValues(t, "POST", link, map[string]string{
119-
"_csrf": htmlDoc.GetCSRF(),
120-
"title": title,
119+
"_csrf": htmlDoc.GetCSRF(),
120+
"title": title,
121+
"content": content,
122+
})
123+
resp = session.MakeRequest(t, req, http.StatusFound)
124+
125+
issueURL := RedirectURL(t, resp)
126+
req = NewRequest(t, "GET", issueURL)
127+
resp = session.MakeRequest(t, req, http.StatusOK)
128+
129+
htmlDoc = NewHTMLParser(t, resp.Body)
130+
val := htmlDoc.doc.Find("#issue-title").Text()
131+
assert.Equal(t, title, val)
132+
val = htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").First().Text()
133+
assert.Equal(t, content, val)
134+
135+
return issueURL
136+
}
137+
138+
func testIssueAddComment(t *testing.T, session *TestSession, issueURL, content, status string) {
139+
140+
req := NewRequest(t, "GET", issueURL)
141+
resp := session.MakeRequest(t, req, http.StatusOK)
142+
143+
htmlDoc := NewHTMLParser(t, resp.Body)
144+
link, exists := htmlDoc.doc.Find("#comment-form").Attr("action")
145+
assert.True(t, exists, "The template has changed")
146+
147+
commentCount := htmlDoc.doc.Find(".comment-list .comments .comment .render-content").Length()
148+
149+
req = NewRequestWithValues(t, "POST", link, map[string]string{
150+
"_csrf": htmlDoc.GetCSRF(),
151+
"content": content,
152+
"status": status,
121153
})
122154
resp = session.MakeRequest(t, req, http.StatusFound)
123155

124156
req = NewRequest(t, "GET", RedirectURL(t, resp))
125157
resp = session.MakeRequest(t, req, http.StatusOK)
158+
159+
htmlDoc = NewHTMLParser(t, resp.Body)
160+
161+
val := htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").Eq(commentCount).Text()
162+
assert.Equal(t, content, val)
126163
}
127164

128165
func TestNewIssue(t *testing.T) {
129166
prepareTestEnv(t)
130167
session := loginUser(t, "user2")
131-
testNewIssue(t, session, "user2", "repo1", "Title")
168+
testNewIssue(t, session, "user2", "repo1", "Title", "Description")
169+
}
170+
171+
func TestIssueCommentClose(t *testing.T) {
172+
prepareTestEnv(t)
173+
session := loginUser(t, "user2")
174+
issueURL := testNewIssue(t, session, "user2", "repo1", "Title", "Description")
175+
testIssueAddComment(t, session, issueURL, "Test comment 1", "")
176+
testIssueAddComment(t, session, issueURL, "Test comment 2", "")
177+
testIssueAddComment(t, session, issueURL, "Test comment 3", "close")
178+
179+
// Validate that issue content has not been updated
180+
req := NewRequest(t, "GET", issueURL)
181+
resp := session.MakeRequest(t, req, http.StatusOK)
182+
htmlDoc := NewHTMLParser(t, resp.Body)
183+
val := htmlDoc.doc.Find(".comment-list .comments .comment .render-content p").First().Text()
184+
assert.Equal(t, "Description", val)
132185
}

integrations/repo_activity_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func TestRepoActivity(t *testing.T) {
3131
testPullCreate(t, session, "user1", "repo1", "feat/much_better_readme")
3232

3333
// Create issues (3 new issues)
34-
testNewIssue(t, session, "user2", "repo1", "Issue 1")
35-
testNewIssue(t, session, "user2", "repo1", "Issue 2")
36-
testNewIssue(t, session, "user2", "repo1", "Issue 3")
34+
testNewIssue(t, session, "user2", "repo1", "Issue 1", "Description 1")
35+
testNewIssue(t, session, "user2", "repo1", "Issue 2", "Description 2")
36+
testNewIssue(t, session, "user2", "repo1", "Issue 3", "Description 3")
3737

3838
// Create releases (1 new release)
3939
createNewRelease(t, session, "/user2/repo1", "v1.0.0", "v1.0.0", false, false)

models/issue_comment.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,15 @@ func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (e
272272
return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
273273
}
274274

275+
content := c.Content
276+
275277
switch opType {
276-
case ActionCommentIssue:
277-
issue.Content = c.Content
278278
case ActionCloseIssue:
279-
issue.Content = fmt.Sprintf("Closed #%d", issue.Index)
279+
content = fmt.Sprintf("Closed #%d", issue.Index)
280280
case ActionReopenIssue:
281-
issue.Content = fmt.Sprintf("Reopened #%d", issue.Index)
281+
content = fmt.Sprintf("Reopened #%d", issue.Index)
282282
}
283-
if err = mailIssueCommentToParticipants(e, issue, c.Poster, c, mentions); err != nil {
283+
if err = mailIssueCommentToParticipants(e, issue, c.Poster, content, c, mentions); err != nil {
284284
log.Error(4, "mailIssueCommentToParticipants: %v", err)
285285
}
286286

models/issue_mail.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (issue *Issue) mailSubject() string {
2222
// This function sends two list of emails:
2323
// 1. Repository watchers and users who are participated in comments.
2424
// 2. Users who are not in 1. but get mentioned in current issue/comment.
25-
func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, comment *Comment, mentions []string) error {
25+
func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content string, comment *Comment, mentions []string) error {
2626
if !setting.Service.EnableNotifyMail {
2727
return nil
2828
}
@@ -80,7 +80,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, comment
8080
names = append(names, participants[i].Name)
8181
}
8282

83-
SendIssueCommentMail(issue, doer, comment, tos)
83+
SendIssueCommentMail(issue, doer, content, comment, tos)
8484

8585
// Mail mentioned people and exclude watchers.
8686
names = append(names, doer.Name)
@@ -92,7 +92,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, comment
9292

9393
tos = append(tos, mentions[i])
9494
}
95-
SendIssueMentionMail(issue, doer, comment, getUserEmailsByNames(e, tos))
95+
SendIssueMentionMail(issue, doer, content, comment, getUserEmailsByNames(e, tos))
9696

9797
return nil
9898
}
@@ -109,7 +109,7 @@ func (issue *Issue) mailParticipants(e Engine) (err error) {
109109
return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
110110
}
111111

112-
if err = mailIssueCommentToParticipants(e, issue, issue.Poster, nil, mentions); err != nil {
112+
if err = mailIssueCommentToParticipants(e, issue, issue.Poster, issue.Content, nil, mentions); err != nil {
113113
log.Error(4, "mailIssueCommentToParticipants: %v", err)
114114
}
115115

models/mail.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ func composeTplData(subject, body, link string) map[string]interface{} {
149149
return data
150150
}
151151

152-
func composeIssueCommentMessage(issue *Issue, doer *User, comment *Comment, tplName base.TplName, tos []string, info string) *mailer.Message {
152+
func composeIssueCommentMessage(issue *Issue, doer *User, content string, comment *Comment, tplName base.TplName, tos []string, info string) *mailer.Message {
153153
subject := issue.mailSubject()
154-
body := string(markup.RenderByType(markdown.MarkupName, []byte(issue.Content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
154+
body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
155155

156156
data := make(map[string]interface{}, 10)
157157
if comment != nil {
@@ -161,30 +161,30 @@ func composeIssueCommentMessage(issue *Issue, doer *User, comment *Comment, tplN
161161
}
162162
data["Doer"] = doer
163163

164-
var content bytes.Buffer
164+
var mailBody bytes.Buffer
165165

166-
if err := templates.ExecuteTemplate(&content, string(tplName), data); err != nil {
166+
if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil {
167167
log.Error(3, "Template: %v", err)
168168
}
169169

170-
msg := mailer.NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, content.String())
170+
msg := mailer.NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String())
171171
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
172172
return msg
173173
}
174174

175175
// SendIssueCommentMail composes and sends issue comment emails to target receivers.
176-
func SendIssueCommentMail(issue *Issue, doer *User, comment *Comment, tos []string) {
176+
func SendIssueCommentMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
177177
if len(tos) == 0 {
178178
return
179179
}
180180

181-
mailer.SendAsync(composeIssueCommentMessage(issue, doer, comment, mailIssueComment, tos, "issue comment"))
181+
mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
182182
}
183183

184184
// SendIssueMentionMail composes and sends issue mention emails to target receivers.
185-
func SendIssueMentionMail(issue *Issue, doer *User, comment *Comment, tos []string) {
185+
func SendIssueMentionMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) {
186186
if len(tos) == 0 {
187187
return
188188
}
189-
mailer.SendAsync(composeIssueCommentMessage(issue, doer, comment, mailIssueMention, tos, "issue mention"))
189+
mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
190190
}

0 commit comments

Comments
 (0)