Skip to content

Commit c01408e

Browse files
committed
fix: add support for get PRs to bitbucket cloud
1 parent 210d615 commit c01408e

File tree

13 files changed

+634
-26
lines changed

13 files changed

+634
-26
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ To help hack on the different drivers here's a list of docs which outline the gi
5555

5656
### Bitbucket Server
5757

58-
* REST API reference: https://docs.atlassian.com/bitbucket-server/rest/6.5.1/bitbucket-rest.html
58+
* REST API reference: https://docs.atlassian.com/bitbucket-server/rest/6.5.1/bitbucket-rest.html
5959
* Webhooks: https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html
6060

61+
### Bitbucket Cloud
62+
63+
* REST API reference: https://developer.atlassian.com/bitbucket/api/2/reference/
64+
6165
### Gitlab
6266

6367
* REST API reference: https://docs.gitlab.com/ee/api/

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/pkg/errors v0.8.1
88
github.com/shurcooL/githubv4 v0.0.0-20190718010115-4ba037080260
99
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
10-
github.com/sirupsen/logrus v1.4.2 // indirect
10+
github.com/sirupsen/logrus v1.4.2
1111
github.com/stretchr/testify v1.3.0
1212
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
1313
k8s.io/apimachinery v0.0.0-20190703205208-4cfb76a8bf76

scm/driver/bitbucket/pr.go

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
package bitbucket
66

77
import (
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+
1823
func (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
2530
func (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

53110
type 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
}

scm/driver/bitbucket/pr_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,37 @@ func TestPullFind(t *testing.T) {
2121
}
2222

2323
func TestPullList(t *testing.T) {
24-
t.Skip()
24+
defer gock.Off()
25+
26+
gock.New("https://api.bitbucket.org").
27+
Get("/2.0/repositories/octocat/hello-world/pullrequests").
28+
MatchParam("pagelen", "30").
29+
MatchParam("page", "1").
30+
MatchParam("state", "all").
31+
Reply(200).
32+
Type("application/json").
33+
//SetHeaders(mockHeaders).
34+
//SetHeaders(mockPageHeaders).
35+
File("testdata/pulls.json")
36+
37+
client := NewDefault()
38+
got, _, err := client.PullRequests.List(context.Background(), "octocat/hello-world", scm.PullRequestListOptions{Page: 1, Size: 30, Open: true, Closed: true})
39+
if err != nil {
40+
t.Error(err)
41+
return
42+
}
43+
44+
want := []*scm.PullRequest{}
45+
raw, _ := ioutil.ReadFile("testdata/pulls.json.golden")
46+
json.Unmarshal(raw, &want)
47+
48+
if diff := cmp.Diff(got, want); diff != "" {
49+
t.Errorf("Unexpected Results")
50+
t.Log(diff)
51+
52+
data, _ := json.Marshal(got)
53+
t.Logf("got JSON: %s", data)
54+
}
2555
}
2656

2757
func TestPullListChanges(t *testing.T) {

scm/driver/bitbucket/repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ func convertRepository(from *repository) *scm.Repository {
195195
ID: from.UUID,
196196
Name: name,
197197
Namespace: namespace,
198+
FullName: from.FullName,
198199
Link: fmt.Sprintf("https://bitbucket.org/%s", from.FullName),
199200
Branch: from.Mainbranch.Name,
200201
Private: from.IsPrivate,

scm/driver/bitbucket/repo_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ func TestRepositoryList(t *testing.T) {
112112
client, _ := New("https://api.bitbucket.org")
113113

114114
for {
115-
repos, res, err := client.Repositories.List(context.Background(), opts)
115+
ctx := context.Background()
116+
repos, res, err := client.Repositories.List(ctx, opts)
116117
if err != nil {
117118
t.Error(err)
118119
}

0 commit comments

Comments
 (0)