5
5
package gogs
6
6
7
7
import (
8
+ "bytes"
9
+ "encoding/json"
10
+ "fmt"
8
11
"time"
9
12
)
10
13
11
- // PullRequest represents a pull reqesut API object.
14
+ // PullRequest represents a pull request API object.
12
15
type PullRequest struct {
13
- // Copied from issue.go
14
16
ID int64 `json:"id"`
15
17
Index int64 `json:"number"`
16
18
Poster * User `json:"user"`
@@ -22,16 +24,95 @@ type PullRequest struct {
22
24
State StateType `json:"state"`
23
25
Comments int `json:"comments"`
24
26
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
-
30
27
HTMLURL string `json:"html_url"`
31
28
32
- Mergeable * bool `json:"mergeable"`
29
+ Mergeable bool `json:"mergeable"`
33
30
HasMerged bool `json:"merged"`
34
31
Merged * time.Time `json:"merged_at"`
35
32
MergedCommitID * string `json:"merge_commit_sha"`
36
33
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
+
37
118
}
0 commit comments