@@ -7,6 +7,7 @@ package bitbucket
7
7
import (
8
8
"context"
9
9
"fmt"
10
+ "time"
10
11
11
12
"github.com/drone/go-scm/scm"
12
13
)
@@ -17,14 +18,14 @@ type pullService struct {
17
18
18
19
func (s * pullService ) Find (ctx context.Context , repo string , number int ) (* scm.PullRequest , * scm.Response , error ) {
19
20
path := fmt .Sprintf ("2.0/repositories/%s/pullrequests/%d" , repo , number )
20
- out := new (pullRequest )
21
+ out := new (pr )
21
22
res , err := s .client .do (ctx , "GET" , path , nil , out )
22
23
return convertPullRequest (out ), res , err
23
24
}
24
25
25
26
func (s * pullService ) List (ctx context.Context , repo string , opts scm.PullRequestListOptions ) ([]* scm.PullRequest , * scm.Response , error ) {
26
27
path := fmt .Sprintf ("2.0/repositories/%s/pullrequests?%s" , repo , encodePullRequestListOptions (opts ))
27
- out := new (pullRequests )
28
+ out := new (prs )
28
29
res , err := s .client .do (ctx , "GET" , path , nil , out )
29
30
copyPagination (out .pagination , res )
30
31
return convertPullRequests (out ), res , err
@@ -48,17 +49,120 @@ func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.
48
49
return nil , scm .ErrNotSupported
49
50
}
50
51
51
- type pullRequest struct {}
52
+ func (s * pullService ) Create (ctx context.Context , repo string , input * scm.PullRequestInput ) (* scm.PullRequest , * scm.Response , error ) {
53
+ path := fmt .Sprintf ("2.0/repositories/%s/pullrequests" , repo )
54
+ in := new (prInput )
55
+ in .Title = input .Title
56
+ in .Description = input .Body
57
+ in .Source .Branch .Name = input .Source
58
+ in .Destination .Branch .Name = input .Target
59
+ out := new (pr )
60
+ res , err := s .client .do (ctx , "POST" , path , in , out )
61
+ return convertPullRequest (out ), res , err
62
+ }
63
+
64
+ type reference struct {
65
+ Commit struct {
66
+ Hash string `json:"hash"`
67
+ Links struct {
68
+ Self link `json:"self"`
69
+ } `json:"links"`
70
+ } `json:"commit"`
71
+ Branch struct {
72
+ Name string `json:"name"`
73
+ } `json:"branch"`
74
+ Repository struct {
75
+ FullName string `json:"full_name"`
76
+ Type string `json:"type"`
77
+ Name string `json:"name"`
78
+ Links struct {
79
+ Self link `json:"self"`
80
+ HTML link `json:"html"`
81
+ Avatar link `json:"avatar"`
82
+ } `json:"links"`
83
+ UUID string `json:"uuid"`
84
+ } `json:"repository"`
85
+ }
52
86
53
- type pullRequests struct {
87
+ type pr struct {
88
+ Description string `json:"description"`
89
+ Links struct {
90
+ HTML link `json:"html"`
91
+ Diff link `json:"diff"`
92
+ } `json:"links"`
93
+ Title string `json:"title"`
94
+ ID int `json:"id"`
95
+ Destination reference `json:"destination"`
96
+ CommentCount int `json:"comment_count"`
97
+ Summary struct {
98
+ Raw string `json:"raw"`
99
+ Markup string `json:"markup"`
100
+ HTML string `json:"html"`
101
+ Type string `json:"type"`
102
+ } `json:"summary"`
103
+ Source reference `json:"source"`
104
+ State string `json:"state"`
105
+ Author user `json:"author"`
106
+ CreatedOn time.Time `json:"created_on"`
107
+ UpdatedOn time.Time `json:"updated_on"`
108
+ }
109
+
110
+ type prs struct {
54
111
pagination
55
- Values []* pullRequest `json:"values"`
112
+ Values []* pr `json:"values"`
113
+ }
114
+
115
+ type prInput struct {
116
+ Title string `json:"title"`
117
+ Description string `json:"description"`
118
+ Source struct {
119
+ Branch struct {
120
+ Name string `json:"name"`
121
+ } `json:"branch"`
122
+ } `json:"source"`
123
+ Destination struct {
124
+ Branch struct {
125
+ Name string `json:"name"`
126
+ } `json:"branch"`
127
+ } `json:"destination"`
56
128
}
57
129
58
- func convertPullRequests (from * pullRequests ) []* scm.PullRequest {
59
- return nil
130
+ func convertPullRequests (from * prs ) []* scm.PullRequest {
131
+ to := []* scm.PullRequest {}
132
+ for _ , v := range from .Values {
133
+ to = append (to , convertPullRequest (v ))
134
+ }
135
+ return to
60
136
}
61
137
62
- func convertPullRequest (from * pullRequest ) * scm.PullRequest {
63
- return nil
138
+ func convertPullRequest (from * pr ) * scm.PullRequest {
139
+ return & scm.PullRequest {
140
+ Number : from .ID ,
141
+ Title : from .Title ,
142
+ Body : from .Description ,
143
+ Sha : from .Source .Commit .Hash ,
144
+ Source : from .Source .Branch .Name ,
145
+ Target : from .Destination .Branch .Name ,
146
+ Fork : from .Source .Repository .FullName ,
147
+ Link : from .Links .HTML .Href ,
148
+ Closed : from .State != "OPEN" ,
149
+ Merged : from .State == "MERGED" ,
150
+ Head : scm.Reference {
151
+ Name : from .Source .Branch .Name ,
152
+ Path : scm .ExpandRef (from .Source .Branch .Name , "refs/heads" ),
153
+ Sha : from .Source .Commit .Hash ,
154
+ },
155
+ Base : scm.Reference {
156
+ Name : from .Destination .Branch .Name ,
157
+ Path : scm .ExpandRef (from .Destination .Branch .Name , "refs/heads" ),
158
+ Sha : from .Destination .Commit .Hash ,
159
+ },
160
+ Author : scm.User {
161
+ Login : from .Author .Nickname ,
162
+ Name : from .Author .DisplayName ,
163
+ Avatar : from .Author .Links .Avatar .Href ,
164
+ },
165
+ Created : from .CreatedOn ,
166
+ Updated : from .UpdatedOn ,
167
+ }
64
168
}
0 commit comments