-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(url): normalizeURL wasn't working on https with .git suffix (#176)
- Loading branch information
Showing
9 changed files
with
76 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package url | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Normalize a Github/Gitlab URL (SSH or HTTPS) to a HTTPS URL | ||
func NormalizeUrl(url string) string { | ||
if strings.Contains(url, "https://") { | ||
return removeGitExtension(url) | ||
} | ||
if strings.Contains(url, "http://") { | ||
return removeGitExtension("https://" + url[7:]) | ||
} | ||
// All SSH URL from GitHub are like "git@padok.github.com:<owner>/<repo>.git" | ||
// We split on ":" then remove ".git" by removing the last characters | ||
// To handle enterprise GitHub, we dynamically get "padok.github.com" | ||
// By removing "git@" at the beginning of the string | ||
server, repo := splitSSHUrl(url) | ||
return fmt.Sprintf("https://%s/%s", server, repo) | ||
} | ||
|
||
func splitSSHUrl(url string) (server string, repo string) { | ||
split := strings.Split(url, ":") | ||
return split[0][4:], removeGitExtension(split[1]) | ||
} | ||
|
||
func removeGitExtension(url string) string { | ||
if strings.HasSuffix(url, ".git") { | ||
return url[:len(url)-4] | ||
} | ||
return url | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package url_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/padok-team/burrito/internal/utils/url" | ||
) | ||
|
||
func TestNormalizeURL(t *testing.T) { | ||
urlTypes := []string{ | ||
"git@github.com:padok-team/burrito.git", | ||
"git@github.com:padok-team/burrito", | ||
"https://github.com/padok-team/burrito.git", | ||
"https://github.com/padok-team/burrito", | ||
"http://github.com/padok-team/burrito.git", | ||
"http://github.com/padok-team/burrito", | ||
} | ||
expected := "https://github.com/padok-team/burrito" | ||
for _, u := range urlTypes { | ||
normalized := url.NormalizeUrl(u) | ||
if normalized != expected { | ||
t.Errorf("Passed: %s, Expected %s, got %s", u, expected, normalized) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters