Skip to content

WIP: Add "changed files" to pull-request webhook #18217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions modules/structs/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 44 additions & 2 deletions services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 ""
}

Expand Down
12 changes: 12 additions & 0 deletions services/webhook/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down