Make gitea webhooks openproject compatible (#28435) (#31081)

Backport #28435 by Chief-Detektor

Co-authored-by: André Rosenhammer <andre.rosenhammer@gmail.com>
This commit is contained in:
Giteabot 2024-05-26 12:53:42 +08:00 committed by GitHub
parent 7d56ee3c0f
commit 1171b24d52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

AI 샘플 코드 생성 중입니다

Loading...
8 changed files with 102 additions and 23 deletions

View File

@ -430,6 +430,21 @@ func (pr *PullRequest) GetGitHeadBranchRefName() string {
return fmt.Sprintf("%s%s", git.BranchPrefix, pr.HeadBranch) 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,
}
conds := opts.ToConds()
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. // IsChecking returns true if this pull request is still checking conflict.
func (pr *PullRequest) IsChecking() bool { func (pr *PullRequest) IsChecking() bool {
return pr.Status == PullRequestStatusChecking return pr.Status == PullRequestStatusChecking

View File

@ -30,6 +30,7 @@ type PullRequestMeta struct {
HasMerged bool `json:"merged"` HasMerged bool `json:"merged"`
Merged *time.Time `json:"merged_at"` Merged *time.Time `json:"merged_at"`
IsWorkInProgress bool `json:"draft"` IsWorkInProgress bool `json:"draft"`
HTMLURL string `json:"html_url"`
} }
// RepositoryMeta basic repository information // RepositoryMeta basic repository information

View File

@ -21,8 +21,14 @@ type PullRequest struct {
Assignees []*User `json:"assignees"` Assignees []*User `json:"assignees"`
RequestedReviewers []*User `json:"requested_reviewers"` RequestedReviewers []*User `json:"requested_reviewers"`
State StateType `json:"state"` State StateType `json:"state"`
Draft bool `json:"draft"`
IsLocked bool `json:"is_locked"` IsLocked bool `json:"is_locked"`
Comments int `json:"comments"` Comments int `json:"comments"`
// 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"` HTMLURL string `json:"html_url"`
DiffURL string `json:"diff_url"` DiffURL string `json:"diff_url"`

View File

@ -28,6 +28,8 @@ type User struct {
Email string `json:"email"` Email string `json:"email"`
// URL to the user's avatar // URL to the user's avatar
AvatarURL string `json:"avatar_url"` AvatarURL string `json:"avatar_url"`
// URL to the user's gitea page
HTMLURL string `json:"html_url"`
// User locale // User locale
Language string `json:"language"` Language string `json:"language"`
// Is the user an administrator // Is the user an administrator

View File

@ -104,6 +104,8 @@ func toIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Iss
if issue.PullRequest.HasMerged { if issue.PullRequest.HasMerged {
apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr() apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
} }
// Add pr's html url
apiIssue.PullRequest.HTMLURL = issue.HTMLURL()
} }
} }
if issue.DeadlineUnix != 0 { if issue.DeadlineUnix != 0 {

View File

@ -51,29 +51,31 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
} }
apiPullRequest := &api.PullRequest{ apiPullRequest := &api.PullRequest{
ID: pr.ID, ID: pr.ID,
URL: pr.Issue.HTMLURL(), URL: pr.Issue.HTMLURL(),
Index: pr.Index, Index: pr.Index,
Poster: apiIssue.Poster, Poster: apiIssue.Poster,
Title: apiIssue.Title, Title: apiIssue.Title,
Body: apiIssue.Body, Body: apiIssue.Body,
Labels: apiIssue.Labels, Labels: apiIssue.Labels,
Milestone: apiIssue.Milestone, Milestone: apiIssue.Milestone,
Assignee: apiIssue.Assignee, Assignee: apiIssue.Assignee,
Assignees: apiIssue.Assignees, Assignees: apiIssue.Assignees,
State: apiIssue.State, State: apiIssue.State,
IsLocked: apiIssue.IsLocked, Draft: pr.IsWorkInProgress(ctx),
Comments: apiIssue.Comments, IsLocked: apiIssue.IsLocked,
HTMLURL: pr.Issue.HTMLURL(), Comments: apiIssue.Comments,
DiffURL: pr.Issue.DiffURL(), ReviewComments: pr.GetReviewCommentsCount(ctx),
PatchURL: pr.Issue.PatchURL(), HTMLURL: pr.Issue.HTMLURL(),
HasMerged: pr.HasMerged, DiffURL: pr.Issue.DiffURL(),
MergeBase: pr.MergeBase, PatchURL: pr.Issue.PatchURL(),
Mergeable: pr.Mergeable(ctx), HasMerged: pr.HasMerged,
Deadline: apiIssue.Deadline, MergeBase: pr.MergeBase,
Created: pr.Issue.CreatedUnix.AsTimePtr(), Mergeable: pr.Mergeable(ctx),
Updated: pr.Issue.UpdatedUnix.AsTimePtr(), Deadline: apiIssue.Deadline,
PinOrder: apiIssue.PinOrder, Created: pr.Issue.CreatedUnix.AsTimePtr(),
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
PinOrder: apiIssue.PinOrder,
AllowMaintainerEdit: pr.AllowMaintainerEdit, AllowMaintainerEdit: pr.AllowMaintainerEdit,
@ -168,6 +170,12 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
return nil return nil
} }
// Outer scope variables to be used in diff calculation
var (
startCommitID string
endCommitID string
)
if git.IsErrBranchNotExist(err) { if git.IsErrBranchNotExist(err) {
headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref) headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref)
if err != nil && !git.IsErrNotExist(err) { if err != nil && !git.IsErrNotExist(err) {
@ -176,6 +184,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
} }
if err == nil { if err == nil {
apiPullRequest.Head.Sha = headCommitID apiPullRequest.Head.Sha = headCommitID
endCommitID = headCommitID
} }
} else { } else {
commit, err := headBranch.GetCommit() commit, err := headBranch.GetCommit()
@ -186,8 +195,17 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
if err == nil { if err == nil {
apiPullRequest.Head.Ref = pr.HeadBranch apiPullRequest.Head.Ref = pr.HeadBranch
apiPullRequest.Head.Sha = commit.ID.String() apiPullRequest.Head.Sha = commit.ID.String()
endCommitID = commit.ID.String()
} }
} }
// Calculate diff
startCommitID = pr.MergeBase
apiPullRequest.ChangedFiles, apiPullRequest.Additions, apiPullRequest.Deletions, err = gitRepo.GetDiffShortStat(startCommitID, endCommitID)
if err != nil {
log.Error("GetDiffShortStat: %v", err)
}
} }
if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 { if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {

View File

@ -53,6 +53,7 @@ func toUser(ctx context.Context, user *user_model.User, signed, authed bool) *ap
FullName: user.FullName, FullName: user.FullName,
Email: user.GetPlaceholderEmail(), Email: user.GetPlaceholderEmail(),
AvatarURL: user.AvatarLink(ctx), AvatarURL: user.AvatarLink(ctx),
HTMLURL: user.HTMLURL(),
Created: user.CreatedUnix.AsTime(), Created: user.CreatedUnix.AsTime(),
Restricted: user.IsRestricted, Restricted: user.IsRestricted,
Location: user.Location, Location: user.Location,

View File

@ -22832,6 +22832,11 @@
"description": "PullRequest represents a pull request", "description": "PullRequest represents a pull request",
"type": "object", "type": "object",
"properties": { "properties": {
"additions": {
"type": "integer",
"format": "int64",
"x-go-name": "Additions"
},
"allow_maintainer_edit": { "allow_maintainer_edit": {
"type": "boolean", "type": "boolean",
"x-go-name": "AllowMaintainerEdit" "x-go-name": "AllowMaintainerEdit"
@ -22853,6 +22858,11 @@
"type": "string", "type": "string",
"x-go-name": "Body" "x-go-name": "Body"
}, },
"changed_files": {
"type": "integer",
"format": "int64",
"x-go-name": "ChangedFiles"
},
"closed_at": { "closed_at": {
"type": "string", "type": "string",
"format": "date-time", "format": "date-time",
@ -22868,10 +22878,19 @@
"format": "date-time", "format": "date-time",
"x-go-name": "Created" "x-go-name": "Created"
}, },
"deletions": {
"type": "integer",
"format": "int64",
"x-go-name": "Deletions"
},
"diff_url": { "diff_url": {
"type": "string", "type": "string",
"x-go-name": "DiffURL" "x-go-name": "DiffURL"
}, },
"draft": {
"type": "boolean",
"x-go-name": "Draft"
},
"due_date": { "due_date": {
"type": "string", "type": "string",
"format": "date-time", "format": "date-time",
@ -22948,6 +22967,12 @@
}, },
"x-go-name": "RequestedReviewers" "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"
},
"state": { "state": {
"$ref": "#/definitions/StateType" "$ref": "#/definitions/StateType"
}, },
@ -22978,6 +23003,10 @@
"type": "boolean", "type": "boolean",
"x-go-name": "IsWorkInProgress" "x-go-name": "IsWorkInProgress"
}, },
"html_url": {
"type": "string",
"x-go-name": "HTMLURL"
},
"merged": { "merged": {
"type": "boolean", "type": "boolean",
"x-go-name": "HasMerged" "x-go-name": "HasMerged"
@ -24271,6 +24300,11 @@
"type": "string", "type": "string",
"x-go-name": "FullName" "x-go-name": "FullName"
}, },
"html_url": {
"description": "URL to the user's gitea page",
"type": "string",
"x-go-name": "HTMLURL"
},
"id": { "id": {
"description": "the user's id", "description": "the user's id",
"type": "integer", "type": "integer",