Skip to content

Commit cc305a6

Browse files
[IAC-941]: PR comment creation for BitBucket (#265)
BitBucket doesn't implement support for PR comments or issues comments. The current implementation of "pullService" in BitBucket defers to the implementation of "issueService", which itself returns a "not implemented" error for all comment-related calls. PR comments and issue comments are separate in BitBucket so this commit adds explicit implementations of the comment functions to the "pullService". Most remain "not implemented" with the exception of comment creation which is now supported.
1 parent 0f7857f commit cc305a6

File tree

7 files changed

+158
-13
lines changed

7 files changed

+158
-13
lines changed

go.sum

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
31
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
42
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
53
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=
64
github.com/h2non/gock v1.0.9/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
75
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
86
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
9-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
13-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
15-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
16-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
17-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

scm/driver/bitbucket/bitbucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func New(uri string) (*scm.Client, error) {
3636
client.Issues = &issueService{client}
3737
client.Milestones = &milestoneService{client}
3838
client.Organizations = &organizationService{client}
39-
client.PullRequests = &pullService{&issueService{client}}
39+
client.PullRequests = &pullService{client}
4040
client.Repositories = &repositoryService{client}
4141
client.Releases = &releaseService{client}
4242
client.Reviews = &reviewService{client}

scm/driver/bitbucket/pr.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
type pullService struct {
16-
*issueService
16+
client *wrapper
1717
}
1818

1919
func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
@@ -69,6 +69,27 @@ func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRe
6969
return convertPullRequest(out), res, err
7070
}
7171

72+
func (s *pullService) FindComment(ctx context.Context, repo string, index, id int) (*scm.Comment, *scm.Response, error) {
73+
return nil, nil, scm.ErrNotSupported
74+
}
75+
76+
func (s *pullService) ListComments(ctx context.Context, repo string, index int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
77+
return nil, nil, scm.ErrNotSupported
78+
}
79+
80+
func (s *pullService) CreateComment(ctx context.Context, repo string, number int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
81+
path := fmt.Sprintf("2.0/repositories/%s/pullrequests/%d/comments", repo, number)
82+
in := &prCommentInput{}
83+
in.Content.Raw = input.Body
84+
out := new(prComment)
85+
res, err := s.client.do(ctx, "POST", path, in, out)
86+
return convertPullRequestComment(out), res, err
87+
}
88+
89+
func (s *pullService) DeleteComment(ctx context.Context, repo string, number, id int) (*scm.Response, error) {
90+
return nil, scm.ErrNotSupported
91+
}
92+
7293
type reference struct {
7394
Commit struct {
7495
Hash string `json:"hash"`
@@ -180,3 +201,18 @@ func convertPullRequest(from *pr) *scm.PullRequest {
180201
Updated: from.UpdatedOn,
181202
}
182203
}
204+
205+
func convertPullRequestComment(from *prComment) *scm.Comment {
206+
return &scm.Comment{
207+
ID: from.ID,
208+
Body: from.Content.Raw,
209+
Author: scm.User{
210+
ID: from.User.UUID,
211+
Login: from.User.Nickname,
212+
Name: from.User.DisplayName,
213+
Avatar: from.User.Links.Avatar.Href,
214+
},
215+
Created: from.CreatedOn,
216+
Updated: from.UpdatedOn,
217+
}
218+
}

scm/driver/bitbucket/pr_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,53 @@ func TestPullListCommits(t *testing.T) {
175175
t.Log(diff)
176176
}
177177
}
178+
179+
func TestPullRequestCommentFind(t *testing.T) {
180+
_, _, err := NewDefault().PullRequests.FindComment(context.Background(), "", 0, 0)
181+
if err != scm.ErrNotSupported {
182+
t.Errorf("Expect Not Supported error")
183+
}
184+
}
185+
186+
func TestPullRequestListComments(t *testing.T) {
187+
_, _, err := NewDefault().PullRequests.ListComments(context.Background(), "", 0, scm.ListOptions{})
188+
if err != scm.ErrNotSupported {
189+
t.Errorf("Expect Not Supported error")
190+
}
191+
}
192+
193+
func TestPullRequestCreateComment(t *testing.T) {
194+
defer gock.Off()
195+
196+
gock.New("https://api.bitbucket.org").
197+
Post("/2.0/repositories/atlassian/atlaskit/pullrequests/12").
198+
Reply(201).
199+
Type("application/json").
200+
File("testdata/prcomment.json")
201+
202+
input := &scm.CommentInput{
203+
Body: "Lovely comment",
204+
}
205+
206+
client, _ := New("https://api.bitbucket.org")
207+
got, _, err := client.PullRequests.CreateComment(context.Background(), "atlassian/atlaskit", 12, input)
208+
if err != nil {
209+
t.Error(err)
210+
}
211+
212+
want := new(scm.Comment)
213+
raw, _ := ioutil.ReadFile("testdata/prcomment.json.golden")
214+
json.Unmarshal(raw, want)
215+
216+
if diff := cmp.Diff(got, want); diff != "" {
217+
t.Errorf("Unexpected Results")
218+
t.Log(diff)
219+
}
220+
}
221+
222+
func TestPullRequestCommentDelete(t *testing.T) {
223+
_, err := NewDefault().PullRequests.DeleteComment(context.Background(), "", 0, 0)
224+
if err != scm.ErrNotSupported {
225+
t.Errorf("Expect Not Supported error")
226+
}
227+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"id": 419169807,
3+
"created_on": "2023-08-14T11:38:53.460132+00:00",
4+
"updated_on": "2023-08-14T11:38:53.460205+00:00",
5+
"content": {
6+
"type": "rendered",
7+
"raw": "Lovely comment",
8+
"markup": "markdown",
9+
"html": "<p>Lovely comment<\/p>"
10+
},
11+
"user": {
12+
"display_name": "Brian Jacobson",
13+
"links": {
14+
"self": {
15+
"href": "https:\/\/bitbucket.org\/!api\/2.0\/users\/%7B6dff94-1b8b-4f62-b37f-3069e13dfdf33e%7D"
16+
},
17+
"avatar": {
18+
"href": "http:\/\/localhost:3000\/avatars\/1"
19+
},
20+
"html": {
21+
"href": "https:\/\/bitbucket.org\/%7B6dff94-1b8b-4f62-b37f-3069e13dfdf33e%7D\/"
22+
}
23+
},
24+
"type": "user",
25+
"uuid": "{6b408a94-1b8b-4f62-b37f-3069e13bc33e}",
26+
"account_id": "60259ce8164527007100d945",
27+
"nickname": "brian.jacobson"
28+
},
29+
"deleted": false,
30+
"type": "pullrequest_comment",
31+
"links": {
32+
"self": {
33+
"href": "https:\/\/bitbucket.org\/!api\/2.0\/repositories\/brianharness\/test\/pullrequests\/3\/comments\/419169807"
34+
},
35+
"html": {
36+
"href": "https:\/\/bitbucket.org\/brianharness\/test\/pull-requests\/3\/_\/diff#comment-419169807"
37+
}
38+
},
39+
"pullrequest": {
40+
"type": "pullrequest",
41+
"id": 3,
42+
"title": "README.md edited online with Bitbucket",
43+
"links": {
44+
"self": {
45+
"href": "https:\/\/bitbucket.org\/!api\/2.0\/repositories\/brianharness\/test\/pullrequests\/3"
46+
},
47+
"html": {
48+
"href": "https:\/\/bitbucket.org\/brianharness\/test\/pull-requests\/3"
49+
}
50+
}
51+
}
52+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"ID": 419169807,
3+
"Body": "Lovely comment",
4+
"Author": {
5+
"ID": "{6b408a94-1b8b-4f62-b37f-3069e13bc33e}",
6+
"Login": "brian.jacobson",
7+
"Name": "Brian Jacobson",
8+
"Avatar": "http://localhost:3000/avatars/1"
9+
},
10+
"Created": "2023-08-14T11:38:53.460132+00:00",
11+
"Updated": "2023-08-14T11:38:53.460205+00:00"
12+
}

scm/driver/bitbucket/webhook.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,12 @@ type (
405405
UUID string `json:"uuid"`
406406
}
407407

408+
prCommentInput struct {
409+
Content struct {
410+
Raw string `json:"raw"`
411+
} `json:"content"`
412+
}
413+
408414
prComment struct {
409415
Links struct {
410416
Self link `json:"self"`

0 commit comments

Comments
 (0)