Skip to content

Commit f36a1af

Browse files
author
chhsia0
committed
Implemented PR operations in Bitbucket driver.
1 parent b189c45 commit f36a1af

File tree

8 files changed

+589
-123
lines changed

8 files changed

+589
-123
lines changed

scm/driver/bitbucket/pr.go

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package bitbucket
77
import (
88
"context"
99
"fmt"
10+
"time"
1011

1112
"github.com/drone/go-scm/scm"
1213
)
@@ -17,14 +18,14 @@ type pullService struct {
1718

1819
func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
1920
path := fmt.Sprintf("2.0/repositories/%s/pullrequests/%d", repo, number)
20-
out := new(pullRequest)
21+
out := new(pr)
2122
res, err := s.client.do(ctx, "GET", path, nil, out)
2223
return convertPullRequest(out), res, err
2324
}
2425

2526
func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
2627
path := fmt.Sprintf("2.0/repositories/%s/pullrequests?%s", repo, encodePullRequestListOptions(opts))
27-
out := new(pullRequests)
28+
out := new(prs)
2829
res, err := s.client.do(ctx, "GET", path, nil, out)
2930
copyPagination(out.pagination, res)
3031
return convertPullRequests(out), res, err
@@ -48,17 +49,120 @@ func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.
4849
return nil, scm.ErrNotSupported
4950
}
5051

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+
}
5286

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 {
54111
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"`
56128
}
57129

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
60136
}
61137

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+
}
64168
}

scm/driver/bitbucket/pr_test.go

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,55 @@ import (
1717
)
1818

1919
func TestPullFind(t *testing.T) {
20-
t.Skip()
20+
defer gock.Off()
21+
22+
gock.New("https://api.bitbucket.org").
23+
Get("/2.0/repositories/atlassian/atlaskit/pullrequests/4982").
24+
Reply(200).
25+
Type("application/json").
26+
File("testdata/pr.json")
27+
28+
client, _ := New("https://api.bitbucket.org")
29+
got, _, err := client.PullRequests.Find(context.Background(), "atlassian/atlaskit", 4982)
30+
if err != nil {
31+
t.Error(err)
32+
}
33+
34+
want := new(scm.PullRequest)
35+
raw, _ := ioutil.ReadFile("testdata/pr.json.golden")
36+
json.Unmarshal(raw, want)
37+
38+
if diff := cmp.Diff(got, want); diff != "" {
39+
t.Errorf("Unexpected Results")
40+
t.Log(diff)
41+
}
2142
}
2243

2344
func TestPullList(t *testing.T) {
24-
t.Skip()
45+
defer gock.Off()
46+
47+
gock.New("https://api.bitbucket.org").
48+
Get("/2.0/repositories/atlassian/atlaskit/pullrequests").
49+
MatchParam("pagelen", "30").
50+
MatchParam("page", "1").
51+
Reply(200).
52+
Type("application/json").
53+
File("testdata/prs.json")
54+
55+
client, _ := New("https://api.bitbucket.org")
56+
got, _, err := client.PullRequests.List(context.Background(), "atlassian/atlaskit", scm.PullRequestListOptions{Size: 30, Page: 1})
57+
if err != nil {
58+
t.Error(err)
59+
}
60+
61+
want := []*scm.PullRequest{}
62+
raw, _ := ioutil.ReadFile("testdata/prs.json.golden")
63+
json.Unmarshal(raw, &want)
64+
65+
if diff := cmp.Diff(got, want); diff != "" {
66+
t.Errorf("Unexpected Results")
67+
t.Log(diff)
68+
}
2569
}
2670

2771
func TestPullListChanges(t *testing.T) {
@@ -52,7 +96,18 @@ func TestPullListChanges(t *testing.T) {
5296
}
5397

5498
func TestPullMerge(t *testing.T) {
55-
t.Skip()
99+
defer gock.Off()
100+
101+
gock.New("https://api.bitbucket.org").
102+
Post("2.0/repositories/atlassian/atlaskit/pullrequests/1/merge").
103+
Reply(200).
104+
Type("application/json")
105+
106+
client, _ := New("https://api.bitbucket.org")
107+
_, err := client.PullRequests.Merge(context.Background(), "atlassian/atlaskit", 1)
108+
if err != nil {
109+
t.Error(err)
110+
}
56111
}
57112

58113
func TestPullClose(t *testing.T) {
@@ -62,3 +117,35 @@ func TestPullClose(t *testing.T) {
62117
t.Errorf("Expect Not Supported error")
63118
}
64119
}
120+
121+
func TestPullCreate(t *testing.T) {
122+
defer gock.Off()
123+
124+
gock.New("https://api.bitbucket.org").
125+
Post("/2.0/repositories/atlassian/atlaskit/pullrequests").
126+
Reply(201).
127+
Type("application/json").
128+
File("testdata/pr.json")
129+
130+
input := &scm.PullRequestInput{
131+
Title: "IOS date picker component duplicate March issue",
132+
Body: "IOS date picker component duplicate March issue",
133+
Source: "Lachlan-Vass/ios-date-picker-component-duplicate-marc-1579222909688",
134+
Target: "master",
135+
}
136+
137+
client, _ := New("https://api.bitbucket.org")
138+
got, _, err := client.PullRequests.Create(context.Background(), "atlassian/atlaskit", input)
139+
if err != nil {
140+
t.Error(err)
141+
}
142+
143+
want := new(scm.PullRequest)
144+
raw, _ := ioutil.ReadFile("testdata/pr.json.golden")
145+
json.Unmarshal(raw, want)
146+
147+
if diff := cmp.Diff(got, want); diff != "" {
148+
t.Errorf("Unexpected Results")
149+
t.Log(diff)
150+
}
151+
}

0 commit comments

Comments
 (0)