Skip to content

Commit fc72432

Browse files
Add missing fields to gitea webhook for openproject compatibility.
1 parent 717d0f5 commit fc72432

File tree

7 files changed

+84
-23
lines changed

7 files changed

+84
-23
lines changed

models/issues/pull.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,24 @@ func (pr *PullRequest) GetGitHeadBranchRefName() string {
448448
return fmt.Sprintf("%s%s", git.BranchPrefix, pr.HeadBranch)
449449
}
450450

451+
func (pr *PullRequest) GetReviewCommentsCount(ctx context.Context) int {
452+
opts := FindCommentsOptions{
453+
Type: CommentTypeReview,
454+
IssueID: pr.IssueID,
455+
ReviewID: 0, // TODO: How to find the review ID?
456+
}
457+
conds := opts.ToConds()
458+
if pr.ID == 0 {
459+
conds = conds.And(builder.Eq{"invalidated": false})
460+
}
461+
462+
count, err := db.GetEngine(ctx).Where(conds).Count(new(Comment))
463+
if err != nil {
464+
return 0
465+
}
466+
return int(count)
467+
}
468+
451469
// IsChecking returns true if this pull request is still checking conflict.
452470
func (pr *PullRequest) IsChecking() bool {
453471
return pr.Status == PullRequestStatusChecking

modules/structs/issue.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
type PullRequestMeta struct {
2929
HasMerged bool `json:"merged"`
3030
Merged *time.Time `json:"merged_at"`
31+
HTMLURL string `json:"html_url"`
3132
}
3233

3334
// RepositoryMeta basic repository information

modules/structs/pull.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ 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+
ReviewComments int `json:"review_comments"`
28+
Additions int `json:"additions"`
29+
Deletions int `json:"deletions"`
30+
ChangedFiles int `json:"changed_files"`
2631

2732
HTMLURL string `json:"html_url"`
2833
DiffURL string `json:"diff_url"`

modules/structs/user.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type User struct {
2525
Email string `json:"email"`
2626
// URL to the user's avatar
2727
AvatarURL string `json:"avatar_url"`
28+
// URL to the user's gitea page
29+
HTMLURL string `json:"html_url"`
2830
// User locale
2931
Language string `json:"language"`
3032
// Is the user an administrator

services/convert/issue.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ func toIssue(ctx context.Context, issue *issues_model.Issue, getDownloadURL func
103103
if issue.PullRequest.HasMerged {
104104
apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
105105
}
106+
// Add pr's html url
107+
apiIssue.PullRequest.HTMLURL = issue.HTMLURL()
106108
}
107109
}
108110
if issue.DeadlineUnix != 0 {

services/convert/pull.go

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
user_model "code.gitea.io/gitea/models/user"
1414
"code.gitea.io/gitea/modules/git"
1515
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/setting"
1617
api "code.gitea.io/gitea/modules/structs"
18+
"code.gitea.io/gitea/services/gitdiff"
1719
)
1820

1921
// ToAPIPullRequest assumes following fields have been assigned with valid values:
@@ -50,29 +52,31 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
5052
}
5153

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

7781
AllowMaintainerEdit: pr.AllowMaintainerEdit,
7882

@@ -187,6 +191,34 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
187191
apiPullRequest.Head.Sha = commit.ID.String()
188192
}
189193
}
194+
195+
startCommitID := pr.MergeBase
196+
endCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
197+
if err != nil {
198+
log.Error("GetRefCommitID[%s]: %v", apiPullRequest.Head.Ref, err)
199+
}
200+
201+
maxLines := setting.Git.MaxGitDiffLines
202+
203+
// FIXME: If there are too many files in the repo, may cause some unpredictable issues.
204+
diff, err := gitdiff.GetDiff(ctx, gitRepo,
205+
&gitdiff.DiffOptions{
206+
BeforeCommitID: startCommitID,
207+
AfterCommitID: endCommitID,
208+
SkipTo: "", // ctx.FormString("skip-to"),
209+
MaxLines: maxLines,
210+
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
211+
MaxFiles: -1, // GetDiff() will return all files
212+
WhitespaceBehavior: gitdiff.GetWhitespaceFlag("show-all"),
213+
})
214+
if err != nil {
215+
log.Error("GetDiff: %v", err)
216+
return nil
217+
}
218+
219+
apiPullRequest.Additions = diff.TotalAddition
220+
apiPullRequest.Deletions = diff.TotalDeletion
221+
apiPullRequest.ChangedFiles = diff.NumFiles
190222
}
191223

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

services/convert/user.go

Lines changed: 1 addition & 0 deletions
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,

0 commit comments

Comments
 (0)