0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-12-08 13:34:51 +01:00
This commit is contained in:
Lunny Xiao 2025-08-22 21:18:10 -07:00
parent 306922cacf
commit b997afd548
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 35 additions and 29 deletions

View File

@ -151,32 +151,30 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
if err != nil { if err != nil {
return err return err
} }
if len(compareInfo.Commits) == 0 { if len(compareInfo.Commits) > 0 {
return nil 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} dataJSON, err := json.Marshal(data)
data.CommitIDs = make([]string, 0, len(compareInfo.Commits)) if err != nil {
for i := len(compareInfo.Commits) - 1; i >= 0; i-- { return err
data.CommitIDs = append(data.CommitIDs, compareInfo.Commits[i].ID.String()) }
}
dataJSON, err := json.Marshal(data) ops := &issues_model.CreateCommentOptions{
if err != nil { Type: issues_model.CommentTypePullRequestPush,
return err Doer: issue.Poster,
} Repo: repo,
Issue: pr.Issue,
IsForcePush: false,
Content: string(dataJSON),
}
ops := &issues_model.CreateCommentOptions{ if _, err = issues_model.CreateComment(ctx, ops); err != nil {
Type: issues_model.CommentTypePullRequestPush, return err
Doer: issue.Poster, }
Repo: repo,
Issue: pr.Issue,
IsForcePush: false,
Content: string(dataJSON),
}
if _, err = issues_model.CreateComment(ctx, ops); err != nil {
return err
} }
// review request from CodeOwners // review request from CodeOwners

View File

@ -14,6 +14,7 @@ import (
"testing" "testing"
auth_model "code.gitea.io/gitea/models/auth" 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/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
@ -678,15 +679,22 @@ func Test_WebhookPullRequest(t *testing.T) {
}, http.StatusOK) }, http.StatusOK)
defer provider.Close() 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 // 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 // 2. trigger the webhook
repo1 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 1}) repo1 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 1})
testPullCreateDirectly(t, session, createPullRequestOptions{ testPullCreateDirectly(t, sessionUser4, createPullRequestOptions{
BaseRepoOwner: repo1.OwnerName, BaseRepoOwner: repo1.OwnerName,
BaseRepoName: repo1.Name, BaseRepoName: repo1.Name,
BaseBranch: repo1.DefaultBranch, BaseBranch: repo1.DefaultBranch,
@ -694,7 +702,7 @@ func Test_WebhookPullRequest(t *testing.T) {
HeadRepoName: "", HeadRepoName: "",
HeadBranch: "master2", HeadBranch: "master2",
Title: "first pull request", Title: "first pull request",
ReviewerIDs: "1", ReviewerIDs: "2", // add user2 as reviewer
}) })
// 3. validate the webhook is triggered // 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.ChangedFiles)
assert.Equal(t, 0, *payloads[0].PullRequest.Deletions) assert.Equal(t, 0, *payloads[0].PullRequest.Deletions)
assert.Len(t, payloads[0].PullRequest.RequestedReviewers, 1) 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)
}) })
} }