Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bitbucket Server] Implements the GetMe method #128

Open
wants to merge 19 commits into
base: bitbucket-server
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
require (
github.com/Masterminds/sprig/v3 v3.2.2
github.com/PuerkitoBio/goquery v1.8.0
github.com/gfleury/go-bitbucket-v1 v0.0.0-20230830121038-6e30c5760c87
github.com/gorilla/mux v1.8.0
github.com/mattermost/mattermost-plugin-api v0.0.22
github.com/mattermost/mattermost-server/v6 v6.5.2
Expand Down
24 changes: 20 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYis
github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc=
github.com/getsentry/sentry-go v0.11.0/go.mod h1:KBQIxiZAetw62Cj8Ri964vAEWVdgfaUCn30Q3bCvANo=
github.com/getsentry/sentry-go v0.12.0/go.mod h1:NSap0JBYWzHND8oMbyi0+XZhUalc1TBdRL1M71JZW2c=
github.com/gfleury/go-bitbucket-v1 v0.0.0-20230830121038-6e30c5760c87 h1:aix/N0cwFcyaYksDTa96rcila54zTQ6Nk6yENtgO7yM=
github.com/gfleury/go-bitbucket-v1 v0.0.0-20230830121038-6e30c5760c87/go.mod h1:6saoZ1uJyRD/w4t5Djj8mik5W9cLjSKvba6ZaryV+98=
github.com/gfleury/go-bitbucket-v1/test/bb-mock-server v0.0.0-20230825095122-9bc1711434ab h1:BeG9dDWckFi/p5Gvqq3wTEDXsUV4G6bdvjEHMOT2B8E=
github.com/gfleury/go-bitbucket-v1/test/bb-mock-server v0.0.0-20230825095122-9bc1711434ab/go.mod h1:VssB0kb1cETNaFFC/0mHVCj+7i5TS2xraYq+tl9JLwE=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gigawattio/window v0.0.0-20180317192513-0f5467e35573/go.mod h1:eBvb3i++NHDH4Ugo9qCvMw8t0mTSctaEa5blJbWcNxs=
Expand Down Expand Up @@ -1036,6 +1040,7 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F
github.com/mitchellh/mapstructure v0.0.0-20180220230111-00c29f56e238/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
Expand Down Expand Up @@ -1449,8 +1454,9 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg=
github.com/yuin/goldmark v1.4.5 h1:4OEQwtW2uLXjEdgnGM3Vg652Pq37X7NOIRzFWb3BzIc=
github.com/yuin/goldmark v1.4.5/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
Expand Down Expand Up @@ -1582,6 +1588,7 @@ golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -1659,8 +1666,10 @@ golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.0.0-20180227000427-d7d64896b5ff/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -1697,6 +1706,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -1822,11 +1832,15 @@ golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220207234003-57398862261d h1:Bm7BNOQt2Qv7ZqysjeLjgCBanX+88Z/OtdvsrEv1Djc=
golang.org/x/sys v0.0.0-20220207234003-57398862261d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -1835,8 +1849,9 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -1925,6 +1940,7 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
3 changes: 2 additions & 1 deletion server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"golang.org/x/oauth2"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
)

const (
Expand Down Expand Up @@ -154,7 +155,7 @@ func checkPluginRequest(next http.HandlerFunc) http.HandlerFunc {
}
}

