diff --git a/pkg/commands/hosting_service/definitions.go b/pkg/commands/hosting_service/definitions.go index bd82d99a5..a5ade4e8b 100644 --- a/pkg/commands/hosting_service/definitions.go +++ b/pkg/commands/hosting_service/definitions.go @@ -4,7 +4,7 @@ package hosting_service // at regoio.herokuapp.com var defaultUrlRegexStrings = []string{ `^(?:https?|ssh)://[^/]+/(?P.*)/(?P.*?)(?:\.git)?$`, - `^.*?@.*:(?P.*)/(?P.*?)(?:\.git)?$`, + `^.*?@.*:/*(?P.*)/(?P.*?)(?:\.git)?$`, } var defaultRepoURLTemplate = "https://{{.webDomain}}/{{.owner}}/{{.repo}}" @@ -26,7 +26,7 @@ var bitbucketServiceDef = ServiceDefinition{ commitURL: "/commits/{{.CommitHash}}", regexStrings: []string{ `^(?:https?|ssh)://.*/(?P.*)/(?P.*?)(?:\.git)?$`, - `^.*@.*:(?P.*)/(?P.*?)(?:\.git)?$`, + `^.*@.*:/*(?P.*)/(?P.*?)(?:\.git)?$`, }, repoURLTemplate: defaultRepoURLTemplate, } diff --git a/pkg/commands/hosting_service/hosting_service_test.go b/pkg/commands/hosting_service/hosting_service_test.go index bcf5e6fb4..1856f21e0 100644 --- a/pkg/commands/hosting_service/hosting_service_test.go +++ b/pkg/commands/hosting_service/hosting_service_test.go @@ -29,6 +29,15 @@ func TestGetPullRequestURL(t *testing.T) { assert.Equal(t, "https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature%2Fprofile-page&t=1", url) }, }, + { + testName: "Opens a link to new pull request on bitbucket with extra slash removed", + from: "feature/profile-page", + remoteUrl: "git@bitbucket.org:/johndoe/social_network.git", + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature%2Fprofile-page&t=1", url) + }, + }, { testName: "Opens a link to new pull request on bitbucket with http remote url", from: "feature/events", @@ -47,6 +56,15 @@ func TestGetPullRequestURL(t *testing.T) { assert.Equal(t, "https://github.com/peter/calculator/compare/feature%2Fsum-operation?expand=1", url) }, }, + { + testName: "Opens a link to new pull request on github with extra slash removed", + from: "feature/sum-operation", + remoteUrl: "git@github.com:/peter/calculator.git", + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://github.com/peter/calculator/compare/feature%2Fsum-operation?expand=1", url) + }, + }, { testName: "Opens a link to new pull request on github with https remote url", from: "feature/sum-operation", @@ -115,6 +133,15 @@ func TestGetPullRequestURL(t *testing.T) { assert.Equal(t, "https://gitlab.com/peter/calculator/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fui", url) }, }, + { + testName: "Opens a link to new pull request on gitlab with extra slash removed", + from: "feature/ui", + remoteUrl: "git@gitlab.com:/peter/calculator.git", + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://gitlab.com/peter/calculator/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fui", url) + }, + }, { testName: "Opens a link to new pull request on gitlab in nested groups", from: "feature/ui", @@ -124,6 +151,15 @@ func TestGetPullRequestURL(t *testing.T) { assert.Equal(t, "https://gitlab.com/peter/public/calculator/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fui", url) }, }, + { + testName: "Opens a link to new pull request on gitlab in nested groups and extra slash removed", + from: "feature/ui", + remoteUrl: "git@gitlab.com:/peter/public/calculator.git", + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://gitlab.com/peter/public/calculator/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fui", url) + }, + }, { testName: "Opens a link to new pull request on gitlab with https remote url in nested groups", from: "feature/ui", @@ -181,6 +217,15 @@ func TestGetPullRequestURL(t *testing.T) { assert.Equal(t, "https://dev.azure.com/myorg/myproject/_git/myrepo/pullrequestcreate?sourceRef=feature%2Fnew", url) }, }, + { + testName: "Opens a link to new pull request on Azure DevOps (SSH) with extra slash removed", + from: "feature/new", + remoteUrl: "git@ssh.dev.azure.com:/v3/myorg/myproject/myrepo", + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://dev.azure.com/myorg/myproject/_git/myrepo/pullrequestcreate?sourceRef=feature%2Fnew", url) + }, + }, { testName: "Opens a link to new pull request on Azure DevOps (SSH) with specific target", from: "feature/new", @@ -248,6 +293,19 @@ func TestGetPullRequestURL(t *testing.T) { assert.Equal(t, "https://mycompany.bitbucket.com/projects/myproject/repos/myrepo/pull-requests?create&sourceBranch=feature%2Fnew", url) }, }, + { + testName: "Opens a link to new pull request on Bitbucket Server (SSH) with extra slash removed", + from: "feature/new", + remoteUrl: "ssh://git@mycompany.bitbucket.com:/myproject/myrepo.git", + configServiceDomains: map[string]string{ + // valid configuration for a bitbucket server URL + "mycompany.bitbucket.com": "bitbucketServer:mycompany.bitbucket.com", + }, + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://mycompany.bitbucket.com/projects/myproject/repos/myrepo/pull-requests?create&sourceBranch=feature%2Fnew", url) + }, + }, { testName: "Opens a link to new pull request on Bitbucket Server (SSH) with specific target", from: "feature/new", @@ -365,6 +423,20 @@ func TestGetPullRequestURL(t *testing.T) { }, expectedLoggedErrors: nil, }, + { + testName: "Does not log error when config service domains are valid with extra slash", + from: "feature/profile-page", + remoteUrl: "git@bitbucket.org:/johndoe/social_network.git", + configServiceDomains: map[string]string{ + // valid configuration for a custom service URL + "git.work.com": "gitlab:code.work.com", + }, + test: func(url string, err error) { + assert.NoError(t, err) + assert.Equal(t, "https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature%2Fprofile-page&t=1", url) + }, + expectedLoggedErrors: nil, + }, { testName: "Does not log error when config service webDomain contains a port", from: "feature/profile-page",