Skip to content

Commit b997afd

Browse files
committed
Fix test
1 parent 306922c commit b997afd

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

services/pull/pull.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -151,32 +151,30 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
151151
if err != nil {
152152
return err
153153
}
154-
if len(compareInfo.Commits) == 0 {
155-
return nil
156-
}
157-
158-
data := issues_model.PushActionContent{IsForcePush: false}
159-
data.CommitIDs = make([]string, 0, len(compareInfo.Commits))
160-
for i := len(compareInfo.Commits) - 1; i >= 0; i-- {
161-
data.CommitIDs = append(data.CommitIDs, compareInfo.Commits[i].ID.String())
162-
}
154+
if len(compareInfo.Commits) > 0 {
155+
data := issues_model.PushActionContent{IsForcePush: false}
156+
data.CommitIDs = make([]string, 0, len(compareInfo.Commits))
157+
for i := len(compareInfo.Commits) - 1; i >= 0; i-- {
158+
data.CommitIDs = append(data.CommitIDs, compareInfo.Commits[i].ID.String())
159+
}
163160

164-
dataJSON, err := json.Marshal(data)
165-
if err != nil {
166-
return err
167-
}
161+
dataJSON, err := json.Marshal(data)
162+
if err != nil {
163+
return err
164+
}
168165

169-
ops := &issues_model.CreateCommentOptions{
170-
Type: issues_model.CommentTypePullRequestPush,
171-
Doer: issue.Poster,
172-
Repo: repo,
173-
Issue: pr.Issue,
174-
IsForcePush: false,
175-
Content: string(dataJSON),
176-
}
166+
ops := &issues_model.CreateCommentOptions{
167+
Type: issues_model.CommentTypePullRequestPush,
168+
Doer: issue.Poster,
169+
Repo: repo,
170+
Issue: pr.Issue,
171+
IsForcePush: false,
172+
Content: string(dataJSON),
173+
}
177174

178-
if _, err = issues_model.CreateComment(ctx, ops); err != nil {
179-
return err
175+
if _, err = issues_model.CreateComment(ctx, ops); err != nil {
176+
return err
177+
}
180178
}
181179

182180
// review request from CodeOwners

tests/integration/repo_webhook_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515

1616
auth_model "code.gitea.io/gitea/models/auth"
17+
"code.gitea.io/gitea/models/perm"
1718
"code.gitea.io/gitea/models/repo"
1819
"code.gitea.io/gitea/models/unittest"
1920
user_model "code.gitea.io/gitea/models/user"
@@ -678,23 +679,30 @@ func Test_WebhookPullRequest(t *testing.T) {
678679
}, http.StatusOK)
679680
defer provider.Close()
680681

682+
testCtx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeAll)
683+
// add user4 as collabrator so that it can be a reviewer
684+
doAPIAddCollaborator(testCtx, "user4", perm.AccessModeWrite)(t)
685+
681686
// 1. create a new webhook with special webhook for repo1
682-
session := loginUser(t, "user2")
687+
sessionUser2 := loginUser(t, "user2")
688+
sessionUser4 := loginUser(t, "user4")
683689

684-
testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "pull_request")
690+
// ignore the possible review_requested event to keep the test deterministic
691+
testAPICreateWebhookForRepo(t, sessionUser2, "user2", "repo1", provider.URL(), "pull_request_only")
692+
693+
testAPICreateBranch(t, sessionUser2, "user2", "repo1", "master", "master2", http.StatusCreated)
685694

686-
testAPICreateBranch(t, session, "user2", "repo1", "master", "master2", http.StatusCreated)
687695
// 2. trigger the webhook
688696
repo1 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 1})
689-
testPullCreateDirectly(t, session, createPullRequestOptions{
697+
testPullCreateDirectly(t, sessionUser4, createPullRequestOptions{
690698
BaseRepoOwner: repo1.OwnerName,
691699
BaseRepoName: repo1.Name,
692700
BaseBranch: repo1.DefaultBranch,
693701
HeadRepoOwner: "",
694702
HeadRepoName: "",
695703
HeadBranch: "master2",
696704
Title: "first pull request",
697-
ReviewerIDs: "1",
705+
ReviewerIDs: "2", // add user2 as reviewer
698706
})
699707

700708
// 3. validate the webhook is triggered
@@ -708,7 +716,7 @@ func Test_WebhookPullRequest(t *testing.T) {
708716
assert.Equal(t, 0, *payloads[0].PullRequest.ChangedFiles)
709717
assert.Equal(t, 0, *payloads[0].PullRequest.Deletions)
710718
assert.Len(t, payloads[0].PullRequest.RequestedReviewers, 1)
711-
assert.Equal(t, int64(1), payloads[0].PullRequest.RequestedReviewers[0].ID)
719+
assert.Equal(t, int64(2), payloads[0].PullRequest.RequestedReviewers[0].ID)
712720
})
713721
}
714722

0 commit comments

Comments
 (0)