func (p *Plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (p *Plugin) ServeHTTP(_ *plugin.Context, w http.ResponseWriter, r *http.Request) {
config := p.getConfiguration()

if err := config.IsValid(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestPlugin_ServeHTTP(t *testing.T) {
req := test.httpTest.CreateHTTPRequest(test.request)
req.Header.Add("Mattermost-User-ID", test.userID)
rr := httptest.NewRecorder()
p.ServeHTTP(rr, req)
p.ServeHTTP(&plugin.Context{}, rr, req)
test.httpTest.CompareHTTPResponse(rr, test.expectedResponse)
})
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestGetToken(t *testing.T) {
req := test.httpTest.CreateHTTPRequest(test.request)
rr := httptest.NewRecorder()

p.ServeHTTP(rr, req)
p.ServeHTTP(test.context, rr, req)

test.httpTest.CompareHTTPResponse(rr, test.expectedResponse)
})
Expand Down
14 changes: 14 additions & 0 deletions server/bitbucket_server/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package bitbucket_server

import bitbucketv1 "github.com/gfleury/go-bitbucket-v1"

type Client interface {
GetMe(accessToken string) (*BitbucketUser, error)
}

type BitbucketClient struct {
apiClient *bitbucketv1.APIClient

selfHostedURL string
selfHostedAPIURL string
}
93 changes: 93 additions & 0 deletions server/bitbucket_server/client_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package bitbucket_server

import (
"encoding/json"
"fmt"
"io"
"net/http"

bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
"github.com/pkg/errors"
)

// TODO: This will be changed to create the main structs when modularized
type Link struct {
Href string `json:"href"`
}

type BitbucketUser struct {
AccountID int `json:"id"`
Username string `json:"name"`
Links struct {
Self []Link `json:"self"`
} `json:"links"`
}

type BitbucketServerClient struct {
BitbucketClient
}

func newServerClient(selfHostedURL string, selfHostedAPIURL string, apiClient *bitbucketv1.APIClient) Client {
return &BitbucketServerClient{
BitbucketClient: BitbucketClient{
apiClient: apiClient,
selfHostedURL: selfHostedURL,
selfHostedAPIURL: selfHostedAPIURL,
},
}
}

func (c *BitbucketServerClient) getWhoAmI(accessToken string) (string, error) {
requestURL := fmt.Sprintf("%s/plugins/servlet/applinks/whoami", c.selfHostedURL)

req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return "", err
}

client := &http.Client{}

req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", accessToken))

resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return "", errors.Errorf("who am i returned non-200 status code: %d", resp.StatusCode)
}

user, err := io.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "failed to read response")
}

return string(user), nil
}

func (c *BitbucketServerClient) GetMe(accessToken string) (*BitbucketUser, error) {
username, err := c.getWhoAmI(accessToken)
if err != nil {
return nil, err
}

resp, err := c.apiClient.DefaultApi.GetUser(username)
if err != nil {
return nil, err
}

jsonData, err := json.Marshal(resp.Values)
if err != nil {
return nil, err
}

var user BitbucketUser
err = json.Unmarshal(jsonData, &user)
if err != nil {
return nil, err
}

return &user, nil
}
131 changes: 131 additions & 0 deletions server/bitbucket_server/client_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package bitbucket_server

import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"golang.org/x/oauth2"
)

type MockAPIClient struct {
mock.Mock
}

func (m *MockAPIClient) GetUser(username string) (*http.Response, error) {
args := m.Called(username)
mockResp := mockHTTPResponse(200, `{"id": 1, "name": "John Doe", "links": {"self": [{"href": "http://example.com"}]}}`)
return mockResp, args.Error(1)
}

func mockHTTPResponse(statusCode int, responseBody string) *http.Response {
return &http.Response{
StatusCode: statusCode,
Body: io.NopCloser(bytes.NewBufferString(responseBody)),
}
}

func TestGetWhoAmI(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization")
if authHeader != "Bearer valid-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("mocked-user"))
}))
defer server.Close()

tests := []struct {
name string
token oauth2.Token
expectedResult string
expectedError string
}{
{
name: "valid token",
token: oauth2.Token{AccessToken: "valid-token"},
expectedResult: "mocked-user",
expectedError: "",
},
{
name: "invalid token",
token: oauth2.Token{AccessToken: "invalid-token"},
expectedResult: "",
expectedError: "who am i returned non-200 status code: 401",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
client := NewClientServer()

Check failure on line 68 in server/bitbucket_server/client_server_test.go

View workflow job for this annotation

GitHub Actions / plugin-ci / test

undefined: NewClientServer

Check failure on line 68 in server/bitbucket_server/client_server_test.go

View workflow job for this annotation

GitHub Actions / plugin-ci / lint

undefined: NewClientServer
client.selfHostedURL = server.URL
client.token = tc.token

result, err := client.getWhoAmI()

if tc.expectedError == "" {
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
} else {
assert.Error(t, err)
assert.Equal(t, tc.expectedError, err.Error())
assert.Equal(t, tc.expectedResult, result)
}
})
}
}

