Add webhook assigning test and fix possible bug (#34420)

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Lunny Xiao 2025-05-28 02:53:27 +08:00 committed by GitHub
parent 24a51059d7
commit 498088c053
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 235 additions and 185 deletions

View File

@ -54,6 +54,8 @@ func ToggleAssigneeWithNotify(ctx context.Context, issue *issues_model.Issue, do
if err != nil {
return false, nil, err
}
issue.AssigneeID = assigneeID
issue.Assignee = assignee
notify_service.IssueChangeAssignee(ctx, doer, issue, assignee, removed, comment)

View File

@ -151,6 +151,15 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content
return issueURL
}
func testIssueAssign(t *testing.T, session *TestSession, repoLink string, issueID, assigneeID int64) {
req := NewRequestWithValues(t, "POST", fmt.Sprintf(repoLink+"/issues/assignee?issue_ids=%d", issueID), map[string]string{
"_csrf": GetUserCSRFToken(t, session),
"id": strconv.FormatInt(assigneeID, 10),
"action": "", // empty action means assign
})
session.MakeRequest(t, req, http.StatusOK)
}
func testIssueAddComment(t *testing.T, session *TestSession, issueURL, content, status string) int64 {
req := NewRequest(t, "GET", issueURL)
resp := session.MakeRequest(t, req, http.StatusOK)

View File

@ -131,6 +131,7 @@ func (m *mockWebhookProvider) Close() {
}
func Test_WebhookCreate(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.CreatePayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -143,7 +144,6 @@ func Test_WebhookCreate(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -163,6 +163,7 @@ func Test_WebhookCreate(t *testing.T) {
}
func Test_WebhookDelete(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.DeletePayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -175,7 +176,6 @@ func Test_WebhookDelete(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -196,6 +196,7 @@ func Test_WebhookDelete(t *testing.T) {
}
func Test_WebhookFork(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.ForkPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -208,7 +209,6 @@ func Test_WebhookFork(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user1")
@ -228,6 +228,7 @@ func Test_WebhookFork(t *testing.T) {
}
func Test_WebhookIssueComment(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.IssueCommentPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -240,7 +241,6 @@ func Test_WebhookIssueComment(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -312,6 +312,7 @@ func Test_WebhookIssueComment(t *testing.T) {
}
func Test_WebhookRelease(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.ReleasePayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -324,7 +325,6 @@ func Test_WebhookRelease(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -345,6 +345,7 @@ func Test_WebhookRelease(t *testing.T) {
}
func Test_WebhookPush(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.PushPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -357,7 +358,6 @@ func Test_WebhookPush(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -416,6 +416,7 @@ func Test_WebhookPushDevBranch(t *testing.T) {
}
func Test_WebhookIssue(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.IssuePayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -428,7 +429,6 @@ func Test_WebhookIssue(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -445,6 +445,45 @@ func Test_WebhookIssue(t *testing.T) {
assert.Equal(t, "user2/repo1", payloads[0].Issue.Repo.FullName)
assert.Equal(t, "Title1", payloads[0].Issue.Title)
assert.Equal(t, "Description1", payloads[0].Issue.Body)
assert.Positive(t, payloads[0].Issue.Created.Unix())
assert.Positive(t, payloads[0].Issue.Updated.Unix())
})
}
func Test_WebhookIssueAssign(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.PullRequestPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
content, _ := io.ReadAll(r.Body)
var payload api.PullRequestPayload
err := json.Unmarshal(content, &payload)
assert.NoError(t, err)
payloads = append(payloads, payload)
triggeredEvent = "pull_request_assign"
}, http.StatusOK)
defer provider.Close()
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
repo1 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 1})
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "pull_request_assign")
// 2. trigger the webhook, issue 2 is a pull request
testIssueAssign(t, session, repo1.Link(), 2, user2.ID)
// 3. validate the webhook is triggered
assert.Equal(t, "pull_request_assign", triggeredEvent)
assert.Len(t, payloads, 1)
assert.EqualValues(t, "assigned", payloads[0].Action)
assert.Equal(t, "repo1", payloads[0].PullRequest.Base.Repository.Name)
assert.Equal(t, "user2/repo1", payloads[0].PullRequest.Base.Repository.FullName)
assert.Equal(t, "issue2", payloads[0].PullRequest.Title)
assert.Equal(t, "content for the second issue", payloads[0].PullRequest.Body)
assert.Equal(t, user2.ID, payloads[0].PullRequest.Assignee.ID)
})
}
@ -521,6 +560,7 @@ func Test_WebhookIssueMilestone(t *testing.T) {
}
func Test_WebhookPullRequest(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.PullRequestPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -533,7 +573,6 @@ func Test_WebhookPullRequest(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -558,6 +597,7 @@ func Test_WebhookPullRequest(t *testing.T) {
}
func Test_WebhookPullRequestComment(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.IssueCommentPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -570,7 +610,6 @@ func Test_WebhookPullRequestComment(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -596,6 +635,7 @@ func Test_WebhookPullRequestComment(t *testing.T) {
}
func Test_WebhookWiki(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.WikiPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -608,7 +648,6 @@ func Test_WebhookWiki(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -628,6 +667,7 @@ func Test_WebhookWiki(t *testing.T) {
}
func Test_WebhookRepository(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.RepositoryPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -640,7 +680,6 @@ func Test_WebhookRepository(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user1")
@ -660,6 +699,7 @@ func Test_WebhookRepository(t *testing.T) {
}
func Test_WebhookPackage(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.PackagePayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -672,7 +712,6 @@ func Test_WebhookPackage(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user1")
@ -697,6 +736,7 @@ func Test_WebhookPackage(t *testing.T) {
}
func Test_WebhookStatus(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.CommitStatusPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -714,7 +754,6 @@ func Test_WebhookStatus(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -750,6 +789,7 @@ func Test_WebhookStatus(t *testing.T) {
}
func Test_WebhookStatus_NoWrongTrigger(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var trigger string
provider := newMockWebhookProvider(func(r *http.Request) {
assert.NotContains(t, r.Header["X-Github-Event-Type"], "status", "X-GitHub-Event-Type should not contain status")
@ -759,7 +799,6 @@ func Test_WebhookStatus_NoWrongTrigger(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
session := loginUser(t, "user2")
@ -775,6 +814,7 @@ func Test_WebhookStatus_NoWrongTrigger(t *testing.T) {
}
func Test_WebhookWorkflowJob(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
var payloads []api.WorkflowJobPayload
var triggeredEvent string
provider := newMockWebhookProvider(func(r *http.Request) {
@ -790,7 +830,6 @@ func Test_WebhookWorkflowJob(t *testing.T) {
}, http.StatusOK)
defer provider.Close()
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// 1. create a new webhook with special webhook for repo1
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
session := loginUser(t, "user2")