]> source.dussan.org Git - gitea.git/commitdiff
Make gitea webhooks openproject compatible (#28435) (#31081)
authorGiteabot <teabot@gitea.io>
Sun, 26 May 2024 04:53:42 +0000 (12:53 +0800)
committerGitHub <noreply@github.com>
Sun, 26 May 2024 04:53:42 +0000 (12:53 +0800)
Backport #28435 by Chief-Detektor

Co-authored-by: André Rosenhammer <andre.rosenhammer@gmail.com>
models/issues/pull.go
modules/structs/issue.go
modules/structs/pull.go
modules/structs/user.go
services/convert/issue.go
services/convert/pull.go
services/convert/user.go
templates/swagger/v1_json.tmpl

index 4194df2e3d535c7d3fbe32db22a12fa591c549f5..014fcd9fd022ef846b3282318806302f35cf9903 100644 (file)
@@ -430,6 +430,21 @@ 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,
+       }
+       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.
 func (pr *PullRequest) IsChecking() bool {
        return pr.Status == PullRequestStatusChecking
index 16242d18ada589aa3a391c4f70cf8094786703fb..3c06e383560a3863aa0361b655595041d4672870 100644 (file)
@@ -30,6 +30,7 @@ type PullRequestMeta struct {
        HasMerged        bool       `json:"merged"`
        Merged           *time.Time `json:"merged_at"`
        IsWorkInProgress bool       `json:"draft"`
+       HTMLURL          string     `json:"html_url"`
 }
 
 // RepositoryMeta basic repository information
index b04def52b866133c1892a4c94fff519707f8eec9..525d90c28e5f64e42730ea7f17e214f211504483 100644 (file)
@@ -21,8 +21,14 @@ 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"`
+       // 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"`
index ca6ab7994463c75b48c12c38656b639525b050af..5ed677f239fb50436e7c6c8e704f9eecc713cc55 100644 (file)
@@ -28,6 +28,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
index 54b00cd88ea391b2ad01e8242c49549a7c8e3ba7..28bef9eb02a7bb6221af861c2e67d9e4301c2153 100644 (file)
@@ -104,6 +104,8 @@ func toIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Iss
                        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 {
index 775bf3806d3fe1926f72e340a963cdd11272ebaa..6d95804b38d0e17c3138a9ab4aad542023a81f80 100644 (file)
@@ -51,29 +51,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,
 
@@ -168,6 +170,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) {
@@ -176,6 +184,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()
@@ -186,8 +195,17 @@ 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()
                        }
                }
+
+               // 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 {
index 2957c58b14771cde50d14869ed9cbe7a784d21e7..90bcf35cf69dded2127eacac8ab5b89d0a11c1ce 100644 (file)
@@ -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,
index 764a13063d54e88205ba9e3bc5b753c8dcd17e89..9ea6763a1aa9c692773618ebfd3f69be39f4ce86 100644 (file)
       "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"
           "type": "string",
           "x-go-name": "Body"
         },
+        "changed_files": {
+          "type": "integer",
+          "format": "int64",
+          "x-go-name": "ChangedFiles"
+        },
         "closed_at": {
           "type": "string",
           "format": "date-time",
           "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",
           },
           "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": {
           "$ref": "#/definitions/StateType"
         },
           "type": "boolean",
           "x-go-name": "IsWorkInProgress"
         },
+        "html_url": {
+          "type": "string",
+          "x-go-name": "HTMLURL"
+        },
         "merged": {
           "type": "boolean",
           "x-go-name": "HasMerged"
           "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",