go-sdk/gitea/pull.go

152 lines
4.8 KiB
Go
Raw Normal View History

2016-08-14 16:02:53 +05:30
// 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.
package gitea
2016-08-14 16:02:53 +05:30
import (
2016-10-12 01:22:07 +05:30
"bytes"
"encoding/json"
"fmt"
2016-08-14 16:02:53 +05:30
"time"
)
2017-11-12 14:40:33 +05:30
// PullRequest represents a pull request
2016-08-14 16:02:53 +05:30
type PullRequest struct {
ID int64 `json:"id"`
URL string `json:"url"`
2016-08-14 16:02:53 +05:30
Index int64 `json:"number"`
2016-08-16 22:46:51 +05:30
Poster *User `json:"user"`
2016-08-14 16:02:53 +05:30
Title string `json:"title"`
Body string `json:"body"`
Labels []*Label `json:"labels"`
Milestone *Milestone `json:"milestone"`
Assignee *User `json:"assignee"`
Assignees []*User `json:"assignees"`
2016-08-16 22:46:51 +05:30
State StateType `json:"state"`
2016-08-14 16:02:53 +05:30
Comments int `json:"comments"`
2016-11-02 05:06:09 +05:30
HTMLURL string `json:"html_url"`
DiffURL string `json:"diff_url"`
PatchURL string `json:"patch_url"`
Mergeable bool `json:"mergeable"`
HasMerged bool `json:"merged"`
2017-11-12 14:40:33 +05:30
// swagger:strfmt date-time
2016-08-14 16:02:53 +05:30
Merged *time.Time `json:"merged_at"`
MergedCommitID *string `json:"merge_commit_sha"`
MergedBy *User `json:"merged_by"`
2016-10-12 01:22:07 +05:30
Base *PRBranchInfo `json:"base"`
Head *PRBranchInfo `json:"head"`
MergeBase string `json:"merge_base"`
// swagger:strfmt date-time
Deadline *time.Time `json:"due_date"`
2017-11-12 14:40:33 +05:30
// swagger:strfmt date-time
Created *time.Time `json:"created_at"`
2017-11-12 14:40:33 +05:30
// swagger:strfmt date-time
Updated *time.Time `json:"updated_at"`
// swagger:strfmt date-time
Closed *time.Time `json:"closed_at"`
2016-10-12 01:22:07 +05:30
}
2017-11-12 14:40:33 +05:30
// PRBranchInfo information about a branch
2016-10-12 01:22:07 +05:30
type PRBranchInfo struct {
2016-11-02 05:06:09 +05:30
Name string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
2016-10-12 01:22:07 +05:30
RepoID int64 `json:"repo_id"`
Repository *Repository `json:"repo"`
}
2017-11-12 14:40:33 +05:30
// ListPullRequestsOptions options for listing pull requests
2016-10-12 01:22:07 +05:30
type ListPullRequestsOptions struct {
Page int `json:"page"`
State string `json:"state"`
}
2016-11-10 15:14:00 +05:30
// ListRepoPullRequests list PRs of one repository
2016-10-12 01:22:07 +05:30
func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) {
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 15:14:00 +05:30
// GetPullRequest get information of one PR
2016-10-12 01:22:07 +05:30
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 15:14:00 +05:30
// CreatePullRequestOption options when creating a pull request
2016-10-12 01:22:07 +05:30
type CreatePullRequestOption struct {
Head string `json:"head" binding:"Required"`
Base string `json:"base" binding:"Required"`
Title string `json:"title" binding:"Required"`
Body string `json:"body"`
Assignee string `json:"assignee"`
Assignees []string `json:"assignees"`
Milestone int64 `json:"milestone"`
Labels []int64 `json:"labels"`
// swagger:strfmt date-time
Deadline *time.Time `json:"due_date"`
2016-10-12 01:22:07 +05:30
}
2016-11-10 15:14:00 +05:30
// CreatePullRequest create pull request with options
2016-10-12 01:22:07 +05:30
func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, error) {
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 15:14:00 +05:30
// EditPullRequestOption options when modify pull request
2016-10-12 01:22:07 +05:30
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"`
State *string `json:"state"`
// swagger:strfmt date-time
Deadline *time.Time `json:"due_date"`
2016-10-12 01:22:07 +05:30
}
2016-11-10 15:14:00 +05:30
// EditPullRequest modify pull request with PR id and options
2016-10-12 01:22:07 +05:30
func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
pr := new(PullRequest)
return pr, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
jsonHeader, bytes.NewReader(body), pr)
}
2016-11-10 15:14:00 +05:30
// MergePullRequest merge a PR to repository by PR id
2016-10-12 01:22:07 +05:30
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 15:14:00 +05:30
// IsPullRequestMerged test if one PR is merged to one repository
2016-10-12 01:22:07 +05:30
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 16:02:53 +05:30
}