1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00

fix: guess git repo from url with custom ssh configs (#573)

* fix: guess git repo from url with custom ssh configs

or github enterprise

* style: removed unnecessary if statement
This commit is contained in:
Carlos Alexandro Becker 2018-02-20 18:21:20 -03:00 committed by GitHub
parent b6299472fc
commit f2bd64f146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 23 deletions

View File

@ -21,21 +21,21 @@ func remoteRepo() (result config.Repo, err error) {
}
func extractRepoFromURL(s string) config.Repo {
for _, r := range []string{
"git@github.com:",
".git",
"https://github.com/",
"\n",
} {
s = strings.Replace(s, r, "", -1)
}
return toRepo(s)
}
func toRepo(s string) config.Repo {
var ss = strings.Split(s, "/")
// removes the .git suffix and any new lines
s = strings.NewReplacer(
".git", "",
"\n", "",
).Replace(s)
// if the URL contains a :, indicating a SSH config,
// remove all chars until it, including itself
// on HTTP and HTTPS URLs it will remove the http(s): prefix,
// which is ok. On SSH URLs the whole user@server will be removed,
// which is required.
s = s[strings.LastIndex(s, ":")+1:]
// split by /, the last to parts should be the owner and name
ss := strings.Split(s, "/")
return config.Repo{
Owner: ss[0],
Name: ss[1],
Owner: ss[len(ss)-2],
Name: ss[len(ss)-1],
}
}

View File

@ -17,12 +17,18 @@ func TestRepoName(t *testing.T) {
assert.Equal(t, "goreleaser/goreleaser", repo.String())
}
func TestExtractReporFromGitURL(t *testing.T) {
repo := extractRepoFromURL("git@github.com:goreleaser/goreleaser.git")
assert.Equal(t, "goreleaser/goreleaser", repo.String())
}
func TestExtractReporFromHttpsURL(t *testing.T) {
repo := extractRepoFromURL("https://github.com/goreleaser/goreleaser.git")
assert.Equal(t, "goreleaser/goreleaser", repo.String())
func TestExtractRepoFromURL(t *testing.T) {
for _, url := range []string{
"git@github.com:goreleaser/goreleaser.git",
"git@custom:goreleaser/goreleaser.git",
"git@custom:crazy/url/goreleaser/goreleaser.git",
"https://github.com/goreleaser/goreleaser.git",
"https://github.enterprise.com/goreleaser/goreleaser.git",
"https://github.enterprise.com/crazy/url/goreleaser/goreleaser.git",
} {
t.Run(url, func(t *testing.T) {
repo := extractRepoFromURL(url)
assert.Equal(t, "goreleaser/goreleaser", repo.String())
})
}
}