mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
API: Add pull review endpoints (#11224)
* API: Added pull review read only endpoints * Update Structs, move Conversion, Refactor * refactor * lint & co * fix lint + refactor * add new Review state, rm unessesary, refacotr loadAttributes, convert patch to diff * add DeletePullReview * add paggination * draft1: Create & submit review * fix lint * fix lint * impruve test * DONT use GhostUser for loadReviewer * expose comments_count of a PullReview * infent GetCodeCommentsCount() * fixes * fix+impruve * some nits * Handle Ghosts 👻 * add TEST for GET apis * complete TESTS * add HTMLURL to PullReview responce * code format as per @lafriks * update swagger definition * Update routers/api/v1/repo/pull_review.go Co-authored-by: David Svantesson <davidsvantesson@gmail.com> * add comments Co-authored-by: Thomas Berger <loki@lokis-chaos.de> Co-authored-by: David Svantesson <davidsvantesson@gmail.com>
This commit is contained in:
127
modules/convert/pull_review.go
Normal file
127
modules/convert/pull_review.go
Normal file
@@ -0,0 +1,127 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package convert
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// ToPullReview convert a review to api format
|
||||
func ToPullReview(r *models.Review, doer *models.User) (*api.PullReview, error) {
|
||||
if err := r.LoadAttributes(); err != nil {
|
||||
if !models.IsErrUserNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
r.Reviewer = models.NewGhostUser()
|
||||
}
|
||||
|
||||
auth := false
|
||||
if doer != nil {
|
||||
auth = doer.IsAdmin || doer.ID == r.ReviewerID
|
||||
}
|
||||
|
||||
result := &api.PullReview{
|
||||
ID: r.ID,
|
||||
Reviewer: ToUser(r.Reviewer, doer != nil, auth),
|
||||
State: api.ReviewStateUnknown,
|
||||
Body: r.Content,
|
||||
CommitID: r.CommitID,
|
||||
Stale: r.Stale,
|
||||
Official: r.Official,
|
||||
CodeCommentsCount: r.GetCodeCommentsCount(),
|
||||
Submitted: r.CreatedUnix.AsTime(),
|
||||
HTMLURL: r.HTMLURL(),
|
||||
HTMLPullURL: r.Issue.HTMLURL(),
|
||||
}
|
||||
|
||||
switch r.Type {
|
||||
case models.ReviewTypeApprove:
|
||||
result.State = api.ReviewStateApproved
|
||||
case models.ReviewTypeReject:
|
||||
result.State = api.ReviewStateRequestChanges
|
||||
case models.ReviewTypeComment:
|
||||
result.State = api.ReviewStateComment
|
||||
case models.ReviewTypePending:
|
||||
result.State = api.ReviewStatePending
|
||||
case models.ReviewTypeRequest:
|
||||
result.State = api.ReviewStateRequestReview
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ToPullReviewList convert a list of review to it's api format
|
||||
func ToPullReviewList(rl []*models.Review, doer *models.User) ([]*api.PullReview, error) {
|
||||
result := make([]*api.PullReview, 0, len(rl))
|
||||
for i := range rl {
|
||||
// show pending reviews only for the user who created them
|
||||
if rl[i].Type == models.ReviewTypePending && !(doer.IsAdmin || doer.ID == rl[i].ReviewerID) {
|
||||
continue
|
||||
}
|
||||
r, err := ToPullReview(rl[i], doer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, r)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ToPullReviewCommentList convert the CodeComments of an review to it's api format
|
||||
func ToPullReviewCommentList(review *models.Review, doer *models.User) ([]*api.PullReviewComment, error) {
|
||||
if err := review.LoadAttributes(); err != nil {
|
||||
if !models.IsErrUserNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
review.Reviewer = models.NewGhostUser()
|
||||
}
|
||||
|
||||
apiComments := make([]*api.PullReviewComment, 0, len(review.CodeComments))
|
||||
|
||||
auth := false
|
||||
if doer != nil {
|
||||
auth = doer.IsAdmin || doer.ID == review.ReviewerID
|
||||
}
|
||||
|
||||
for _, lines := range review.CodeComments {
|
||||
for _, comments := range lines {
|
||||
for _, comment := range comments {
|
||||
apiComment := &api.PullReviewComment{
|
||||
ID: comment.ID,
|
||||
Body: comment.Content,
|
||||
Reviewer: ToUser(review.Reviewer, doer != nil, auth),
|
||||
ReviewID: review.ID,
|
||||
Created: comment.CreatedUnix.AsTime(),
|
||||
Updated: comment.UpdatedUnix.AsTime(),
|
||||
Path: comment.TreePath,
|
||||
CommitID: comment.CommitSHA,
|
||||
OrigCommitID: comment.OldRef,
|
||||
DiffHunk: patch2diff(comment.Patch),
|
||||
HTMLURL: comment.HTMLURL(),
|
||||
HTMLPullURL: review.Issue.APIURL(),
|
||||
}
|
||||
|
||||
if comment.Line < 0 {
|
||||
apiComment.OldLineNum = comment.UnsignedLine()
|
||||
} else {
|
||||
apiComment.LineNum = comment.UnsignedLine()
|
||||
}
|
||||
apiComments = append(apiComments, apiComment)
|
||||
}
|
||||
}
|
||||
}
|
||||
return apiComments, nil
|
||||
}
|
||||
|
||||
func patch2diff(patch string) string {
|
||||
split := strings.Split(patch, "\n@@")
|
||||
if len(split) == 2 {
|
||||
return "@@" + split[1]
|
||||
}
|
||||
return ""
|
||||
}
|
92
modules/structs/pull_review.go
Normal file
92
modules/structs/pull_review.go
Normal file
@@ -0,0 +1,92 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package structs
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ReviewStateType review state type
|
||||
type ReviewStateType string
|
||||
|
||||
const (
|
||||
// ReviewStateApproved pr is approved
|
||||
ReviewStateApproved ReviewStateType = "APPROVED"
|
||||
// ReviewStatePending pr state is pending
|
||||
ReviewStatePending ReviewStateType = "PENDING"
|
||||
// ReviewStateComment is a comment review
|
||||
ReviewStateComment ReviewStateType = "COMMENT"
|
||||
// ReviewStateRequestChanges changes for pr are requested
|
||||
ReviewStateRequestChanges ReviewStateType = "REQUEST_CHANGES"
|
||||
// ReviewStateRequestReview review is requested from user
|
||||
ReviewStateRequestReview ReviewStateType = "REQUEST_REVIEW"
|
||||
// ReviewStateUnknown state of pr is unknown
|
||||
ReviewStateUnknown ReviewStateType = ""
|
||||
)
|
||||
|
||||
// PullReview represents a pull request review
|
||||
type PullReview struct {
|
||||
ID int64 `json:"id"`
|
||||
Reviewer *User `json:"user"`
|
||||
State ReviewStateType `json:"state"`
|
||||
Body string `json:"body"`
|
||||
CommitID string `json:"commit_id"`
|
||||
Stale bool `json:"stale"`
|
||||
Official bool `json:"official"`
|
||||
CodeCommentsCount int `json:"comments_count"`
|
||||
// swagger:strfmt date-time
|
||||
Submitted time.Time `json:"submitted_at"`
|
||||
|
||||
HTMLURL string `json:"html_url"`
|
||||
HTMLPullURL string `json:"pull_request_url"`
|
||||
}
|
||||
|
||||
// PullReviewComment represents a comment on a pull request review
|
||||
type PullReviewComment struct {
|
||||
ID int64 `json:"id"`
|
||||
Body string `json:"body"`
|
||||
Reviewer *User `json:"user"`
|
||||
ReviewID int64 `json:"pull_request_review_id"`
|
||||
|
||||
// swagger:strfmt date-time
|
||||
Created time.Time `json:"created_at"`
|
||||
// swagger:strfmt date-time
|
||||
Updated time.Time `json:"updated_at"`
|
||||
|
||||
Path string `json:"path"`
|
||||
CommitID string `json:"commit_id"`
|
||||
OrigCommitID string `json:"original_commit_id"`
|
||||
DiffHunk string `json:"diff_hunk"`
|
||||
LineNum uint64 `json:"position"`
|
||||
OldLineNum uint64 `json:"original_position"`
|
||||
|
||||
HTMLURL string `json:"html_url"`
|
||||
HTMLPullURL string `json:"pull_request_url"`
|
||||
}
|
||||
|
||||
// CreatePullReviewOptions are options to create a pull review
|
||||
type CreatePullReviewOptions struct {
|
||||
Event ReviewStateType `json:"event"`
|
||||
Body string `json:"body"`
|
||||
CommitID string `json:"commit_id"`
|
||||
Comments []CreatePullReviewComment `json:"comments"`
|
||||
}
|
||||
|
||||
// CreatePullReviewComment represent a review comment for creation api
|
||||
type CreatePullReviewComment struct {
|
||||
// the tree path
|
||||
Path string `json:"path"`
|
||||
Body string `json:"body"`
|
||||
// if comment to old file line or 0
|
||||
OldLineNum int64 `json:"old_position"`
|
||||
// if comment to new file line or 0
|
||||
NewLineNum int64 `json:"new_position"`
|
||||
}
|
||||
|
||||
// SubmitPullReviewOptions are options to submit a pending pull review
|
||||
type SubmitPullReviewOptions struct {
|
||||
Event ReviewStateType `json:"event"`
|
||||
Body string `json:"body"`
|
||||
}
|
Reference in New Issue
Block a user