func TestBitbucketServerClient_GetMe(t *testing.T) {
tests := []struct {
name string
setupMock func(*MockAPIClient)
expectedUser *BitbucketUser
expectedErr error
}{
{
name: "successful retrieval",
setupMock: func(m *MockAPIClient) {
m.On("GetUser", mock.Anything).Return(mockHTTPResponse(200, `{"id": 1, "name": "John Doe", "links": {"self": [{"href": "http://example.com"}]}}`), nil)
},
expectedUser: &BitbucketUser{ /* expected data */ },
expectedErr: nil,
},
{
name: "getWhoAmI fails",
setupMock: func(m *MockAPIClient) {},
expectedUser: nil,
expectedErr: errors.New("getWhoAmI error"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAPIClient := &MockAPIClient{}
tt.setupMock(mockAPIClient)

c := &BitbucketServerClient{
apiClient: mockAPIClient,

Check failure on line 115 in server/bitbucket_server/client_server_test.go

View workflow job for this annotation

GitHub Actions / plugin-ci / test

unknown field apiClient in struct literal of type BitbucketServerClient

Check failure on line 115 in server/bitbucket_server/client_server_test.go

View workflow job for this annotation

GitHub Actions / plugin-ci / lint

unknown field apiClient in struct literal of type BitbucketServerClient
}

user, err := c.GetMe()

Check failure on line 118 in server/bitbucket_server/client_server_test.go

View workflow job for this annotation

GitHub Actions / plugin-ci / test

not enough arguments in call to c.GetMe

Check failure on line 118 in server/bitbucket_server/client_server_test.go

View workflow job for this annotation

GitHub Actions / plugin-ci / lint

not enough arguments in call to c.GetMe

assert.Equal(t, tt.expectedUser, user)
if tt.expectedErr != nil {
assert.Error(t, err)
assert.EqualError(t, err, tt.expectedErr.Error())
} else {
assert.NoError(t, err)
}

mockAPIClient.AssertExpectations(t)
})
}
}
14 changes: 14 additions & 0 deletions server/bitbucket_server/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package bitbucket_server

import (
"fmt"

bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
)

func GetBitbucketClient(clientType string, selfHostedURL string, selfHostedAPIURL string, apiClient *bitbucketv1.APIClient) (Client, error) {
if clientType == "server" {
return newServerClient(selfHostedURL, selfHostedAPIURL, apiClient), nil
}
return nil, fmt.Errorf("wrong client passed")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this package only creates a Bitbucket Server client, and not a Bitbucket Cloud client, what benefit do we get from having this clientType parameter?

Copy link
Author

@panoramix360 panoramix360 Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, the idea is to create the Bitbucket Cloud as well! I didn't insert this yet because we plan to do this more consistently on this issue.

But if you want, I can add the condition here. Let me know what you think!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the function to create the BB Cloud client will be in a separate package though like bitbucket_cloud. What do you think? We'll see how the code feels when instantiating either client from the main package

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change the name of this package to just package bitbucket and have both on the same package.

so we have:

  • client.go: generic client with the necessary methods
  • client_server: concrete server client with the implementation of the Bitbucket Server
  • client_cloud: concrete cloud client with the implementation of the Bitbucket Cloud

What do you think of this approach?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we think that everything to do with Bitbucket Server can/should fit in its own file, then this works well. But if there are multiple files for each client type, I think we should have separate packages for each client type. What do you think?

So either this that I think you're suggesting

client := bitbucket.NewBBServerClient()

or

client := bitbucket_server.NewClient()

Loading
Loading