Skip to content

Commit eb9b574

Browse files
committed
Fix pull request comment webhook with test
1 parent b2cecb0 commit eb9b574

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

routers/api/v1/utils/hook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
385385
w.HookEvents[webhook_module.HookEventPullRequestAssign] = pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign))
386386
w.HookEvents[webhook_module.HookEventPullRequestLabel] = pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel))
387387
w.HookEvents[webhook_module.HookEventPullRequestMilestone] = pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone))
388+
w.HookEvents[webhook_module.HookEventPullRequestComment] = pullHook(form.Events, string(webhook_module.HookEventPullRequestComment))
388389
w.HookEvents[webhook_module.HookEventPullRequestReview] = pullHook(form.Events, "pull_request_review")
389390
w.HookEvents[webhook_module.HookEventPullRequestReviewRequest] = pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest))
390391
w.HookEvents[webhook_module.HookEventPullRequestSync] = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync))

tests/integration/issue_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func testIssueAddComment(t *testing.T, session *TestSession, issueURL, content,
174174

175175
htmlDoc = NewHTMLParser(t, resp.Body)
176176

177-
val := htmlDoc.doc.Find(".comment-list .comment .render-content p").Eq(commentCount).Text()
177+
val := strings.TrimSpace(htmlDoc.doc.Find(".comment-list .comment .render-content").Eq(commentCount).Text())
178178
assert.Equal(t, content, val)
179179

180180
idAttr, has := htmlDoc.doc.Find(".comment-list .comment").Eq(commentCount).Attr("id")

tests/integration/repo_webhook_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,44 @@ func Test_WebhookPullRequest(t *testing.T) {
373373
})
374374
}
375375

376+
func Test_WebhookPullRequestComment(t *testing.T) {
377+
var payloads []api.IssueCommentPayload
378+
var triggeredEvent string
379+
provider := newMockWebhookProvider(func(r *http.Request) {
380+
content, _ := io.ReadAll(r.Body)
381+
var payload api.IssueCommentPayload
382+
err := json.Unmarshal([]byte(content), &payload)
383+
assert.NoError(t, err)
384+
payloads = append(payloads, payload)
385+
triggeredEvent = "pull_request_comment"
386+
}, http.StatusOK)
387+
defer provider.Close()
388+
389+
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
390+
// 1. create a new webhook with special webhook for repo1
391+
session := loginUser(t, "user2")
392+
393+
testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "pull_request_comment")
394+
395+
// 2. trigger the webhook
396+
testAPICreateBranch(t, session, "user2", "repo1", "master", "master2", http.StatusCreated)
397+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 1})
398+
prID := testCreatePullToDefaultBranch(t, session, repo1, repo1, "master2", "first pull request")
399+
400+
testIssueAddComment(t, session, "/user2/repo1/pulls/"+prID, "pull title2 comment1", "")
401+
402+
// 3. validate the webhook is triggered
403+
assert.EqualValues(t, "pull_request_comment", triggeredEvent)
404+
assert.Len(t, payloads, 1)
405+
assert.EqualValues(t, "created", payloads[0].Action)
406+
assert.EqualValues(t, "repo1", payloads[0].Issue.Repo.Name)
407+
assert.EqualValues(t, "user2/repo1", payloads[0].Issue.Repo.FullName)
408+
assert.EqualValues(t, "first pull request", payloads[0].Issue.Title)
409+
assert.EqualValues(t, "", payloads[0].Issue.Body)
410+
assert.EqualValues(t, "pull title2 comment1", payloads[0].Comment.Body)
411+
})
412+
}
413+
376414
func Test_WebhookWiki(t *testing.T) {
377415
var payloads []api.WikiPayload
378416
var triggeredEvent string

0 commit comments

Comments
 (0)