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

Commit 95c5f7a

Browse files
committed
API support for pull requests
1 parent d8aff57 commit 95c5f7a

File tree

2 files changed

+99
-8
lines changed

2 files changed

+99
-8
lines changed

gogs.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+
}

pull.go

+89-8
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
package gogs
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,95 @@ 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"`
29-
3027
HTMLURL string `json:"html_url"`
3128

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

0 commit comments

Comments
 (0)