-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repospec: support ssh urls with ssh certificates
Organizations that use a custom SSH Certificate Authority with GitHub Enterprise Cloud will have a host that starts with `org-12345@` instead of `git@`. This removes the hard-coded `git@` in the repo spec parsing for a regexp match on a valid username instead. E.g. now it will be able to parse specs like `[email protected]:someOrg/someRepo/README.md?ref=someBranch`. Docs [here](https://docs.github.com/en/enterprise-cloud@latest/organizations/managing-git-access-to-your-organizations-repositories/about-ssh-certificate-authorities#about-ssh-urls-with-ssh-certificates)
- Loading branch information
1 parent
e62480d
commit 1e77a88
Showing
2 changed files
with
25 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"fmt" | ||
"net/url" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
@@ -212,18 +213,26 @@ func peelQuery(arg string) (string, string, time.Duration, bool) { | |
return parsed.Path, ref, duration, submodules | ||
} | ||
|
||
var userRegex = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9-]*@`) | ||
|
||
func parseHostSpec(n string) (string, string) { | ||
var host string | ||
// Start accumulating the host part. | ||
for _, p := range []string{ | ||
// Order matters here. | ||
"git::", "gh:", "ssh://", "https://", "http://", "file://", | ||
"git@", "github.com:", "github.com/"} { | ||
if len(p) < len(n) && strings.ToLower(n[:len(p)]) == p { | ||
n = n[len(p):] | ||
host += p | ||
consumeHostStrings := func(parts []string) { | ||
for _, p := range parts { | ||
if len(p) < len(n) && strings.ToLower(n[:len(p)]) == p { | ||
n = n[len(p):] | ||
host += p | ||
} | ||
} | ||
} | ||
// Start accumulating the host part. | ||
// Order matters here. | ||
consumeHostStrings([]string{"git::", "gh:", "ssh://", "https://", "http://", "file://"}) | ||
if p := userRegex.FindString(n); p != "" { | ||
n = n[len(p):] | ||
host += p | ||
} | ||
consumeHostStrings([]string{"github.com:", "github.com/"}) | ||
if host == "git@" { | ||
i := strings.Index(n, "/") | ||
if i > -1 { | ||
|
@@ -257,10 +266,14 @@ func parseHostSpec(n string) (string, string) { | |
|
||
func normalizeGitHostSpec(host string) string { | ||
s := strings.ToLower(host) | ||
m := userRegex.FindString(host) | ||
if strings.Contains(s, "github.com") { | ||
if strings.Contains(s, "git@") || strings.Contains(s, "ssh:") { | ||
switch { | ||
case m != "": | ||
host = m + "github.com:" | ||
case strings.Contains(s, "ssh:"): | ||
host = "[email protected]:" | ||
} else { | ||
default: | ||
host = "https://github.com/" | ||
} | ||
} | ||
|
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 |
---|---|---|
|
@@ -35,6 +35,8 @@ func TestNewRepoSpecFromUrl_Permute(t *testing.T) { | |
{"git::https://git.example.com/", "https://git.example.com/"}, | ||
{"[email protected]:", "[email protected]:"}, | ||
{"[email protected]/", "[email protected]:"}, | ||
{"[email protected]:", "[email protected]:"}, | ||
{"[email protected]/", "[email protected]:"}, | ||
} | ||
var orgRepos = []string{"someOrg/someRepo", "kubernetes/website"} | ||
var pathNames = []string{"README.md", "foo/krusty.txt", ""} | ||
|