Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 4d94826

Browse files
authored
Merge pull request #11 from go-gitea/api-pull-request-support
API support for Pull Requests
2 parents 0a0a04c + fea83e6 commit 4d94826

File tree

2 files changed

+104
-9
lines changed

2 files changed

+104
-9
lines changed

gitea/gitea.go

+10
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,13 @@ func (c *Client) getParsedResponse(method, path string, header http.Header, body
8888
}
8989
return json.Unmarshal(data, obj)
9090
}
91+
92+
func (c *Client) getStatusCode(method, path string, header http.Header, body io.Reader) (int, error) {
93+
resp, err := c.doRequest(method, path, header, body)
94+
if err != nil {
95+
return -1, err
96+
}
97+
defer resp.Body.Close()
98+
99+
return resp.StatusCode, nil
100+
}

gitea/pull.go

+94-9
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
package gitea
66

77
import (
8+
"bytes"
9+
"encoding/json"
10+
"fmt"
811
"time"
912
)
1013

11-
// PullRequest represents a pull reqesut API object.
14+
// PullRequest represents a pull request API object.
1215
type PullRequest struct {
13-
// Copied from issue.go
1416
ID int64 `json:"id"`
1517
Index int64 `json:"number"`
1618
Poster *User `json:"user"`
@@ -22,16 +24,99 @@ type PullRequest struct {
2224
State StateType `json:"state"`
2325
Comments int `json:"comments"`
2426

25-
HeadBranch string `json:"head_branch"`
26-
HeadRepo *Repository `json:"head_repo"`
27-
BaseBranch string `json:"base_branch"`
28-
BaseRepo *Repository `json:"base_repo"`
27+
HTMLURL string `json:"html_url"`
28+
DiffURL string `json:"diff_url"`
29+
PatchURL string `json:"patch_url"`
2930

30-
HTMLURL string `json:"html_url"`
31-
32-
Mergeable *bool `json:"mergeable"`
31+
Mergeable bool `json:"mergeable"`
3332
HasMerged bool `json:"merged"`
3433
Merged *time.Time `json:"merged_at"`
3534
MergedCommitID *string `json:"merge_commit_sha"`
3635
MergedBy *User `json:"merged_by"`
36+
37+
Base *PRBranchInfo `json:"base"`
38+
Head *PRBranchInfo `json:"head"`
39+
MergeBase string `json:"merge_base"`
40+
}
41+
42+
type PRBranchInfo struct {
43+
Name string `json:"label"`
44+
Ref string `json:"ref"`
45+
Sha string `json:"sha"`
46+
RepoID int64 `json:"repo_id"`
47+
Repository *Repository `json:"repo"`
48+
}
49+
50+
type ListPullRequestsOptions struct {
51+
Page int `json:"page"`
52+
State string `json:"state"`
53+
}
54+
55+
func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) {
56+
body, err := json.Marshal(&opt)
57+
if err != nil {
58+
return nil, err
59+
}
60+
prs := make([]*PullRequest, 0, 10)
61+
return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, bytes.NewReader(body), &prs)
62+
}
63+
64+
func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, error) {
65+
pr := new(PullRequest)
66+
return pr, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index), nil, nil, pr)
67+
}
68+
69+
type CreatePullRequestOption struct {
70+
Head string `json:"head" binding:"Required"`
71+
Base string `json:"base" binding:"Required"`
72+
Title string `json:"title" binding:"Required"`
73+
Body string `json:"body"`
74+
Assignee string `json:"assignee"`
75+
Milestone int64 `json:"milestone"`
76+
Labels []int64 `json:"labels"`
77+
}
78+
79+
func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, error) {
80+
body, err := json.Marshal(&opt)
81+
if err != nil {
82+
return nil, err
83+
}
84+
pr := new(PullRequest)
85+
return pr, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo),
86+
jsonHeader, bytes.NewReader(body), pr)
87+
}
88+
89+
type EditPullRequestOption struct {
90+
Title string `json:"title"`
91+
Body string `json:"body"`
92+
Assignee string `json:"assignee"`
93+
Milestone int64 `json:"milestone"`
94+
Labels []int64 `json:"labels"`
95+
State *string `json:"state"`
96+
}
97+
98+
func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, error) {
99+
body, err := json.Marshal(&opt)
100+
if err != nil {
101+
return nil, err
102+
}
103+
pr := new(PullRequest)
104+
return pr, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
105+
jsonHeader, bytes.NewReader(body), pr)
106+
}
107+
108+
func (c *Client) MergePullRequest(owner, repo string, index int64) error {
109+
_, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
110+
return err
111+
}
112+
113+
func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, error) {
114+
statusCode, err := c.getStatusCode("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), nil, nil)
115+
116+
if err != nil {
117+
return false, err
118+
}
119+
120+
return statusCode == 204, nil
121+
37122
}

0 commit comments

Comments
 (0)