From 6b2157ce3d083df358929abde8298927ff720f94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Rosenhammer?= Date: Mon, 11 Dec 2023 14:22:39 +0100 Subject: [PATCH 1/9] Add missing fields to gitea webhook for openproject compatibility. --- models/issues/pull.go | 18 +++++++++ modules/structs/issue.go | 1 + modules/structs/pull.go | 5 +++ modules/structs/user.go | 2 + services/convert/issue.go | 2 + services/convert/pull.go | 78 +++++++++++++++++++++++++++------------ services/convert/user.go | 1 + 7 files changed, 84 insertions(+), 23 deletions(-) diff --git a/models/issues/pull.go b/models/issues/pull.go index 34bea921a0d26..a1f7b3c412615 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -430,6 +430,24 @@ func (pr *PullRequest) GetGitHeadBranchRefName() string { return fmt.Sprintf("%s%s", git.BranchPrefix, pr.HeadBranch) } +func (pr *PullRequest) GetReviewCommentsCount(ctx context.Context) int { + opts := FindCommentsOptions{ + Type: CommentTypeReview, + IssueID: pr.IssueID, + ReviewID: 0, // TODO: How to find the review ID? + } + conds := opts.ToConds() + if pr.ID == 0 { + conds = conds.And(builder.Eq{"invalidated": false}) + } + + count, err := db.GetEngine(ctx).Where(conds).Count(new(Comment)) + if err != nil { + return 0 + } + return int(count) +} + // IsChecking returns true if this pull request is still checking conflict. func (pr *PullRequest) IsChecking() bool { return pr.Status == PullRequestStatusChecking diff --git a/modules/structs/issue.go b/modules/structs/issue.go index 1aec5cc6b86c8..450ad7a5addba 100644 --- a/modules/structs/issue.go +++ b/modules/structs/issue.go @@ -28,6 +28,7 @@ const ( type PullRequestMeta struct { HasMerged bool `json:"merged"` Merged *time.Time `json:"merged_at"` + HTMLURL string `json:"html_url"` } // RepositoryMeta basic repository information diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 05a8d596338bb..f6b498df009c9 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -21,8 +21,13 @@ type PullRequest struct { Assignees []*User `json:"assignees"` RequestedReviewers []*User `json:"requested_reviewers"` State StateType `json:"state"` + Draft bool `json:"draft"` IsLocked bool `json:"is_locked"` Comments int `json:"comments"` + ReviewComments int `json:"review_comments"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + ChangedFiles int `json:"changed_files"` HTMLURL string `json:"html_url"` DiffURL string `json:"diff_url"` diff --git a/modules/structs/user.go b/modules/structs/user.go index 0df67894b0397..b13dcb184c09f 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -25,6 +25,8 @@ type User struct { Email string `json:"email"` // URL to the user's avatar AvatarURL string `json:"avatar_url"` + // URL to the user's gitea page + HTMLURL string `json:"html_url"` // User locale Language string `json:"language"` // Is the user an administrator diff --git a/services/convert/issue.go b/services/convert/issue.go index 39d785e108332..8fd57106fb67c 100644 --- a/services/convert/issue.go +++ b/services/convert/issue.go @@ -103,6 +103,8 @@ func toIssue(ctx context.Context, issue *issues_model.Issue, getDownloadURL func if issue.PullRequest.HasMerged { apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr() } + // Add pr's html url + apiIssue.PullRequest.HTMLURL = issue.HTMLURL() } } if issue.DeadlineUnix != 0 { diff --git a/services/convert/pull.go b/services/convert/pull.go index 7eebe20426c31..0e5a79d458e96 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -13,7 +13,9 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/services/gitdiff" ) // 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 } apiPullRequest := &api.PullRequest{ - ID: pr.ID, - URL: pr.Issue.HTMLURL(), - Index: pr.Index, - Poster: apiIssue.Poster, - Title: apiIssue.Title, - Body: apiIssue.Body, - Labels: apiIssue.Labels, - Milestone: apiIssue.Milestone, - Assignee: apiIssue.Assignee, - Assignees: apiIssue.Assignees, - State: apiIssue.State, - IsLocked: apiIssue.IsLocked, - Comments: apiIssue.Comments, - HTMLURL: pr.Issue.HTMLURL(), - DiffURL: pr.Issue.DiffURL(), - PatchURL: pr.Issue.PatchURL(), - HasMerged: pr.HasMerged, - MergeBase: pr.MergeBase, - Mergeable: pr.Mergeable(ctx), - Deadline: apiIssue.Deadline, - Created: pr.Issue.CreatedUnix.AsTimePtr(), - Updated: pr.Issue.UpdatedUnix.AsTimePtr(), - PinOrder: apiIssue.PinOrder, + ID: pr.ID, + URL: pr.Issue.HTMLURL(), + Index: pr.Index, + Poster: apiIssue.Poster, + Title: apiIssue.Title, + Body: apiIssue.Body, + Labels: apiIssue.Labels, + Milestone: apiIssue.Milestone, + Assignee: apiIssue.Assignee, + Assignees: apiIssue.Assignees, + State: apiIssue.State, + Draft: pr.IsWorkInProgress(ctx), + IsLocked: apiIssue.IsLocked, + Comments: apiIssue.Comments, + ReviewComments: pr.GetReviewCommentsCount(ctx), + HTMLURL: pr.Issue.HTMLURL(), + DiffURL: pr.Issue.DiffURL(), + PatchURL: pr.Issue.PatchURL(), + HasMerged: pr.HasMerged, + MergeBase: pr.MergeBase, + Mergeable: pr.Mergeable(ctx), + Deadline: apiIssue.Deadline, + Created: pr.Issue.CreatedUnix.AsTimePtr(), + Updated: pr.Issue.UpdatedUnix.AsTimePtr(), + PinOrder: apiIssue.PinOrder, AllowMaintainerEdit: pr.AllowMaintainerEdit, @@ -187,6 +191,34 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u apiPullRequest.Head.Sha = commit.ID.String() } } + + startCommitID := pr.MergeBase + endCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref) + if err != nil { + log.Error("GetRefCommitID[%s]: %v", apiPullRequest.Head.Ref, err) + } + + maxLines := setting.Git.MaxGitDiffLines + + // FIXME: If there are too many files in the repo, may cause some unpredictable issues. + diff, err := gitdiff.GetDiff(ctx, gitRepo, + &gitdiff.DiffOptions{ + BeforeCommitID: startCommitID, + AfterCommitID: endCommitID, + SkipTo: "", // ctx.FormString("skip-to"), + MaxLines: maxLines, + MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, + MaxFiles: -1, // GetDiff() will return all files + WhitespaceBehavior: gitdiff.GetWhitespaceFlag("show-all"), + }) + if err != nil { + log.Error("GetDiff: %v", err) + return nil + } + + apiPullRequest.Additions = diff.TotalAddition + apiPullRequest.Deletions = diff.TotalDeletion + apiPullRequest.ChangedFiles = diff.NumFiles } if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 { diff --git a/services/convert/user.go b/services/convert/user.go index 3521dd2f905c3..3006a3506189d 100644 --- a/services/convert/user.go +++ b/services/convert/user.go @@ -53,6 +53,7 @@ func toUser(ctx context.Context, user *user_model.User, signed, authed bool) *ap FullName: user.FullName, Email: user.GetPlaceholderEmail(), AvatarURL: user.AvatarLink(ctx), + HTMLURL: user.HTMLURL(), Created: user.CreatedUnix.AsTime(), Restricted: user.IsRestricted, Location: user.Location, From 99487dd10f989fc5c59d8119a65e471e5452b654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Rosenhammer?= Date: Tue, 12 Dec 2023 16:17:51 +0100 Subject: [PATCH 2/9] Update swagger files. --- templates/swagger/v1_json.tmpl | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index de3bc331f160b..842d369e035e5 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -21444,6 +21444,11 @@ "description": "PullRequest represents a pull request", "type": "object", "properties": { + "additions": { + "type": "integer", + "format": "int64", + "x-go-name": "Additions" + }, "allow_maintainer_edit": { "type": "boolean", "x-go-name": "AllowMaintainerEdit" @@ -21465,6 +21470,11 @@ "type": "string", "x-go-name": "Body" }, + "changed_files": { + "type": "integer", + "format": "int64", + "x-go-name": "ChangedFiles" + }, "closed_at": { "type": "string", "format": "date-time", @@ -21480,10 +21490,19 @@ "format": "date-time", "x-go-name": "Created" }, + "deletions": { + "type": "integer", + "format": "int64", + "x-go-name": "Deletions" + }, "diff_url": { "type": "string", "x-go-name": "DiffURL" }, + "draft": { + "type": "boolean", + "x-go-name": "Draft" + }, "due_date": { "type": "string", "format": "date-time", @@ -21560,6 +21579,11 @@ }, "x-go-name": "RequestedReviewers" }, + "review_comments": { + "type": "integer", + "format": "int64", + "x-go-name": "ReviewComments" + }, "state": { "$ref": "#/definitions/StateType" }, @@ -21586,6 +21610,10 @@ "description": "PullRequestMeta PR info if an issue is a PR", "type": "object", "properties": { + "html_url": { + "type": "string", + "x-go-name": "HTMLURL" + }, "merged": { "type": "boolean", "x-go-name": "HasMerged" @@ -22842,6 +22870,11 @@ "type": "string", "x-go-name": "FullName" }, + "html_url": { + "description": "URL to the user's gitea page", + "type": "string", + "x-go-name": "HTMLURL" + }, "id": { "description": "the user's id", "type": "integer", From f0c74fb23f3b1fd5e45e0ba81a3618f50a027420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Rosenhammer?= Date: Wed, 13 Dec 2023 12:33:54 +0100 Subject: [PATCH 3/9] Fix diff calculation when using sqlite. --- services/convert/pull.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/services/convert/pull.go b/services/convert/pull.go index 0e5a79d458e96..826346bc5ed95 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -171,6 +171,12 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u return nil } + // Outer scope variables to be used in diff calculation + var ( + startCommitID string + endCommitID string + ) + if git.IsErrBranchNotExist(err) { headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref) if err != nil && !git.IsErrNotExist(err) { @@ -179,6 +185,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u } if err == nil { apiPullRequest.Head.Sha = headCommitID + endCommitID = headCommitID } } else { commit, err := headBranch.GetCommit() @@ -189,16 +196,12 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u if err == nil { apiPullRequest.Head.Ref = pr.HeadBranch apiPullRequest.Head.Sha = commit.ID.String() + endCommitID = commit.ID.String() } } - startCommitID := pr.MergeBase - endCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref) - if err != nil { - log.Error("GetRefCommitID[%s]: %v", apiPullRequest.Head.Ref, err) - } - - maxLines := setting.Git.MaxGitDiffLines + // Calcuate diff + startCommitID = pr.MergeBase // FIXME: If there are too many files in the repo, may cause some unpredictable issues. diff, err := gitdiff.GetDiff(ctx, gitRepo, @@ -206,7 +209,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u BeforeCommitID: startCommitID, AfterCommitID: endCommitID, SkipTo: "", // ctx.FormString("skip-to"), - MaxLines: maxLines, + MaxLines: setting.Git.MaxGitDiffLines, MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, MaxFiles: -1, // GetDiff() will return all files WhitespaceBehavior: gitdiff.GetWhitespaceFlag("show-all"), From 635d4112aadbdc4f9f2556a6dca232864499746f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Rosenhammer?= Date: Thu, 21 Dec 2023 11:55:49 +0100 Subject: [PATCH 4/9] Remove superfluous parameter for DiffOptions. --- services/convert/pull.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/convert/pull.go b/services/convert/pull.go index 826346bc5ed95..06d879a6a8713 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -200,7 +200,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u } } - // Calcuate diff + // Calculate diff startCommitID = pr.MergeBase // FIXME: If there are too many files in the repo, may cause some unpredictable issues. @@ -208,7 +208,6 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u &gitdiff.DiffOptions{ BeforeCommitID: startCommitID, AfterCommitID: endCommitID, - SkipTo: "", // ctx.FormString("skip-to"), MaxLines: setting.Git.MaxGitDiffLines, MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, MaxFiles: -1, // GetDiff() will return all files From b1fe6e5b4de158fe1276ab88ec849f90cf132f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Rosenhammer?= Date: Fri, 22 Dec 2023 10:08:32 +0100 Subject: [PATCH 5/9] Remove suoerfluous parameter for FindCommentsOptions. --- models/issues/pull.go | 1 - 1 file changed, 1 deletion(-) diff --git a/models/issues/pull.go b/models/issues/pull.go index a1f7b3c412615..3707f66bb9a03 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -434,7 +434,6 @@ func (pr *PullRequest) GetReviewCommentsCount(ctx context.Context) int { opts := FindCommentsOptions{ Type: CommentTypeReview, IssueID: pr.IssueID, - ReviewID: 0, // TODO: How to find the review ID? } conds := opts.ToConds() if pr.ID == 0 { From 7b7fa4fc0f031ea24607027b8151620f1c6771de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Rosenhammer?= Date: Fri, 5 Jan 2024 23:27:35 +0100 Subject: [PATCH 6/9] Use GetDiffShortStat to obtain diff changes. Add descriptive comments. --- models/issues/pull.go | 5 +++-- modules/structs/pull.go | 9 +++++---- services/convert/pull.go | 19 ++----------------- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/models/issues/pull.go b/models/issues/pull.go index 3707f66bb9a03..0666edc1822ba 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -430,10 +430,11 @@ func (pr *PullRequest) GetGitHeadBranchRefName() string { return fmt.Sprintf("%s%s", git.BranchPrefix, pr.HeadBranch) } +// 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) func (pr *PullRequest) GetReviewCommentsCount(ctx context.Context) int { opts := FindCommentsOptions{ - Type: CommentTypeReview, - IssueID: pr.IssueID, + Type: CommentTypeReview, + IssueID: pr.IssueID, } conds := opts.ToConds() if pr.ID == 0 { diff --git a/modules/structs/pull.go b/modules/structs/pull.go index f6b498df009c9..abf1df36fd3d4 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -24,10 +24,11 @@ type PullRequest struct { Draft bool `json:"draft"` IsLocked bool `json:"is_locked"` Comments int `json:"comments"` - ReviewComments int `json:"review_comments"` - Additions int `json:"additions"` - Deletions int `json:"deletions"` - ChangedFiles int `json:"changed_files"` + // number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR) + ReviewComments int `json:"review_comments"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + ChangedFiles int `json:"changed_files"` HTMLURL string `json:"html_url"` DiffURL string `json:"diff_url"` diff --git a/services/convert/pull.go b/services/convert/pull.go index 06d879a6a8713..d3679d26a9e32 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -13,9 +13,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/services/gitdiff" ) // ToAPIPullRequest assumes following fields have been assigned with valid values: @@ -203,24 +201,11 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u // Calculate diff startCommitID = pr.MergeBase - // FIXME: If there are too many files in the repo, may cause some unpredictable issues. - diff, err := gitdiff.GetDiff(ctx, gitRepo, - &gitdiff.DiffOptions{ - BeforeCommitID: startCommitID, - AfterCommitID: endCommitID, - MaxLines: setting.Git.MaxGitDiffLines, - MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, - MaxFiles: -1, // GetDiff() will return all files - WhitespaceBehavior: gitdiff.GetWhitespaceFlag("show-all"), - }) + apiPullRequest.ChangedFiles, apiPullRequest.Additions, apiPullRequest.Deletions, err = gitRepo.GetDiffShortStat(startCommitID, endCommitID) if err != nil { - log.Error("GetDiff: %v", err) + log.Error("GetDiffShortStat: %v", err) return nil } - - apiPullRequest.Additions = diff.TotalAddition - apiPullRequest.Deletions = diff.TotalDeletion - apiPullRequest.ChangedFiles = diff.NumFiles } if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 { From 8e50d3fff777de6f31d33d63a92fe1f4b98c4a9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Rosenhammer?= Date: Sat, 6 Jan 2024 09:30:14 +0100 Subject: [PATCH 7/9] Update swagger file to include review_comments description. --- templates/swagger/v1_json.tmpl | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 842d369e035e5..4a769d1e46b9a 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -21580,6 +21580,7 @@ "x-go-name": "RequestedReviewers" }, "review_comments": { + "description": "number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)", "type": "integer", "format": "int64", "x-go-name": "ReviewComments" From 2632a49944c2e2e5e0a4e59952a0cb2a64fb5af6 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 15 May 2024 21:44:51 +0200 Subject: [PATCH 8/9] Update services/convert/pull.go Co-authored-by: wxiaoguang --- services/convert/pull.go | 1 - 1 file changed, 1 deletion(-) diff --git a/services/convert/pull.go b/services/convert/pull.go index 8a269c29ace54..6d95804b38d0e 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -205,7 +205,6 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u apiPullRequest.ChangedFiles, apiPullRequest.Additions, apiPullRequest.Deletions, err = gitRepo.GetDiffShortStat(startCommitID, endCommitID) if err != nil { log.Error("GetDiffShortStat: %v", err) - return nil } } From b3b694d9aef2488cb99dacbcd668beeaa7a18555 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 26 May 2024 11:38:33 +0800 Subject: [PATCH 9/9] Update models/issues/pull.go --- models/issues/pull.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/models/issues/pull.go b/models/issues/pull.go index 5bbb4ba5eeb56..014fcd9fd022e 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -437,9 +437,6 @@ func (pr *PullRequest) GetReviewCommentsCount(ctx context.Context) int { IssueID: pr.IssueID, } conds := opts.ToConds() - if pr.ID == 0 { - conds = conds.And(builder.Eq{"invalidated": false}) - } count, err := db.GetEngine(ctx).Where(conds).Count(new(Comment)) if err != nil {