From b997afd548ba910884fdc3297f8d46c19e25ae2b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 22 Aug 2025 21:18:10 -0700 Subject: [PATCH] Fix test --- services/pull/pull.go | 44 ++++++++++++-------------- tests/integration/repo_webhook_test.go | 20 ++++++++---- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/services/pull/pull.go b/services/pull/pull.go index f80f290333..3af9c9318e 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -151,32 +151,30 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error { if err != nil { return err } - if len(compareInfo.Commits) == 0 { - return nil - } + if len(compareInfo.Commits) > 0 { + data := issues_model.PushActionContent{IsForcePush: false} + data.CommitIDs = make([]string, 0, len(compareInfo.Commits)) + for i := len(compareInfo.Commits) - 1; i >= 0; i-- { + data.CommitIDs = append(data.CommitIDs, compareInfo.Commits[i].ID.String()) + } - data := issues_model.PushActionContent{IsForcePush: false} - data.CommitIDs = make([]string, 0, len(compareInfo.Commits)) - for i := len(compareInfo.Commits) - 1; i >= 0; i-- { - data.CommitIDs = append(data.CommitIDs, compareInfo.Commits[i].ID.String()) - } + dataJSON, err := json.Marshal(data) + if err != nil { + return err + } - dataJSON, err := json.Marshal(data) - if err != nil { - return err - } + ops := &issues_model.CreateCommentOptions{ + Type: issues_model.CommentTypePullRequestPush, + Doer: issue.Poster, + Repo: repo, + Issue: pr.Issue, + IsForcePush: false, + Content: string(dataJSON), + } - ops := &issues_model.CreateCommentOptions{ - Type: issues_model.CommentTypePullRequestPush, - Doer: issue.Poster, - Repo: repo, - Issue: pr.Issue, - IsForcePush: false, - Content: string(dataJSON), - } - - if _, err = issues_model.CreateComment(ctx, ops); err != nil { - return err + if _, err = issues_model.CreateComment(ctx, ops); err != nil { + return err + } } // review request from CodeOwners diff --git a/tests/integration/repo_webhook_test.go b/tests/integration/repo_webhook_test.go index a5196debdf..2d1565b797 100644 --- a/tests/integration/repo_webhook_test.go +++ b/tests/integration/repo_webhook_test.go @@ -14,6 +14,7 @@ import ( "testing" auth_model "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -678,15 +679,22 @@ func Test_WebhookPullRequest(t *testing.T) { }, http.StatusOK) defer provider.Close() + testCtx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeAll) + // add user4 as collabrator so that it can be a reviewer + doAPIAddCollaborator(testCtx, "user4", perm.AccessModeWrite)(t) + // 1. create a new webhook with special webhook for repo1 - session := loginUser(t, "user2") + sessionUser2 := loginUser(t, "user2") + sessionUser4 := loginUser(t, "user4") - testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "pull_request") + // ignore the possible review_requested event to keep the test deterministic + testAPICreateWebhookForRepo(t, sessionUser2, "user2", "repo1", provider.URL(), "pull_request_only") + + testAPICreateBranch(t, sessionUser2, "user2", "repo1", "master", "master2", http.StatusCreated) - testAPICreateBranch(t, session, "user2", "repo1", "master", "master2", http.StatusCreated) // 2. trigger the webhook repo1 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 1}) - testPullCreateDirectly(t, session, createPullRequestOptions{ + testPullCreateDirectly(t, sessionUser4, createPullRequestOptions{ BaseRepoOwner: repo1.OwnerName, BaseRepoName: repo1.Name, BaseBranch: repo1.DefaultBranch, @@ -694,7 +702,7 @@ func Test_WebhookPullRequest(t *testing.T) { HeadRepoName: "", HeadBranch: "master2", Title: "first pull request", - ReviewerIDs: "1", + ReviewerIDs: "2", // add user2 as reviewer }) // 3. validate the webhook is triggered @@ -708,7 +716,7 @@ func Test_WebhookPullRequest(t *testing.T) { assert.Equal(t, 0, *payloads[0].PullRequest.ChangedFiles) assert.Equal(t, 0, *payloads[0].PullRequest.Deletions) assert.Len(t, payloads[0].PullRequest.RequestedReviewers, 1) - assert.Equal(t, int64(1), payloads[0].PullRequest.RequestedReviewers[0].ID) + assert.Equal(t, int64(2), payloads[0].PullRequest.RequestedReviewers[0].ID) }) }