55package gitea
66
77import (
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.
1215type 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