55package bitbucket
66
77import (
8+ "bytes"
89 "context"
910 "fmt"
11+ "strings"
12+ "time"
1013
1114 "github.com/jenkins-x/go-scm/scm"
1215)
@@ -15,6 +18,8 @@ type pullService struct {
1518 * issueService
1619}
1720
21+ const debugDump = false
22+
1823func (s * pullService ) Find (ctx context.Context , repo string , number int ) (* scm.PullRequest , * scm.Response , error ) {
1924 path := fmt .Sprintf ("2.0/repositories/%s/pullrequests/%d" , repo , number )
2025 out := new (pullRequest )
@@ -25,6 +30,12 @@ func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.P
2530func (s * pullService ) List (ctx context.Context , repo string , opts scm.PullRequestListOptions ) ([]* scm.PullRequest , * scm.Response , error ) {
2631 path := fmt .Sprintf ("2.0/repositories/%s/pullrequests?%s" , repo , encodePullRequestListOptions (opts ))
2732 out := new (pullRequests )
33+ if debugDump {
34+ var buf bytes.Buffer
35+ res , err := s .client .do (ctx , "GET" , path , nil , & buf )
36+ fmt .Printf ("%s\n " , buf .String ())
37+ return nil , res , err
38+ }
2839 res , err := s .client .do (ctx , "GET" , path , nil , out )
2940 copyPagination (out .pagination , res )
3041 return convertPullRequests (out ), res , err
@@ -48,17 +59,102 @@ func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.
4859 return nil , scm .ErrNotSupported
4960}
5061
51- type pullRequest struct {}
62+ type prCommit struct {
63+ LatestCommit string `json:"hash"`
64+ }
65+
66+ type prSource struct {
67+ Commit struct {
68+ Type string `json:"type"`
69+ Ref string `json:"ref"`
70+ Commit string `json:"hash"`
71+ } `json:"commit"`
72+ Repository repository `json:"repository"`
73+ Branch struct {
74+ Name string `json:"name"`
75+ } `json:"branch"`
76+ }
77+
78+ type prDestination struct {
79+ Commit struct {
80+ Type string `json:"type"`
81+ Ref string `json:"ref"`
82+ Commit string `json:"Commit"`
83+ } `json:"commit"`
84+ Repository repository `json:"repository"`
85+ Branch struct {
86+ Name string `json:"name"`
87+ } `json:"branch"`
88+ }
89+
90+ type pullRequest struct {
91+ ID int `json:"id"`
92+ //Version int `json:"version"`
93+ Title string `json:"title"`
94+ Description string `json:"description"`
95+ State string `json:"state"`
96+ CreatedDate time.Time `json:"created_on"`
97+ UpdatedDate time.Time `json:"updated_on"`
98+ Source prSource `json:"source"`
99+ Destination prDestination `json:"destination"`
100+ Locked bool `json:"locked"`
101+ Author user `json:"author"`
102+ Reviewers []user `json:"reviewers"`
103+ Participants []user `json:"participants"`
104+ Links struct {
105+ Self link `json:"self"`
106+ HTML link `json:"html"`
107+ } `json:"links"`
108+ }
52109
53110type pullRequests struct {
54111 pagination
55112 Values []* pullRequest `json:"values"`
56113}
57114
58- func convertPullRequests (from * pullRequests ) []* scm.PullRequest {
59- return nil
115+ func convertPullRequest (from * pullRequest ) * scm.PullRequest {
116+ // TODO
117+ fork := "false"
118+ closed := strings .ToLower (from .State ) != "open"
119+ return & scm.PullRequest {
120+ Number : from .ID ,
121+ Title : from .Title ,
122+ Body : from .Description ,
123+ Sha : from .Source .Commit .Commit ,
124+ Ref : fmt .Sprintf ("refs/pull-requests/%d/from" , from .ID ),
125+ Source : from .Source .Commit .Commit ,
126+ Target : from .Destination .Commit .Commit ,
127+ Fork : fork ,
128+ Base : convertPullRequestBranch (from .Destination .Commit .Ref , from .Destination .Commit .Commit , from .Destination .Repository ),
129+ Head : convertPullRequestBranch (from .Source .Commit .Ref , from .Source .Commit .Commit , from .Source .Repository ),
130+ Link : from .Links .HTML .Href ,
131+ State : strings .ToLower (from .State ),
132+ Closed : closed ,
133+ Merged : from .State == "MERGED" ,
134+ Created : from .CreatedDate ,
135+ Updated : from .UpdatedDate ,
136+ Author : scm.User {
137+ Login : from .Author .GetLogin (),
138+ Name : from .Author .DisplayName ,
139+ Email : from .Author .EmailAddress ,
140+ Link : from .Author .Links .Self .Href ,
141+ Avatar : from .Author .Links .Avatar .Href ,
142+ },
143+ }
60144}
61145
62- func convertPullRequest (from * pullRequest ) * scm.PullRequest {
63- return nil
146+ func convertPullRequestBranch (ref string , sha string , repo repository ) scm.PullRequestBranch {
147+ return scm.PullRequestBranch {
148+ Ref : ref ,
149+ Sha : sha ,
150+ Repo : * convertRepository (& repo ),
151+ }
152+ }
153+
154+ func convertPullRequests (from * pullRequests ) []* scm.PullRequest {
155+ answer := []* scm.PullRequest {}
156+ for _ , pr := range from .Values {
157+ answer = append (answer , convertPullRequest (pr ))
158+ }
159+ return answer
64160}
0 commit comments