Skip to content

Commit 6853a77

Browse files
author
chhsia0
committed
Used html and clone links from the Bitbucket payload.
1 parent 2d4a70a commit 6853a77

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

scm/driver/bitbucket/repo.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
"github.com/drone/go-scm/scm"
1414
)
1515

16+
type cloneLink struct {
17+
link
18+
Name string `json:"name"`
19+
}
20+
1621
type repository struct {
1722
UUID string `json:"uuid"`
1823
SCM string `json:"scm"`
@@ -24,6 +29,10 @@ type repository struct {
2429
Type string `json:"type"`
2530
Name string `json:"name"`
2631
} `json:"mainbranch"`
32+
Links struct {
33+
HTML link `json:"html"`
34+
Clone []cloneLink `json:"clone"`
35+
} `json:"links"`
2736
}
2837

2938
type perms struct {
@@ -176,16 +185,36 @@ func convertRepository(from *repository) *scm.Repository {
176185
ID: from.UUID,
177186
Name: name,
178187
Namespace: namespace,
179-
Link: fmt.Sprintf("https://bitbucket.org/%s", from.FullName),
188+
Link: from.Links.HTML.Href,
180189
Branch: from.Mainbranch.Name,
181190
Private: from.IsPrivate,
182-
Clone: fmt.Sprintf("https://bitbucket.org/%s.git", from.FullName),
183-
CloneSSH: fmt.Sprintf("[email protected]:%s.git", from.FullName),
191+
CloneSSH: extractCloneLink(from.Links.Clone, "ssh"),
192+
Clone: anonymizeLink(extractCloneLink(from.Links.Clone, "https", "http")),
184193
Created: from.CreatedOn,
185194
Updated: from.UpdatedOn,
186195
}
187196
}
188197

198+
func extractCloneLink(links []cloneLink, names ...string) (href string) {
199+
for _, name := range names {
200+
for _, link := range links {
201+
if link.Name == name && link.Href != "" {
202+
return link.Href
203+
}
204+
}
205+
}
206+
return
207+
}
208+
209+
func anonymizeLink(link string) (href string) {
210+
parsed, err := url.Parse(link)
211+
if err != nil {
212+
return link
213+
}
214+
parsed.User = nil
215+
return parsed.String()
216+
}
217+
189218
func convertPerms(from *perms) *scm.Perm {
190219
to := new(scm.Perm)
191220
if len(from.Values) != 1 {

0 commit comments

Comments
 (0)