Skip to content

Commit

Permalink
repospec: support ssh urls with ssh certificates
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mightyguava committed Aug 1, 2022
1 parent e57b5d2 commit 03d7aec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
29 changes: 20 additions & 9 deletions api/internal/git/repospec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/url"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -195,18 +196,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://",
"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://"})
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 {
Expand Down Expand Up @@ -241,7 +250,9 @@ func parseHostSpec(n string) (string, string) {
func normalizeGitHostSpec(host string) string {
s := strings.ToLower(host)
if strings.Contains(s, "github.com") {
if strings.Contains(s, "git@") || strings.Contains(s, "ssh:") {
if m := userRegex.FindString(host); m != "" {
host = m + "github.com:"
} else if strings.Contains(s, "ssh:") {
host = "git@github.com:"
} else {
host = "https://github.com/"
Expand Down
5 changes: 5 additions & 0 deletions api/internal/git/repospec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var hostNamesRawAndNormalized = [][]string{
{"git::https://git.example.com/", "https://git.example.com/"},
{"git@github.com:", "git@github.com:"},
{"git@github.com/", "git@github.com:"},
{"org-12345@github.com:", "org-12345@github.com:"},
{"org-12345@github.com/", "org-12345@github.com:"},
}

func makeURL(hostFmt, orgRepo, path, href string) string {
Expand All @@ -60,6 +62,9 @@ func TestNewRepoSpecFromUrl(t *testing.T) {
rs, err := NewRepoSpecFromURL(uri)
if err != nil {
t.Errorf("problem %v", err)
if rs == nil {
rs = &RepoSpec{}
}
}
if rs.Host != hostSpec {
bad = append(bad, []string{"host", uri, rs.Host, hostSpec})
Expand Down

0 comments on commit 03d7aec

Please sign in to comment.