Browse Source

Issue content should not be updated when closing with comment (#2833)

Lauris BH 1 year ago
parent
commit
240609432b
5 changed files with 78 additions and 25 deletions
  1. 57 4
      integrations/issue_test.go
  2. 3 3
      integrations/repo_activity_test.go
  3. 5 5
      models/issue_comment.go
  4. 4 4
      models/issue_mail.go
  5. 9 9
      models/mail.go

+ 57 - 4
integrations/issue_test.go

@@ -107,7 +107,7 @@ func TestNoLoginViewIssue(t *testing.T) {
107 107
 	MakeRequest(t, req, http.StatusOK)
108 108
 }
109 109
 
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 {
111 111
 
112 112
 	req := NewRequest(t, "GET", path.Join(user, repo, "issues", "new"))
113 113
 	resp := session.MakeRequest(t, req, http.StatusOK)
@@ -116,17 +116,70 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title string)
116 116
 	link, exists := htmlDoc.doc.Find("form.ui.form").Attr("action")
117 117
 	assert.True(t, exists, "The template has changed")
118 118
 	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,
121 153
 	})
122 154
 	resp = session.MakeRequest(t, req, http.StatusFound)
123 155
 
124 156
 	req = NewRequest(t, "GET", RedirectURL(t, resp))
125 157
 	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)
126 163
 }
127 164
 
128 165
 func TestNewIssue(t *testing.T) {
129 166
 	prepareTestEnv(t)
130 167
 	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)
132 185
 }

+ 3 - 3
integrations/repo_activity_test.go

@@ -31,9 +31,9 @@ func TestRepoActivity(t *testing.T) {
31 31
 	testPullCreate(t, session, "user1", "repo1", "feat/much_better_readme")
32 32
 
33 33
 	// 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")
37 37
 
38 38
 	// Create releases (1 new release)
39 39
 	createNewRelease(t, session, "/user2/repo1", "v1.0.0", "v1.0.0", false, false)

+ 5 - 5
models/issue_comment.go

@@ -272,15 +272,15 @@ func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (e
272 272
 		return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
273 273
 	}
274 274
 
275
+	content := c.Content
276
+
275 277
 	switch opType {
276
-	case ActionCommentIssue:
277
-		issue.Content = c.Content
278 278
 	case ActionCloseIssue:
279
-		issue.Content = fmt.Sprintf("Closed #%d", issue.Index)
279
+		content = fmt.Sprintf("Closed #%d", issue.Index)
280 280
 	case ActionReopenIssue:
281
-		issue.Content = fmt.Sprintf("Reopened #%d", issue.Index)
281
+		content = fmt.Sprintf("Reopened #%d", issue.Index)
282 282
 	}
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 {
284 284
 		log.Error(4, "mailIssueCommentToParticipants: %v", err)
285 285
 	}
286 286
 

+ 4 - 4
models/issue_mail.go

@@ -22,7 +22,7 @@ func (issue *Issue) mailSubject() string {
22 22
 // This function sends two list of emails:
23 23
 // 1. Repository watchers and users who are participated in comments.
24 24
 // 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 {
26 26
 	if !setting.Service.EnableNotifyMail {
27 27
 		return nil
28 28
 	}
@@ -80,7 +80,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, comment
80 80
 		names = append(names, participants[i].Name)
81 81
 	}
82 82
 
83
-	SendIssueCommentMail(issue, doer, comment, tos)
83
+	SendIssueCommentMail(issue, doer, content, comment, tos)
84 84
 
85 85
 	// Mail mentioned people and exclude watchers.
86 86
 	names = append(names, doer.Name)
@@ -92,7 +92,7 @@ func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, comment
92 92
 
93 93
 		tos = append(tos, mentions[i])
94 94
 	}
95
-	SendIssueMentionMail(issue, doer, comment, getUserEmailsByNames(e, tos))
95
+	SendIssueMentionMail(issue, doer, content, comment, getUserEmailsByNames(e, tos))
96 96
 
97 97
 	return nil
98 98
 }
@@ -109,7 +109,7 @@ func (issue *Issue) mailParticipants(e Engine) (err error) {
109 109
 		return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
110 110
 	}
111 111
 
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 {
113 113
 		log.Error(4, "mailIssueCommentToParticipants: %v", err)
114 114
 	}
115 115
 

+ 9 - 9
models/mail.go

@@ -149,9 +149,9 @@ func composeTplData(subject, body, link string) map[string]interface{} {
149 149
 	return data
150 150
 }
151 151
 
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 {
153 153
 	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()))
155 155
 
156 156
 	data := make(map[string]interface{}, 10)
157 157
 	if comment != nil {
@@ -161,30 +161,30 @@ func composeIssueCommentMessage(issue *Issue, doer *User, comment *Comment, tplN
161 161
 	}
162 162
 	data["Doer"] = doer
163 163
 
164
-	var content bytes.Buffer
164
+	var mailBody bytes.Buffer
165 165
 
166
-	if err := templates.ExecuteTemplate(&content, string(tplName), data); err != nil {
166
+	if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil {
167 167
 		log.Error(3, "Template: %v", err)
168 168
 	}
169 169
 
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())
171 171
 	msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
172 172
 	return msg
173 173
 }
174 174
 
175 175
 // 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) {
177 177
 	if len(tos) == 0 {
178 178
 		return
179 179
 	}
180 180
 
181
-	mailer.SendAsync(composeIssueCommentMessage(issue, doer, comment, mailIssueComment, tos, "issue comment"))
181
+	mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment"))
182 182
 }
183 183
 
184 184
 // 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) {
186 186
 	if len(tos) == 0 {
187 187
 		return
188 188
 	}
189
-	mailer.SendAsync(composeIssueCommentMessage(issue, doer, comment, mailIssueMention, tos, "issue mention"))
189
+	mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention"))
190 190
 }