Skip to content

Commit 54fb8f7

Browse files
Merge pull request #58 from chhsia0/repo-link
Set `Repository.Link` in various SCM drivers.
2 parents b346688 + bd5fdc5 commit 54fb8f7

39 files changed

+77
-46
lines changed

scm/driver/bitbucket/bitbucket.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ type pagination struct {
115115
Next string `json:"next"`
116116
}
117117

118+
// link represents Bitbucket link properties embedded in responses.
119+
type link struct {
120+
Href string `json:"href"`
121+
}
122+
118123
// Error represents a Bitbucket error.
119124
type Error struct {
120125
Type string `json:"type"`

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 {

scm/driver/bitbucket/user.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,10 @@ func (s *userService) FindEmail(ctx context.Context) (string, *scm.Response, err
3535
type user struct {
3636
Login string `json:"username"`
3737
Name string `json:"display_name"`
38-
Links links `json:"links"`
39-
}
40-
41-
type links struct {
42-
HTML link `json:"html"`
43-
Avatar link `json:"avatar"`
44-
}
45-
46-
type link struct {
47-
Href string `json:"href"`
38+
Links struct {
39+
HTML link `json:"html"`
40+
Avatar link `json:"avatar"`
41+
} `json:"links"`
4842
}
4943

5044
func convertUser(from *user) *scm.User {

scm/driver/gitea/repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func convertRepository(src *repository) *scm.Repository {
189189
Private: src.Private,
190190
Clone: src.CloneURL,
191191
CloneSSH: src.SSHURL,
192+
Link: src.HTMLURL,
192193
}
193194
}
194195

scm/driver/gitea/testdata/repo.json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"Private": true,
1212
"Clone": "https://try.gitea.io/go-gitea/gitea.git",
1313
"CloneSSH": "[email protected]:go-gitea/gitea.git",
14-
"Link": "",
14+
"Link": "https://try.gitea.io/go-gitea/gitea",
1515
"Created": "0001-01-01T00:00:00Z",
1616
"Updated": "0001-01-01T00:00:00Z"
1717
}

scm/driver/gitea/testdata/repos.json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"Private": true,
1313
"Clone": "https://try.gitea.io/go-gitea/gitea.git",
1414
"CloneSSH": "[email protected]:go-gitea/gitea.git",
15-
"Link": "",
15+
"Link": "https://try.gitea.io/go-gitea/gitea",
1616
"Created": "0001-01-01T00:00:00Z",
1717
"Updated": "0001-01-01T00:00:00Z"
1818
}

scm/driver/gitea/testdata/webhooks/branch_create.json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"Private": true,
1313
"Clone": "http://try.gitea.io/gogits/hello-world.git",
1414
"CloneSSH": "git@localhost:gogits/hello-world.git",
15-
"Link": "",
15+
"Link": "http://try.gitea.io/gogits/hello-world",
1616
"Created": "0001-01-01T00:00:00Z",
1717
"Updated": "0001-01-01T00:00:00Z"
1818
},

scm/driver/gitea/testdata/webhooks/branch_delete.json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"Private": true,
1313
"Clone": "http://try.gitea.io/gogits/hello-world.git",
1414
"CloneSSH": "git@localhost:gogits/hello-world.git",
15-
"Link": "",
15+
"Link": "http://try.gitea.io/gogits/hello-world",
1616
"Created": "0001-01-01T00:00:00Z",
1717
"Updated": "0001-01-01T00:00:00Z"
1818
},

scm/driver/gitea/testdata/webhooks/issue_comment_created.json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"Private": true,
1010
"Clone": "http://try.gitea.io/gogits/hello-world.git",
1111
"CloneSSH": "git@localhost:gogits/hello-world.git",
12-
"Link": "",
12+
"Link": "http://try.gitea.io/gogits/hello-world",
1313
"Created": "0001-01-01T00:00:00Z",
1414
"Updated": "0001-01-01T00:00:00Z"
1515
},

scm/driver/gitea/testdata/webhooks/issues_opened.json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"Private": true,
1010
"Clone": "http://try.gitea.io/gogits/hello-world.git",
1111
"CloneSSH": "git@localhost:gogits/hello-world.git",
12-
"Link": "",
12+
"Link": "http://try.gitea.io/gogits/hello-world",
1313
"Created": "0001-01-01T00:00:00Z",
1414
"Updated": "0001-01-01T00:00:00Z"
1515
},

0 commit comments

Comments
 (0)