Skip to content

Commit 2d8affe

Browse files
committed
Fix webhook assign
1 parent ad27144 commit 2d8affe

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

services/issue/assignee.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func ToggleAssigneeWithNotify(ctx context.Context, issue *issues_model.Issue, do
5454
if err != nil {
5555
return false, nil, err
5656
}
57+
issue.AssigneeID = assigneeID
58+
issue.Assignee = assignee
5759

5860
notify_service.IssueChangeAssignee(ctx, doer, issue, assignee, removed, comment)
5961

tests/integration/issue_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content
151151
return issueURL
152152
}
153153

154+
func testIssueAssign(t *testing.T, session *TestSession, repoLink string, issueID, assigneeID int64) {
155+
req := NewRequestWithValues(t, "POST", fmt.Sprintf(repoLink+"/issues/assignee?issue_ids=%d", issueID), map[string]string{
156+
"_csrf": GetUserCSRFToken(t, session),
157+
"id": strconv.FormatInt(assigneeID, 10),
158+
"action": "", // empty action means assign
159+
})
160+
session.MakeRequest(t, req, http.StatusOK)
161+
}
162+
154163
func testIssueAddComment(t *testing.T, session *TestSession, issueURL, content, status string) int64 {
155164
req := NewRequest(t, "GET", issueURL)
156165
resp := session.MakeRequest(t, req, http.StatusOK)

tests/integration/repo_webhook_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,45 @@ func Test_WebhookIssue(t *testing.T) {
352352
assert.Equal(t, "user2/repo1", payloads[0].Issue.Repo.FullName)
353353
assert.Equal(t, "Title1", payloads[0].Issue.Title)
354354
assert.Equal(t, "Description1", payloads[0].Issue.Body)
355+
assert.Greater(t, payloads[0].Issue.Created.Unix(), int64(0))
356+
assert.Greater(t, payloads[0].Issue.Updated.Unix(), int64(0))
357+
})
358+
}
359+
360+
func Test_WebhookIssueAssign(t *testing.T) {
361+
var payloads []api.PullRequestPayload
362+
var triggeredEvent string
363+
provider := newMockWebhookProvider(func(r *http.Request) {
364+
content, _ := io.ReadAll(r.Body)
365+
var payload api.PullRequestPayload
366+
err := json.Unmarshal(content, &payload)
367+
assert.NoError(t, err)
368+
payloads = append(payloads, payload)
369+
triggeredEvent = "pull_request_assign"
370+
}, http.StatusOK)
371+
defer provider.Close()
372+
373+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
374+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 1})
375+
376+
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
377+
// 1. create a new webhook with special webhook for repo1
378+
session := loginUser(t, "user2")
379+
380+
testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "pull_request_assign")
381+
382+
// 2. trigger the webhook, issue 2 is a pull request
383+
testIssueAssign(t, session, repo1.Link(), 2, user2.ID)
384+
385+
// 3. validate the webhook is triggered
386+
assert.Equal(t, "pull_request_assign", triggeredEvent)
387+
assert.Len(t, payloads, 1)
388+
assert.EqualValues(t, "assigned", payloads[0].Action)
389+
assert.Equal(t, "repo1", payloads[0].PullRequest.Base.Repository.Name)
390+
assert.Equal(t, "user2/repo1", payloads[0].PullRequest.Base.Repository.FullName)
391+
assert.Equal(t, "issue2", payloads[0].PullRequest.Title)
392+
assert.Equal(t, "content for the second issue", payloads[0].PullRequest.Body)
393+
assert.Equal(t, user2.ID, payloads[0].PullRequest.Assignee.ID)
355394
})
356395
}
357396

0 commit comments

Comments
 (0)