2016-08-14 11:32:53 +01:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
// 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"
|
|
|
|
|
2019-05-11 16:38:52 +01:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
2016-10-11 20:52:07 +01:00
|
|
|
|
2019-05-11 16:38:52 +01:00
|
|
|
// PullRequest is equal to structs.PullRequest
|
|
|
|
type PullRequest = structs.PullRequest
|
2016-10-11 20:52:07 +01:00
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// ListRepoPullRequests list PRs of one repository
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) ListRepoPullRequests(owner, repo string, opt structs.ListPullRequestsOptions) ([]*PullRequest, error) {
|
2016-10-11 20:52:07 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
prs := make([]*PullRequest, 0, 10)
|
|
|
|
return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, bytes.NewReader(body), &prs)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// CreatePullRequest create pull request with options
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) CreatePullRequest(owner, repo string, opt structs.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)
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// EditPullRequest modify pull request with PR id and options
|
2019-05-11 16:38:52 +01:00
|
|
|
func (c *Client) EditPullRequest(owner, repo string, index int64, opt structs.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)
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// MergePullRequest merge a PR to repository by PR id
|
2016-10-11 20:52:07 +01:00
|
|
|
func (c *Client) MergePullRequest(owner, repo string, index int64) error {
|
|
|
|
_, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|