mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	When updating comment, if the content is the same, just return and not update the databse (#34422)
Fix #34318
This commit is contained in:
		| @@ -609,6 +609,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	if form.Body != comment.Content { | ||||
| 		oldContent := comment.Content | ||||
| 		comment.Content = form.Body | ||||
| 		if err := issue_service.UpdateComment(ctx, comment, comment.ContentVersion, ctx.Doer, oldContent); err != nil { | ||||
| @@ -619,6 +620,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) | ||||
| 			} | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	ctx.JSON(http.StatusOK, convert.ToAPIComment(ctx, ctx.Repo.Repository, comment)) | ||||
| } | ||||
|   | ||||
| @@ -239,12 +239,18 @@ func UpdateCommentContent(ctx *context.Context) { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	oldContent := comment.Content | ||||
| 	newContent := ctx.FormString("content") | ||||
| 	contentVersion := ctx.FormInt("content_version") | ||||
| 	if contentVersion != comment.ContentVersion { | ||||
| 		ctx.JSONError(ctx.Tr("repo.comments.edit.already_changed")) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	if newContent != comment.Content { | ||||
| 		// allow to save empty content | ||||
| 		oldContent := comment.Content | ||||
| 		comment.Content = newContent | ||||
|  | ||||
| 		if err = issue_service.UpdateComment(ctx, comment, contentVersion, ctx.Doer, oldContent); err != nil { | ||||
| 			if errors.Is(err, user_model.ErrBlockedUser) { | ||||
| 				ctx.JSONError(ctx.Tr("repo.issues.comment.blocked_user")) | ||||
| @@ -255,6 +261,7 @@ func UpdateCommentContent(ctx *context.Context) { | ||||
| 			} | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if err := comment.LoadAttachments(ctx); err != nil { | ||||
| 		ctx.ServerError("LoadAttachments", err) | ||||
|   | ||||
| @@ -241,6 +241,7 @@ func Test_WebhookIssueComment(t *testing.T) { | ||||
|  | ||||
| 		testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "issue_comment") | ||||
|  | ||||
| 		t.Run("create comment", func(t *testing.T) { | ||||
| 			// 2. trigger the webhook | ||||
| 			issueURL := testNewIssue(t, session, "user2", "repo1", "Title2", "Description2") | ||||
| 			testIssueAddComment(t, session, issueURL, "issue title2 comment1", "") | ||||
| @@ -255,6 +256,54 @@ func Test_WebhookIssueComment(t *testing.T) { | ||||
| 			assert.Equal(t, "Description2", payloads[0].Issue.Body) | ||||
| 			assert.Equal(t, "issue title2 comment1", payloads[0].Comment.Body) | ||||
| 		}) | ||||
|  | ||||
| 		t.Run("update comment", func(t *testing.T) { | ||||
| 			payloads = make([]api.IssueCommentPayload, 0, 2) | ||||
| 			triggeredEvent = "" | ||||
|  | ||||
| 			// 2. trigger the webhook | ||||
| 			issueURL := testNewIssue(t, session, "user2", "repo1", "Title3", "Description3") | ||||
| 			commentID := testIssueAddComment(t, session, issueURL, "issue title3 comment1", "") | ||||
| 			modifiedContent := "issue title2 comment1 - modified" | ||||
| 			req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/comments/%d", "user2", "repo1", commentID), map[string]string{ | ||||
| 				"_csrf":   GetUserCSRFToken(t, session), | ||||
| 				"content": modifiedContent, | ||||
| 			}) | ||||
| 			session.MakeRequest(t, req, http.StatusOK) | ||||
|  | ||||
| 			// 3. validate the webhook is triggered | ||||
| 			assert.Equal(t, "issue_comment", triggeredEvent) | ||||
| 			assert.Len(t, payloads, 2) | ||||
| 			assert.EqualValues(t, "edited", payloads[1].Action) | ||||
| 			assert.Equal(t, "repo1", payloads[1].Issue.Repo.Name) | ||||
| 			assert.Equal(t, "user2/repo1", payloads[1].Issue.Repo.FullName) | ||||
| 			assert.Equal(t, "Title3", payloads[1].Issue.Title) | ||||
| 			assert.Equal(t, "Description3", payloads[1].Issue.Body) | ||||
| 			assert.Equal(t, modifiedContent, payloads[1].Comment.Body) | ||||
| 		}) | ||||
|  | ||||
| 		t.Run("Update comment with no content change", func(t *testing.T) { | ||||
| 			payloads = make([]api.IssueCommentPayload, 0, 2) | ||||
| 			triggeredEvent = "" | ||||
| 			commentContent := "issue title3 comment1" | ||||
|  | ||||
| 			// 2. trigger the webhook | ||||
| 			issueURL := testNewIssue(t, session, "user2", "repo1", "Title3", "Description3") | ||||
| 			commentID := testIssueAddComment(t, session, issueURL, commentContent, "") | ||||
|  | ||||
| 			payloads = make([]api.IssueCommentPayload, 0, 2) | ||||
| 			triggeredEvent = "" | ||||
| 			req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/comments/%d", "user2", "repo1", commentID), map[string]string{ | ||||
| 				"_csrf":   GetUserCSRFToken(t, session), | ||||
| 				"content": commentContent, | ||||
| 			}) | ||||
| 			session.MakeRequest(t, req, http.StatusOK) | ||||
|  | ||||
| 			// 3. validate the webhook is not triggered because no content change | ||||
| 			assert.Empty(t, triggeredEvent) | ||||
| 			assert.Empty(t, payloads) | ||||
| 		}) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func Test_WebhookRelease(t *testing.T) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user