Skip to content

Commit

Permalink
fix: support the parsing of a broader range of git remote origins (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
ga-paul-t authored Dec 5, 2022
1 parent f33dd28 commit ca3a9fa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ FROM alpine:3.17.0

# Install tini to ensure docker waits for uplift to finish before terminating
RUN apk add --no-cache \
git=2.36.2-r0 \
tini=0.19.0-r0 \
gnupg=2.2.35-r4
git=2.38.1-r0 \
tini=0.19.0-r1 \
gnupg=2.2.40-r0

COPY uplift /usr/local/bin

Expand Down
22 changes: 12 additions & 10 deletions internal/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,6 @@ func Remote() (Repository, error) {
// Sanitise any SSH based URL to ensure it is parseable
rem = strings.TrimPrefix(rem, "git@")
rem = strings.Replace(rem, ":", "/", 1)
} else if strings.HasPrefix(rem, "http") {
// Strip any credentials from the URL to ensure it is parseable
if tkn := strings.Index(rem, "@"); tkn > -1 {
rem = rem[tkn+1:]
} else {
rem = rem[strings.Index(rem, "//")+2:]
}
}

u, err := url.Parse(rem)
Expand All @@ -175,15 +168,24 @@ func Remote() (Repository, error) {

// Split into parts
p := strings.Split(u.Path, "/")

if len(p) < 3 {
return Repository{}, fmt.Errorf("malformed repository URL: %s", remURL)
// This could be a custom Git server that doesn't follow the expected pattern.
// Don't fail, but return the raw origin for custom parsing
return Repository{Origin: origin}, nil
}

// If the repository has a HTTP(S) origin, the host will have been correctly identified
host := u.Host
if host == "" {
// For other schemes, assume the host is contained within the first part
host = p[0]
}
host := p[0]
owner := p[1]
name := p[len(p)-1]
path := strings.Join(p[1:], "/")

if strings.Contains(p[0], "codecommit") {
if strings.Contains(host, "codecommit") {
// No concept of an owner with CodeCommit repositories
owner = ""
path = name
Expand Down
20 changes: 12 additions & 8 deletions internal/git/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ func TestRemote(t *testing.T) {
repo: "testing10",
path: "testing10",
},
{
name: "CustomGitServerSchemeWithPort",
cloneURL: "http://172.24.100.14:7990/owner/nested/testing11.git",
host: "172.24.100.14:7990",
owner: "owner",
repo: "testing11",
path: "owner/nested/testing11",
},
{
name: "LocalRunningGitServer",
cloneURL: "http://localhost:8000/testing12.git",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -260,14 +272,6 @@ func TestRemote_NoRemoteSet(t *testing.T) {
require.Error(t, err)
}

func TestRemote_MalformedURL(t *testing.T) {
InitRepo(t)
RemoteOrigin(t, "whizzbang.com/repository")

_, err := Remote()
require.EqualError(t, err, "malformed repository URL: whizzbang.com/repository")
}

func TestAllTags(t *testing.T) {
InitRepo(t)

Expand Down

0 comments on commit ca3a9fa

Please # to comment.