diff --git a/modules/structs/hook.go b/modules/structs/hook.go index cd91d4bc462a4..e12c0707bae3a 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -385,15 +385,16 @@ type ChangesPayload struct { // PullRequestPayload represents a payload information of pull request event. type PullRequestPayload struct { - Action HookIssueAction `json:"action"` - Index int64 `json:"number"` - Changes *ChangesPayload `json:"changes,omitempty"` - PullRequest *PullRequest `json:"pull_request"` - RequestedReviewer *User `json:"requested_reviewer"` - Repository *Repository `json:"repository"` - Sender *User `json:"sender"` - CommitID string `json:"commit_id"` - Review *ReviewPayload `json:"review"` + Action HookIssueAction `json:"action"` + Index int64 `json:"number"` + Changes *ChangesPayload `json:"changes,omitempty"` + PullRequest *PullRequest `json:"pull_request"` + RequestedReviewer *User `json:"requested_reviewer"` + Repository *Repository `json:"repository"` + Sender *User `json:"sender"` + CommitID string `json:"commit_id"` + Commits []*PayloadCommit `json:"commits"` + Review *ReviewPayload `json:"review"` } // JSONPayload FIXME diff --git a/services/pull/pull.go b/services/pull/pull.go index 8730b9684dd2a..930d119b76437 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -583,6 +583,48 @@ func CloseRepoBranchesPulls(ctx context.Context, doer *user_model.User, repo *re var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+[ \t]*:[^\n]+\n*(?:[ \t]+[^\n]+\n*)*)+$`) +// CommitsBetweenLimit returns a list that contains at most limit commits skipping the first skip commits between [before, last] +func CommitsBetweenLimit(ctx context.Context, pr *models.PullRequest, limit, skip int) ([]*git.Commit, error) { + if pr.HeadRepo == nil { + var err error + pr.HeadRepo, err = repo_model.GetRepositoryByID(pr.HeadRepoID) + if err != nil { + return nil, fmt.Errorf("GetRepositoryById[%d]: %v", pr.HeadRepoID, err) + } + } + + gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) + if err != nil { + return nil, fmt.Errorf("Unable to open head repository: Error: %v", err) + } + defer gitRepo.Close() + + var headCommit *git.Commit + if pr.Flow == models.PullRequestFlowGithub { + headCommit, err = gitRepo.GetBranchCommit(pr.HeadBranch) + } else { + pr.HeadCommitID, err = gitRepo.GetRefCommitID(pr.GetGitRefName()) + if err != nil { + return nil, fmt.Errorf("Unable to get head commit: %s Error: %v", pr.GetGitRefName(), err) + } + headCommit, err = gitRepo.GetCommit(pr.HeadCommitID) + } + if err != nil { + return nil, fmt.Errorf("Unable to get head commit: %s Error: %v", pr.HeadBranch, err) + } + + mergeBase, err := gitRepo.GetCommit(pr.MergeBase) + if err != nil { + return nil, fmt.Errorf("Unable to get merge base commit: %s Error: %v", pr.MergeBase, err) + } + + commits, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, skip) + if err != nil { + return nil, fmt.Errorf("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err) + } + return commits, nil +} + // GetSquashMergeCommitMessages returns the commit messages between head and merge base (if there is one) func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequest) string { if err := pr.LoadIssue(ctx); err != nil { @@ -635,9 +677,9 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ limit := setting.Repository.PullRequest.DefaultMergeMessageCommitsLimit - commits, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, 0) + commits, err := CommitsBetweenLimit(context.TODO(), pr, limit, 0) if err != nil { - log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err) + log.Error("CommitsBetweenLimit: %v", err) return "" } diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index 23080a5a3514c..298ea88cce3e2 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -21,6 +21,7 @@ import ( api "code.gitea.io/gitea/modules/structs" webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/services/convert" + pull_service "code.gitea.io/gitea/services/pull" ) func init() { @@ -304,11 +305,17 @@ func (m *webhookNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues return } + commits, err := pull_service.CommitsBetweenLimit(context.TODO(), pull, 100, 0) // TODO: limit is hardcoded + if err != nil { + + } + permission, _ := access_model.GetUserRepoPermission(ctx, pull.Issue.Repo, pull.Issue.Poster) if err := PrepareWebhooks(ctx, EventSource{Repository: pull.Issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueOpened, Index: pull.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pull, nil), + Commits: nil, // TODO Repository: convert.ToRepo(ctx, pull.Issue.Repo, permission), Sender: convert.ToUser(ctx, pull.Issue.Poster, nil), }); err != nil { @@ -642,6 +649,7 @@ func (*webhookNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m apiPullRequest := &api.PullRequestPayload{ Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), + Commits: nil, // TODO Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission), Sender: convert.ToUser(ctx, doer, nil), Action: api.HookIssueClosed, @@ -670,6 +678,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex }, }, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), + Commits: nil, // TODO Repository: convert.ToRepo(ctx, issue.Repo, mode), Sender: convert.ToUser(ctx, doer, nil), }); err != nil { @@ -707,6 +716,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issue Action: api.HookIssueReviewed, Index: review.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), + Commits: nil, // TODO Repository: convert.ToRepo(ctx, review.Issue.Repo, permission), Sender: convert.ToUser(ctx, review.Reviewer, nil), Review: &api.ReviewPayload{ @@ -731,6 +741,7 @@ func (m *webhookNotifier) NotifyPullRequestReviewRequest(ctx context.Context, do apiPullRequest := &api.PullRequestPayload{ Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), + Commits: nil, // TODO RequestedReviewer: convert.ToUser(ctx, reviewer, nil), Repository: convert.ToRepo(ctx, issue.Repo, permission), Sender: convert.ToUser(ctx, doer, nil), @@ -776,6 +787,7 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe Action: api.HookIssueSynchronized, Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, pr, nil), + Commits: nil, // TODO Repository: convert.ToRepo(ctx, pr.Issue.Repo, access_model.Permission{AccessMode: perm.AccessModeOwner}), Sender: convert.ToUser(ctx, doer, nil), }); err != nil {