2016-08-14 11:32:53 +01:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2020-01-03 06:22:50 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-08-14 11:32:53 +01:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-11-07 01:57:49 +00:00
|
|
|
package gitea
|
2016-08-14 11:32:53 +01:00
|
|
|
|
|
|
|
import (
|
2016-10-11 20:52:07 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-01-16 03:52:26 +00:00
|
|
|
"net/url"
|
2019-10-13 02:34:01 +01:00
|
|
|
"time"
|
2019-05-11 16:38:52 +01:00
|
|
|
)
|
2016-10-11 20:52:07 +01:00
|
|
|
|
2019-10-13 02:34:01 +01: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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"`
|
|
|
|
Comments int `json:"comments"`
|
|
|
|
|
|
|
|
HTMLURL string `json:"html_url"`
|
|
|
|
DiffURL string `json:"diff_url"`
|
|
|
|
PatchURL string `json:"patch_url"`
|
|
|
|
|
|
|
|
Mergeable bool `json:"mergeable"`
|
|
|
|
HasMerged bool `json:"merged"`
|
|
|
|
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"`
|
|
|
|
Created *time.Time `json:"created_at"`
|
|
|
|
Updated *time.Time `json:"updated_at"`
|
|
|
|
Closed *time.Time `json:"closed_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPullRequestsOptions options for listing pull requests
|
|
|
|
type ListPullRequestsOptions struct {
|
2020-02-05 07:59:55 +00:00
|
|
|
ListOptions
|
2020-02-04 07:58:46 +00:00
|
|
|
State StateType `json:"state"`
|
2020-01-13 17:58:39 +00:00
|
|
|
// oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority
|
2020-02-05 07:59:55 +00:00
|
|
|
Sort string
|
|
|
|
Milestone int64
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
2016-10-11 20:52:07 +01:00
|
|
|
|
2020-02-05 07:59:55 +00:00
|
|
|
// QueryEncode turns options into querystring argument
|
|
|
|
func (opt *ListPullRequestsOptions) QueryEncode() string {
|
|
|
|
query := opt.getURLQuery()
|
2020-01-16 03:52:26 +00:00
|
|
|
if len(opt.State) > 0 {
|
2020-02-04 07:58:46 +00:00
|
|
|
query.Add("state", string(opt.State))
|
2020-01-16 03:52:26 +00:00
|
|
|
}
|
|
|
|
if len(opt.Sort) > 0 {
|
|
|
|
query.Add("sort", opt.Sort)
|
|
|
|
}
|
|
|
|
if opt.Milestone > 0 {
|
|
|
|
query.Add("milestone", fmt.Sprintf("%d", opt.Milestone))
|
|
|
|
}
|
2020-02-05 07:59:55 +00:00
|
|
|
return query.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepoPullRequests list PRs of one repository
|
|
|
|
func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) {
|
|
|
|
opt.setDefaults()
|
|
|
|
prs := make([]*PullRequest, 0, opt.PageSize)
|
|
|
|
|
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls", owner, repo))
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-01-16 03:52:26 +00:00
|
|
|
return prs, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs)
|
2016-10-11 20:52:07 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// GetPullRequest get information of one PR
|
2016-10-11 20:52:07 +01:00
|
|
|
func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, error) {
|
|
|
|
pr := new(PullRequest)
|
|
|
|
return pr, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index), nil, nil, pr)
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// CreatePullRequestOption options when creating a pull request
|
|
|
|
type CreatePullRequestOption struct {
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// CreatePullRequest create pull request with options
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, error) {
|
2016-10-11 20:52:07 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pr := new(PullRequest)
|
|
|
|
return pr, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo),
|
|
|
|
jsonHeader, bytes.NewReader(body), pr)
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// EditPullRequestOption options when modify pull request
|
|
|
|
type EditPullRequestOption struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
Assignee string `json:"assignee"`
|
|
|
|
Assignees []string `json:"assignees"`
|
|
|
|
Milestone int64 `json:"milestone"`
|
|
|
|
Labels []int64 `json:"labels"`
|
2020-02-04 07:58:46 +00:00
|
|
|
State *StateType `json:"state"`
|
2019-10-13 02:34:01 +01:00
|
|
|
Deadline *time.Time `json:"due_date"`
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// EditPullRequest modify pull request with PR id and options
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, error) {
|
2016-10-11 20:52:07 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pr := new(PullRequest)
|
2019-05-17 07:03:42 +01:00
|
|
|
return pr, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index),
|
2016-10-11 20:52:07 +01:00
|
|
|
jsonHeader, bytes.NewReader(body), pr)
|
|
|
|
}
|
|
|
|
|
2020-01-03 06:22:50 +00:00
|
|
|
// MergePullRequestOption options when merging a pull request
|
|
|
|
type MergePullRequestOption struct {
|
|
|
|
// required: true
|
|
|
|
// enum: merge,rebase,rebase-merge,squash
|
|
|
|
Do string `json:"Do" binding:"Required;In(merge,rebase,rebase-merge,squash)"`
|
|
|
|
MergeTitleField string `json:"MergeTitleField"`
|
|
|
|
MergeMessageField string `json:"MergeMessageField"`
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// MergePullRequest merge a PR to repository by PR id
|
2020-04-23 13:51:36 +01:00
|
|
|
func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (bool, error) {
|
2020-01-03 06:22:50 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-04-23 13:51:36 +01:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
status, err := c.getStatusCode("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), jsonHeader, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2020-01-03 06:22:50 +00:00
|
|
|
}
|
2020-04-23 13:51:36 +01:00
|
|
|
return status == 200, nil
|
2016-10-11 20:52:07 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// IsPullRequestMerged test if one PR is merged to one repository
|
2016-10-11 20:52:07 +01:00
|
|
|
func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, error) {
|
|
|
|
statusCode, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusCode == 204, nil
|
2016-08-14 11:32:53 +01:00
|
|
|
}
|