Skip to content

Commit 570ed6b

Browse files
Chief-DetektorGiteaBot
authored andcommitted
Make gitea webhooks openproject compatible (go-gitea#28435)
This PR adds some fields to the gitea webhook payload that [openproject](https://www.openproject.org/) expects to exists in order to process the webhooks. These fields do exists in Github's webhook payload so adding them makes Gitea's native webhook more compatible towards Github's.
1 parent 7d56ee3 commit 570ed6b

File tree

8 files changed

+102
-23
lines changed

8 files changed

+102
-23
lines changed

models/issues/pull.go

+15
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,21 @@ func (pr *PullRequest) GetGitHeadBranchRefName() string {
430430
return fmt.Sprintf("%s%s", git.BranchPrefix, pr.HeadBranch)
431431
}
432432

433+
// GetReviewCommentsCount returns the number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)
434+
func (pr *PullRequest) GetReviewCommentsCount(ctx context.Context) int {
435+
opts := FindCommentsOptions{
436+
Type: CommentTypeReview,
437+
IssueID: pr.IssueID,
438+
}
439+
conds := opts.ToConds()
440+
441+
count, err := db.GetEngine(ctx).Where(conds).Count(new(Comment))
442+
if err != nil {
443+
return 0
444+
}
445+
return int(count)
446+
}
447+
433448
// IsChecking returns true if this pull request is still checking conflict.
434449
func (pr *PullRequest) IsChecking() bool {
435450
return pr.Status == PullRequestStatusChecking

modules/structs/issue.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type PullRequestMeta struct {
3030
HasMerged bool `json:"merged"`
3131
Merged *time.Time `json:"merged_at"`
3232
IsWorkInProgress bool `json:"draft"`
33+
HTMLURL string `json:"html_url"`
3334
}
3435

3536
// RepositoryMeta basic repository information

modules/structs/pull.go

+6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ type PullRequest struct {
2121
Assignees []*User `json:"assignees"`
2222
RequestedReviewers []*User `json:"requested_reviewers"`
2323
State StateType `json:"state"`
24+
Draft bool `json:"draft"`
2425
IsLocked bool `json:"is_locked"`
2526
Comments int `json:"comments"`
27+
// number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)
28+
ReviewComments int `json:"review_comments"`
29+
Additions int `json:"additions"`
30+
Deletions int `json:"deletions"`
31+
ChangedFiles int `json:"changed_files"`
2632

2733
HTMLURL string `json:"html_url"`
2834
DiffURL string `json:"diff_url"`

modules/structs/user.go

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type User struct {
2828
Email string `json:"email"`
2929
// URL to the user's avatar
3030
AvatarURL string `json:"avatar_url"`
31+
// URL to the user's gitea page
32+
HTMLURL string `json:"html_url"`
3133
// User locale
3234
Language string `json:"language"`
3335
// Is the user an administrator

services/convert/issue.go

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ func toIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Iss
104104
if issue.PullRequest.HasMerged {
105105
apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
106106
}
107+
// Add pr's html url
108+
apiIssue.PullRequest.HTMLURL = issue.HTMLURL()
107109
}
108110
}
109111
if issue.DeadlineUnix != 0 {

services/convert/pull.go

+41-23
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,31 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
5151
}
5252

5353
apiPullRequest := &api.PullRequest{
54-
ID: pr.ID,
55-
URL: pr.Issue.HTMLURL(),
56-
Index: pr.Index,
57-
Poster: apiIssue.Poster,
58-
Title: apiIssue.Title,
59-
Body: apiIssue.Body,
60-
Labels: apiIssue.Labels,
61-
Milestone: apiIssue.Milestone,
62-
Assignee: apiIssue.Assignee,
63-
Assignees: apiIssue.Assignees,
64-
State: apiIssue.State,
65-
IsLocked: apiIssue.IsLocked,
66-
Comments: apiIssue.Comments,
67-
HTMLURL: pr.Issue.HTMLURL(),
68-
DiffURL: pr.Issue.DiffURL(),
69-
PatchURL: pr.Issue.PatchURL(),
70-
HasMerged: pr.HasMerged,
71-
MergeBase: pr.MergeBase,
72-
Mergeable: pr.Mergeable(ctx),
73-
Deadline: apiIssue.Deadline,
74-
Created: pr.Issue.CreatedUnix.AsTimePtr(),
75-
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
76-
PinOrder: apiIssue.PinOrder,
54+
ID: pr.ID,
55+
URL: pr.Issue.HTMLURL(),
56+
Index: pr.Index,
57+
Poster: apiIssue.Poster,
58+
Title: apiIssue.Title,
59+
Body: apiIssue.Body,
60+
Labels: apiIssue.Labels,
61+
Milestone: apiIssue.Milestone,
62+
Assignee: apiIssue.Assignee,
63+
Assignees: apiIssue.Assignees,
64+
State: apiIssue.State,
65+
Draft: pr.IsWorkInProgress(ctx),
66+
IsLocked: apiIssue.IsLocked,
67+
Comments: apiIssue.Comments,
68+
ReviewComments: pr.GetReviewCommentsCount(ctx),
69+
HTMLURL: pr.Issue.HTMLURL(),
70+
DiffURL: pr.Issue.DiffURL(),
71+
PatchURL: pr.Issue.PatchURL(),
72+
HasMerged: pr.HasMerged,
73+
MergeBase: pr.MergeBase,
74+
Mergeable: pr.Mergeable(ctx),
75+
Deadline: apiIssue.Deadline,
76+
Created: pr.Issue.CreatedUnix.AsTimePtr(),
77+
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
78+
PinOrder: apiIssue.PinOrder,
7779

7880
AllowMaintainerEdit: pr.AllowMaintainerEdit,
7981

@@ -168,6 +170,12 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
168170
return nil
169171
}
170172

173+
// Outer scope variables to be used in diff calculation
174+
var (
175+
startCommitID string
176+
endCommitID string
177+
)
178+
171179
if git.IsErrBranchNotExist(err) {
172180
headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
173181
if err != nil && !git.IsErrNotExist(err) {
@@ -176,6 +184,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
176184
}
177185
if err == nil {
178186
apiPullRequest.Head.Sha = headCommitID
187+
endCommitID = headCommitID
179188
}
180189
} else {
181190
commit, err := headBranch.GetCommit()
@@ -186,8 +195,17 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
186195
if err == nil {
187196
apiPullRequest.Head.Ref = pr.HeadBranch
188197
apiPullRequest.Head.Sha = commit.ID.String()
198+
endCommitID = commit.ID.String()
189199
}
190200
}
201+
202+
// Calculate diff
203+
startCommitID = pr.MergeBase
204+
205+
apiPullRequest.ChangedFiles, apiPullRequest.Additions, apiPullRequest.Deletions, err = gitRepo.GetDiffShortStat(startCommitID, endCommitID)
206+
if err != nil {
207+
log.Error("GetDiffShortStat: %v", err)
208+
}
191209
}
192210

193211
if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {

services/convert/user.go

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func toUser(ctx context.Context, user *user_model.User, signed, authed bool) *ap
5353
FullName: user.FullName,
5454
Email: user.GetPlaceholderEmail(),
5555
AvatarURL: user.AvatarLink(ctx),
56+
HTMLURL: user.HTMLURL(),
5657
Created: user.CreatedUnix.AsTime(),
5758
Restricted: user.IsRestricted,
5859
Location: user.Location,

templates/swagger/v1_json.tmpl

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)