2020-05-27 13:48:09 +00:00
|
|
|
// 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 gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2020-09-16 02:01:41 +00:00
|
|
|
"strings"
|
2020-05-27 13:48:09 +00:00
|
|
|
"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"`
|
2020-09-16 02:01:41 +00:00
|
|
|
Submitted time.Time `json:"submitted_at"`
|
2020-05-27 13:48:09 +00:00
|
|
|
|
|
|
|
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"`
|
|
|
|
|
|
|
|
Created time.Time `json:"created_at"`
|
|
|
|
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 {
|
|
|
|
State 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 {
|
|
|
|
State ReviewStateType `json:"event"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPullReviewsOptions options for listing PullReviews
|
|
|
|
type ListPullReviewsOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
// Validate the CreatePullReviewOptions struct
|
|
|
|
func (opt CreatePullReviewOptions) Validate() error {
|
|
|
|
if opt.State != ReviewStateApproved && len(strings.TrimSpace(opt.Body)) == 0 {
|
|
|
|
return fmt.Errorf("body is empty")
|
|
|
|
}
|
|
|
|
for i := range opt.Comments {
|
|
|
|
if err := opt.Comments[i].Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the SubmitPullReviewOptions struct
|
|
|
|
func (opt SubmitPullReviewOptions) Validate() error {
|
|
|
|
if opt.State != ReviewStateApproved && len(strings.TrimSpace(opt.Body)) == 0 {
|
|
|
|
return fmt.Errorf("body is empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the CreatePullReviewComment struct
|
|
|
|
func (opt CreatePullReviewComment) Validate() error {
|
|
|
|
if len(strings.TrimSpace(opt.Body)) == 0 {
|
|
|
|
return fmt.Errorf("body is empty")
|
|
|
|
}
|
|
|
|
if opt.NewLineNum != 0 && opt.OldLineNum != 0 {
|
|
|
|
return fmt.Errorf("old and new line num are set, cant identify the code comment position")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 13:48:09 +00:00
|
|
|
// ListPullReviews lists all reviews of a pull request
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) ListPullReviews(owner, repo string, index int64, opt ListPullReviewsOptions) ([]*PullReview, *Response, error) {
|
2020-05-27 13:48:09 +00:00
|
|
|
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, nil, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
opt.setDefaults()
|
|
|
|
rs := make([]*PullReview, 0, opt.PageSize)
|
|
|
|
|
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews", owner, repo, index))
|
|
|
|
link.RawQuery = opt.ListOptions.getURLQuery().Encode()
|
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &rs)
|
|
|
|
return rs, resp, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPullReview gets a specific review of a pull request
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) GetPullReview(owner, repo string, index, id int64) (*PullReview, *Response, error) {
|
2020-05-27 13:48:09 +00:00
|
|
|
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, nil, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r := new(PullReview)
|
2020-09-16 02:01:41 +00:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d", owner, repo, index, id), jsonHeader, nil, &r)
|
|
|
|
return r, resp, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListPullReviewComments lists all comments of a pull request review
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) ListPullReviewComments(owner, repo string, index, id int64) ([]*PullReviewComment, *Response, error) {
|
2020-05-27 13:48:09 +00:00
|
|
|
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, nil, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
2020-09-16 02:01:41 +00:00
|
|
|
rcl := make([]*PullReviewComment, 0, 4)
|
2020-05-27 13:48:09 +00:00
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d/comments", owner, repo, index, id))
|
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &rcl)
|
|
|
|
return rcl, resp, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePullReview delete a specific review from a pull request
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) DeletePullReview(owner, repo string, index, id int64) (*Response, error) {
|
2020-05-27 13:48:09 +00:00
|
|
|
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d", owner, repo, index, id), jsonHeader, nil)
|
|
|
|
return resp, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePullReview create a review to an pull request
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) CreatePullReview(owner, repo string, index int64, opt CreatePullReviewOptions) (*PullReview, *Response, error) {
|
2020-05-27 13:48:09 +00:00
|
|
|
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, nil, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, nil, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r := new(PullReview)
|
2020-09-16 02:01:41 +00:00
|
|
|
resp, err := c.getParsedResponse("POST",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews", owner, repo, index),
|
2020-05-27 13:48:09 +00:00
|
|
|
jsonHeader, bytes.NewReader(body), r)
|
2020-09-16 02:01:41 +00:00
|
|
|
return r, resp, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitPullReview submit a pending review to an pull request
|
2020-09-16 02:01:41 +00:00
|
|
|
func (c *Client) SubmitPullReview(owner, repo string, index, id int64, opt SubmitPullReviewOptions) (*PullReview, *Response, error) {
|
2020-05-27 13:48:09 +00:00
|
|
|
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, nil, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-16 02:01:41 +00:00
|
|
|
return nil, nil, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r := new(PullReview)
|
2020-09-16 02:01:41 +00:00
|
|
|
resp, err := c.getParsedResponse("POST",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/pulls/%d/reviews/%d", owner, repo, index, id),
|
2020-05-27 13:48:09 +00:00
|
|
|
jsonHeader, bytes.NewReader(body), r)
|
2020-09-16 02:01:41 +00:00
|
|
|
return r, resp, err
|
2020-05-27 13:48:09 +00:00
|
|
|
}
|