From 85f0bb27d0dcbc28fdda46fa5dccce6e12ac6122 Mon Sep 17 00:00:00 2001 From: AnotherGitProfile Date: Fri, 26 Feb 2021 01:42:58 +0500 Subject: [PATCH] Update project path parsing based on remote url * Fix project path for projects with dot in name (tsoding#185) * Fix project path for repositories with custom port --- main.go | 22 +++++++++++++++++----- main_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 main_test.go diff --git a/main.go b/main.go index aa8cfd5..8fc8825 100644 --- a/main.go +++ b/main.go @@ -259,6 +259,20 @@ func getRemote(params map[string]string) string { return "origin" } +func getRepoFromRemoteURL(host, remoteURL string) *string { + hostRegex := regexp.MustCompile(host + "(?::[\\d]+){0,1}:?/*(.*)") + groups := hostRegex.FindStringSubmatch(remoteURL) + if groups == nil { + return nil + } + path := groups[1] + path = strings.TrimSuffix(path, "/") + if strings.HasSuffix(path, ".git") { + path = strings.TrimSuffix(path, ".git") + } + return &path +} + func getRepo(directory string, remote string) (string, IssueAPI, error) { credentials := getCredentials() if len(credentials) == 0 { @@ -304,12 +318,10 @@ func getRepo(directory string, remote string) (string, IssueAPI, error) { } for _, creds := range credentials { - hostRegex := regexp.MustCompile( - creds.getHost() + "[:/]([-\\w]+)\\/([-\\w]+)(.git)?") - groups := hostRegex.FindStringSubmatch(urlString) + repo := getRepoFromRemoteURL(creds.getHost(), urlString) - if groups != nil { - return groups[1] + "/" + groups[2], creds, nil + if repo != nil { + return *repo, creds, nil } } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..71bf569 --- /dev/null +++ b/main_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "testing" +) + +func TestMain_GetRepoFromRemoteURL(t *testing.T) { + serviceURL := "github.com" + tests := []struct { + in string + out *string + }{ + {"https://bitbucket.org/user/project.git", nil}, + {"https://github.com/user/project", stringPtr("user/project")}, + {"https://github.com/user/project/", stringPtr("user/project")}, + {"https://github.com/user/project.git", stringPtr("user/project")}, + {"https://github.com/user/project.with.dot.git", stringPtr("user/project.with.dot")}, + {"https://github.com/long/path/to/project", stringPtr("long/path/to/project")}, + {"https://github.com/long/path/to/project.git", stringPtr("long/path/to/project")}, + {"https://github.com/long/path/to/project.git/", stringPtr("long/path/to/project")}, + {"ssh://git@github.com:22/user/project.git", stringPtr("user/project")}, + {"ssh://git@github.com:22/long/path/to/project.git", stringPtr("long/path/to/project")}, + {"ssh://git@github.com:user/project.git", stringPtr("user/project")}, + } + + for _, tt := range tests { + t.Run(tt.in, func(t *testing.T) { + if got := getRepoFromRemoteURL(serviceURL, tt.in); !stringPtrEqual(got, tt.out) { + t.Errorf("got %q, want %q", derefString(got), derefString(tt.out)) + } + }) + } +} + +func TestMain_GetRepoFromRemoteURLWithPort(t *testing.T) { + serviceURL := "gitea.com:80" + gitRemote := "https://gitea.com:80/user/path.git" + expected := stringPtr("user/path") + if got := getRepoFromRemoteURL(serviceURL, gitRemote); !stringPtrEqual(got, expected) { + t.Errorf("got %q, want %q", derefString(got), derefString(expected)) + } +}