2019-04-25 13:06:53 -04:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2020-01-03 20:44:33 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2019-04-25 13:06:53 -04:00
|
|
|
// 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"
|
2020-02-07 08:53:15 -05:00
|
|
|
"net/url"
|
2020-09-15 22:01:41 -04:00
|
|
|
"strings"
|
2019-04-25 13:06:53 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-10-13 11:13:33 -04:00
|
|
|
// PRBranchInfo information about a branch
|
|
|
|
type PRBranchInfo struct {
|
|
|
|
Name string `json:"label"`
|
|
|
|
Ref string `json:"ref"`
|
|
|
|
Sha string `json:"sha"`
|
|
|
|
RepoID int64 `json:"repo_id"`
|
|
|
|
Repository *Repository `json:"repo"`
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// PullRequest represents a pull request
|
|
|
|
type PullRequest struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Index int64 `json:"number"`
|
|
|
|
Poster *User `json:"user"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
Labels []*Label `json:"labels"`
|
|
|
|
Milestone *Milestone `json:"milestone"`
|
|
|
|
Assignee *User `json:"assignee"`
|
|
|
|
Assignees []*User `json:"assignees"`
|
|
|
|
State StateType `json:"state"`
|
2020-09-15 22:01:41 -04:00
|
|
|
IsLocked bool `json:"is_locked"`
|
2019-04-25 13:06:53 -04:00
|
|
|
Comments int `json:"comments"`
|
|
|
|
|
|
|
|
HTMLURL string `json:"html_url"`
|
|
|
|
DiffURL string `json:"diff_url"`
|
|
|
|
PatchURL string `json:"patch_url"`
|
|
|
|
|
2019-10-13 11:13:33 -04:00
|
|
|
Mergeable bool `json:"mergeable"`
|
|
|
|
HasMerged bool `json:"merged"`
|
2019-04-25 13:06:53 -04:00
|
|
|
Merged *time.Time `json:"merged_at"`
|
|
|
|
MergedCommitID *string `json:"merge_commit_sha"`
|
|
|
|
MergedBy *User `json:"merged_by"`
|
|
|
|
|
|
|
|
Base *PRBranchInfo `json:"base"`
|
|
|
|
Head *PRBranchInfo `json:"head"`
|
|
|
|
MergeBase string `json:"merge_base"`
|
|
|
|
|
|
|
|
Deadline *time.Time `json:"due_date"`
|
2019-10-13 11:13:33 -04:00
|
|
|
Created *time.Time `json:"created_at"`
|
|
|
|
Updated *time.Time `json:"updated_at"`
|
|
|
|
Closed *time.Time `json:"closed_at"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListPullRequestsOptions options for listing pull requests
|
|
|
|
type ListPullRequestsOptions struct {
|
2020-05-27 09:48:09 -04:00
|
|
|
ListOptions
|
|
|
|
State StateType `json:"state"`
|
2020-02-07 08:53:15 -05:00
|
|
|
// oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority
|
2020-05-27 09:48:09 -04:00
|
|
|
Sort string
|
|
|
|
Milestone int64
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
// MergeStyle is used specify how a pull is merged
|
|
|
|
type MergeStyle string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MergeStyleMerge merge pull as usual
|
|
|
|
MergeStyleMerge MergeStyle = "merge"
|
|
|
|
// MergeStyleRebase rebase pull
|
|
|
|
MergeStyleRebase MergeStyle = "rebase"
|
|
|
|
// MergeStyleRebaseMerge rebase and merge pull
|
|
|
|
MergeStyleRebaseMerge MergeStyle = "rebase-merge"
|
|
|
|
// MergeStyleSquash squash and merge pull
|
|
|
|
MergeStyleSquash MergeStyle = "squash"
|
|
|
|
)
|
|
|
|
|
|
|
|
// QueryEncode turns options into querystring argument
|
|
|
|
func (opt *ListPullRequestsOptions) QueryEncode() string {
|
|
|
|
query := opt.getURLQuery()
|
2020-02-07 08:53:15 -05:00
|
|
|
if len(opt.State) > 0 {
|
2020-05-27 09:48:09 -04:00
|
|
|
query.Add("state", string(opt.State))
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
if len(opt.Sort) > 0 {
|
|
|
|
query.Add("sort", opt.Sort)
|
|
|
|
}
|
|
|
|
if opt.Milestone > 0 {
|
|
|
|
query.Add("milestone", fmt.Sprintf("%d", opt.Milestone))
|
|
|
|
}
|
2020-05-27 09:48:09 -04:00
|
|
|
return query.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepoPullRequests list PRs of one repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
opt.setDefaults()
|
|
|
|
prs := make([]*PullRequest, 0, opt.PageSize)
|
|
|
|
|
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls", owner, repo))
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs)
|
|
|
|
return prs, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPullRequest get information of one PR
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
pr := new(PullRequest)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index), nil, nil, pr)
|
|
|
|
return pr, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePullRequestOption options when creating a pull request
|
|
|
|
type CreatePullRequestOption struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
Head string `json:"head"`
|
|
|
|
Base string `json:"base"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
Assignee string `json:"assignee"`
|
|
|
|
Assignees []string `json:"assignees"`
|
|
|
|
Milestone int64 `json:"milestone"`
|
|
|
|
Labels []int64 `json:"labels"`
|
|
|
|
Deadline *time.Time `json:"due_date"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePullRequest create pull request with options
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
pr := new(PullRequest)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/pulls", owner, repo),
|
2019-04-25 13:06:53 -04:00
|
|
|
jsonHeader, bytes.NewReader(body), pr)
|
2020-09-15 22:01:41 -04:00
|
|
|
return pr, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditPullRequestOption options when modify pull request
|
|
|
|
type EditPullRequestOption struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
2020-07-13 18:38:05 -04:00
|
|
|
Base string `json:"base"`
|
2019-10-13 11:13:33 -04:00
|
|
|
Assignee string `json:"assignee"`
|
|
|
|
Assignees []string `json:"assignees"`
|
|
|
|
Milestone int64 `json:"milestone"`
|
|
|
|
Labels []int64 `json:"labels"`
|
2020-05-27 09:48:09 -04:00
|
|
|
State *StateType `json:"state"`
|
2019-10-13 11:13:33 -04:00
|
|
|
Deadline *time.Time `json:"due_date"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
// Validate the EditPullRequestOption struct
|
|
|
|
func (opt EditPullRequestOption) Validate(c *Client) error {
|
|
|
|
if len(opt.Title) != 0 && len(strings.TrimSpace(opt.Title)) == 0 {
|
|
|
|
return fmt.Errorf("title is empty")
|
|
|
|
}
|
|
|
|
if len(opt.Base) != 0 {
|
|
|
|
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil {
|
|
|
|
return fmt.Errorf("can not change base gitea to old")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// EditPullRequest modify pull request with PR id and options
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, *Response, error) {
|
|
|
|
if err := opt.Validate(c); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
pr := new(PullRequest)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("PATCH",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index),
|
2019-04-25 13:06:53 -04:00
|
|
|
jsonHeader, bytes.NewReader(body), pr)
|
2020-09-15 22:01:41 -04:00
|
|
|
return pr, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-01-03 20:44:33 -05:00
|
|
|
// MergePullRequestOption options when merging a pull request
|
|
|
|
type MergePullRequestOption struct {
|
2020-05-27 09:48:09 -04:00
|
|
|
Style MergeStyle `json:"Do"`
|
|
|
|
Title string `json:"MergeTitleField"`
|
|
|
|
Message string `json:"MergeMessageField"`
|
2020-01-03 20:44:33 -05:00
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
// Validate the MergePullRequestOption struct
|
|
|
|
func (opt MergePullRequestOption) Validate(c *Client) error {
|
2020-05-27 09:48:09 -04:00
|
|
|
if opt.Style == MergeStyleSquash {
|
|
|
|
if err := c.CheckServerVersionConstraint(">=1.11.5"); err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return err
|
2020-05-27 09:48:09 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergePullRequest merge a PR to repository by PR id
|
|
|
|
func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (bool, *Response, error) {
|
|
|
|
if err := opt.Validate(c); err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
2020-01-03 20:44:33 -05:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return false, nil, err
|
2020-04-30 06:54:11 -04:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
status, resp, err := c.getStatusCode("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), jsonHeader, bytes.NewReader(body))
|
2020-04-30 06:54:11 -04:00
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return false, resp, err
|
2020-01-03 20:44:33 -05:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
return status == 200, resp, nil
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsPullRequestMerged test if one PR is merged to one repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, *Response, error) {
|
|
|
|
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
|
2019-04-25 13:06:53 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return false, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return status == 204, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPullRequestDiffOrPatch gets the patch or diff file as bytes for a PR
|
|
|
|
func (c *Client) getPullRequestDiffOrPatch(owner, repo, kind string, index int64) ([]byte, *Response, error) {
|
|
|
|
if err := c.CheckServerVersionConstraint(">=1.13.0"); err != nil {
|
|
|
|
r, _, err2 := c.GetRepo(owner, repo)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if r.Private {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return c.getWebResponse("GET", fmt.Sprintf("/%s/%s/pulls/%d.%s", owner, repo, index, kind), nil)
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d.%s", owner, repo, index, kind), nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPullRequestPatch gets the .patch file as bytes for a PR
|
|
|
|
func (c *Client) GetPullRequestPatch(owner, repo string, index int64) ([]byte, *Response, error) {
|
|
|
|
return c.getPullRequestDiffOrPatch(owner, repo, "patch", index)
|
|
|
|
}
|
2019-04-25 13:06:53 -04:00
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
// GetPullRequestDiff gets the .diff file as bytes for a PR
|
|
|
|
func (c *Client) GetPullRequestDiff(owner, repo string, index int64) ([]byte, *Response, error) {
|
|
|
|
return c.getPullRequestDiffOrPatch(owner, repo, "diff", index)
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|