From d5c0d945f88c2507abbb5b7aca94e597d8afc937 Mon Sep 17 00:00:00 2001 From: hrzlgnm Date: Thu, 6 Nov 2025 19:55:47 +0100 Subject: [PATCH] Open pull requests in browser with extra leading slashes removed This allows for having extra slashes in git urls, for example to avoid warnings with older bitbake fetcher implementations in yocto. Which warns about a missing / in git urls --- pkg/commands/hosting_service/definitions.go | 4 +- .../hosting_service/hosting_service_test.go | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) 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",