5
5
package bitbucket
6
6
7
7
import (
8
+ "bytes"
8
9
"context"
9
10
"fmt"
11
+ "strings"
12
+ "time"
10
13
11
14
"github.com/jenkins-x/go-scm/scm"
12
15
)
@@ -15,6 +18,8 @@ type pullService struct {
15
18
* issueService
16
19
}
17
20
21
+ const debugDump = false
22
+
18
23
func (s * pullService ) Find (ctx context.Context , repo string , number int ) (* scm.PullRequest , * scm.Response , error ) {
19
24
path := fmt .Sprintf ("2.0/repositories/%s/pullrequests/%d" , repo , number )
20
25
out := new (pullRequest )
@@ -25,6 +30,12 @@ func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.P
25
30
func (s * pullService ) List (ctx context.Context , repo string , opts scm.PullRequestListOptions ) ([]* scm.PullRequest , * scm.Response , error ) {
26
31
path := fmt .Sprintf ("2.0/repositories/%s/pullrequests?%s" , repo , encodePullRequestListOptions (opts ))
27
32
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
+ }
28
39
res , err := s .client .do (ctx , "GET" , path , nil , out )
29
40
copyPagination (out .pagination , res )
30
41
return convertPullRequests (out ), res , err
@@ -48,17 +59,102 @@ func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.
48
59
return nil , scm .ErrNotSupported
49
60
}
50
61
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
+ }
52
109
53
110
type pullRequests struct {
54
111
pagination
55
112
Values []* pullRequest `json:"values"`
56
113
}
57
114
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
+ }
60
144
}
61
145
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
64
160
}
0 commit comments