From ea1157f6846ed50c64cc1fc47223107e2f7bef7b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 8 Jan 2022 22:03:02 +0100 Subject: [PATCH] =?UTF-8?q?can=20we=20please=20get=20#17868=20in=20?= =?UTF-8?q?=F0=9F=99=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/git/repo_commit.go | 2 +- modules/notification/webhook/webhook.go | 13 +++++ modules/structs/hook.go | 15 +++--- services/pull/pull.go | 66 ++++++++++++++++--------- 4 files changed, 65 insertions(+), 31 deletions(-) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 896ad345187fb..b6b8752910c2e 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -283,7 +283,7 @@ func (repo *Repository) CommitsBetween(last, before *Commit) ([]*Commit, error) return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout)) } -// CommitsBetweenLimit returns a list that contains at most limit commits skipping the first skip commits between [before, last) +// CommitsBetweenLimit returns a list that contains at most limit commits skipping the first skip commits between [before, last] func (repo *Repository) CommitsBetweenLimit(last, before *Commit, limit, skip int) ([]*Commit, error) { var stdout []byte var err error diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index 16ec31318b1d0..adfe6c6df2566 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -5,6 +5,8 @@ package webhook import ( + "context" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" @@ -18,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + pull_service "code.gitea.io/gitea/services/pull" webhook_services "code.gitea.io/gitea/services/webhook" ) @@ -302,11 +305,17 @@ func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest, mention return } + commits, err := pull_service.CommitsBetweenLimit(context.TODO(), pull, 100, 0) //TODO: limit is hardcoded + if err != nil { + + } + mode, _ := models.AccessLevel(pull.Issue.Poster, pull.Issue.Repo) if err := webhook_services.PrepareWebhooks(pull.Issue.Repo, webhook.HookEventPullRequest, &api.PullRequestPayload{ Action: api.HookIssueOpened, Index: pull.Issue.Index, PullRequest: convert.ToAPIPullRequest(pull, nil), + Commits: nil, // TODO Repository: convert.ToRepo(pull.Issue.Repo, mode), Sender: convert.ToUser(pull.Issue.Poster, nil), }); err != nil { @@ -615,6 +624,7 @@ func (*webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *use apiPullRequest := &api.PullRequestPayload{ Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(pr, nil), + Commits: nil, // TODO Repository: convert.ToRepo(pr.Issue.Repo, mode), Sender: convert.ToUser(doer, nil), Action: api.HookIssueClosed, @@ -648,6 +658,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(doer *user_model.U }, }, PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil), + Commits: nil, // TODO Repository: convert.ToRepo(issue.Repo, mode), Sender: convert.ToUser(doer, nil), }) @@ -687,6 +698,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review Action: api.HookIssueReviewed, Index: review.Issue.Index, PullRequest: convert.ToAPIPullRequest(pr, nil), + Commits: nil, // TODO Repository: convert.ToRepo(review.Issue.Repo, mode), Sender: convert.ToUser(review.Reviewer, nil), Review: &api.ReviewPayload{ @@ -742,6 +754,7 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *user_model.User, p Action: api.HookIssueSynchronized, Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(pr, nil), + Commits: nil, // TODO Repository: convert.ToRepo(pr.Issue.Repo, perm.AccessModeNone), Sender: convert.ToUser(doer, nil), }); err != nil { diff --git a/modules/structs/hook.go b/modules/structs/hook.go index 163fb5c94d619..78e593e084603 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -378,13 +378,14 @@ 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"` - Repository *Repository `json:"repository"` - Sender *User `json:"sender"` - Review *ReviewPayload `json:"review"` + Action HookIssueAction `json:"action"` + Index int64 `json:"number"` + Changes *ChangesPayload `json:"changes,omitempty"` + PullRequest *PullRequest `json:"pull_request"` + Commits []*PayloadCommit `json:"commits"` + Repository *Repository `json:"repository"` + Sender *User `json:"sender"` + Review *ReviewPayload `json:"review"` } // JSONPayload FIXME diff --git a/services/pull/pull.go b/services/pull/pull.go index 9103da07f4998..d11dd1d314817 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -562,31 +562,19 @@ func CloseRepoBranchesPulls(doer *user_model.User, repo *repo_model.Repository) var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+[ \t]*:[^\n]+\n*(?:[ \t]+[^\n]+\n*)*)+$`) -// GetSquashMergeCommitMessages returns the commit messages between head and merge base (if there is one) -func GetSquashMergeCommitMessages(pr *models.PullRequest) string { - if err := pr.LoadIssue(); err != nil { - log.Error("Cannot load issue %d for PR id %d: Error: %v", pr.IssueID, pr.ID, err) - return "" - } - - if err := pr.Issue.LoadPoster(); err != nil { - log.Error("Cannot load poster %d for pr id %d, index %d Error: %v", pr.Issue.PosterID, pr.ID, pr.Index, err) - return "" - } - +// 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 { - log.Error("GetRepositoryById[%d]: %v", pr.HeadRepoID, err) - return "" + return nil, fmt.Errorf("GetRepositoryById[%d]: %v", pr.HeadRepoID, err) } } gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) if err != nil { - log.Error("Unable to open head repository: Error: %v", err) - return "" + return nil, fmt.Errorf("Unable to open head repository: Error: %v", err) } defer gitRepo.Close() @@ -596,27 +584,59 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string { } else { pr.HeadCommitID, err = gitRepo.GetRefCommitID(pr.GetGitRefName()) if err != nil { - log.Error("Unable to get head commit: %s Error: %v", pr.GetGitRefName(), err) - return "" + return nil, fmt.Errorf("Unable to get head commit: %s Error: %v", pr.GetGitRefName(), err) } headCommit, err = gitRepo.GetCommit(pr.HeadCommitID) } if err != nil { - log.Error("Unable to get head commit: %s Error: %v", pr.HeadBranch, err) - return "" + return nil, fmt.Errorf("Unable to get head commit: %s Error: %v", pr.HeadBranch, err) } mergeBase, err := gitRepo.GetCommit(pr.MergeBase) if err != nil { - log.Error("Unable to get merge base commit: %s Error: %v", pr.MergeBase, err) + 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(pr *models.PullRequest) string { + if err := pr.LoadIssue(); err != nil { + log.Error("Cannot load issue %d for PR id %d: Error: %v", pr.IssueID, pr.ID, err) + return "" + } + + if err := pr.Issue.LoadPoster(); err != nil { + log.Error("Cannot load poster %d for pr id %d, index %d Error: %v", pr.Issue.PosterID, pr.ID, pr.Index, err) + return "" + } + + if pr.HeadRepo == nil { + var err error + pr.HeadRepo, err = repo_model.GetRepositoryByID(pr.HeadRepoID) + if err != nil { + log.Error("GetRepositoryById[%d]: %v", pr.HeadRepoID, err) + return "" + } + } + + gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) + if err != nil { + log.Error("Unable to open head repository: Error: %v", err) return "" } + defer gitRepo.Close() 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 "" }