Skip to content

Commit 9c0cbd8

Browse files
authored
Open pull requests in browser with extra leading slashes removed (#5018)
### PR Description This allows for having extra slashes in git URLs. Especially in submodules. Those may be used to avoid warnings with older bitbake fetcher implementations in yocto. Which warns about relative urls when not having a slash in git urls. - git@bitbucket.org:project/repo.git -> generates a warning in bitbake, which is annoying. - git@bitbucket.org:/project/repo.git -> does not generate a warning in bitbake. But when trying to open a PR from lazygit with an url like git@bitbucket.org:/project/repo.git leads to https://bitbucket.org//project/repo being opened and one has to stare at a blank page unless one removes the extra / manualy. This PR updates the regex used for matching git url fragments in a way, that the extra `/` is ignored.
2 parents 1d17507 + f729e2c commit 9c0cbd8

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

pkg/commands/hosting_service/definitions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package hosting_service
22

33
// if you want to make a custom regex for a given service feel free to test it out
4-
// at regoio.herokuapp.com
4+
// at https://regex101.com using the flavor Golang
55
var defaultUrlRegexStrings = []string{
66
`^(?:https?|ssh)://[^/]+/(?P<owner>.*)/(?P<repo>.*?)(?:\.git)?$`,
7-
`^.*?@.*:(?P<owner>.*)/(?P<repo>.*?)(?:\.git)?$`,
7+
`^.*?@.*:/*(?P<owner>.*)/(?P<repo>.*?)(?:\.git)?$`,
88
}
99
var defaultRepoURLTemplate = "https://{{.webDomain}}/{{.owner}}/{{.repo}}"
1010

@@ -26,7 +26,7 @@ var bitbucketServiceDef = ServiceDefinition{
2626
commitURL: "/commits/{{.CommitHash}}",
2727
regexStrings: []string{
2828
`^(?:https?|ssh)://.*/(?P<owner>.*)/(?P<repo>.*?)(?:\.git)?$`,
29-
`^.*@.*:(?P<owner>.*)/(?P<repo>.*?)(?:\.git)?$`,
29+
`^.*@.*:/*(?P<owner>.*)/(?P<repo>.*?)(?:\.git)?$`,
3030
},
3131
repoURLTemplate: defaultRepoURLTemplate,
3232
}

pkg/commands/hosting_service/hosting_service_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ func TestGetPullRequestURL(t *testing.T) {
2929
assert.Equal(t, "https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature%2Fprofile-page&t=1", url)
3030
},
3131
},
32+
{
33+
testName: "Opens a link to new pull request on bitbucket with extra slash removed",
34+
from: "feature/profile-page",
35+
remoteUrl: "git@bitbucket.org:/johndoe/social_network.git",
36+
test: func(url string, err error) {
37+
assert.NoError(t, err)
38+
assert.Equal(t, "https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature%2Fprofile-page&t=1", url)
39+
},
40+
},
3241
{
3342
testName: "Opens a link to new pull request on bitbucket with http remote url",
3443
from: "feature/events",
@@ -47,6 +56,15 @@ func TestGetPullRequestURL(t *testing.T) {
4756
assert.Equal(t, "https://github.com/peter/calculator/compare/feature%2Fsum-operation?expand=1", url)
4857
},
4958
},
59+
{
60+
testName: "Opens a link to new pull request on github with extra slash removed",
61+
from: "feature/sum-operation",
62+
remoteUrl: "git@github.com:/peter/calculator.git",
63+
test: func(url string, err error) {
64+
assert.NoError(t, err)
65+
assert.Equal(t, "https://github.com/peter/calculator/compare/feature%2Fsum-operation?expand=1", url)
66+
},
67+
},
5068
{
5169
testName: "Opens a link to new pull request on github with https remote url",
5270
from: "feature/sum-operation",
@@ -115,6 +133,15 @@ func TestGetPullRequestURL(t *testing.T) {
115133
assert.Equal(t, "https://gitlab.com/peter/calculator/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fui", url)
116134
},
117135
},
136+
{
137+
testName: "Opens a link to new pull request on gitlab with extra slash removed",
138+
from: "feature/ui",
139+
remoteUrl: "git@gitlab.com:/peter/calculator.git",
140+
test: func(url string, err error) {
141+
assert.NoError(t, err)
142+
assert.Equal(t, "https://gitlab.com/peter/calculator/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fui", url)
143+
},
144+
},
118145
{
119146
testName: "Opens a link to new pull request on gitlab in nested groups",
120147
from: "feature/ui",
@@ -124,6 +151,15 @@ func TestGetPullRequestURL(t *testing.T) {
124151
assert.Equal(t, "https://gitlab.com/peter/public/calculator/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fui", url)
125152
},
126153
},
154+
{
155+
testName: "Opens a link to new pull request on gitlab in nested groups and extra slash removed",
156+
from: "feature/ui",
157+
remoteUrl: "git@gitlab.com:/peter/public/calculator.git",
158+
test: func(url string, err error) {
159+
assert.NoError(t, err)
160+
assert.Equal(t, "https://gitlab.com/peter/public/calculator/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fui", url)
161+
},
162+
},
127163
{
128164
testName: "Opens a link to new pull request on gitlab with https remote url in nested groups",
129165
from: "feature/ui",
@@ -181,6 +217,15 @@ func TestGetPullRequestURL(t *testing.T) {
181217
assert.Equal(t, "https://dev.azure.com/myorg/myproject/_git/myrepo/pullrequestcreate?sourceRef=feature%2Fnew", url)
182218
},
183219
},
220+
{
221+
testName: "Opens a link to new pull request on Azure DevOps (SSH) with extra slash removed",
222+
from: "feature/new",
223+
remoteUrl: "git@ssh.dev.azure.com:/v3/myorg/myproject/myrepo",
224+
test: func(url string, err error) {
225+
assert.NoError(t, err)
226+
assert.Equal(t, "https://dev.azure.com/myorg/myproject/_git/myrepo/pullrequestcreate?sourceRef=feature%2Fnew", url)
227+
},
228+
},
184229
{
185230
testName: "Opens a link to new pull request on Azure DevOps (SSH) with specific target",
186231
from: "feature/new",
@@ -248,6 +293,19 @@ func TestGetPullRequestURL(t *testing.T) {
248293
assert.Equal(t, "https://mycompany.bitbucket.com/projects/myproject/repos/myrepo/pull-requests?create&sourceBranch=feature%2Fnew", url)
249294
},
250295
},
296+
{
297+
testName: "Opens a link to new pull request on Bitbucket Server (SSH) with extra slash removed",
298+
from: "feature/new",
299+
remoteUrl: "ssh://git@mycompany.bitbucket.com:/myproject/myrepo.git",
300+
configServiceDomains: map[string]string{
301+
// valid configuration for a bitbucket server URL
302+
"mycompany.bitbucket.com": "bitbucketServer:mycompany.bitbucket.com",
303+
},
304+
test: func(url string, err error) {
305+
assert.NoError(t, err)
306+
assert.Equal(t, "https://mycompany.bitbucket.com/projects/myproject/repos/myrepo/pull-requests?create&sourceBranch=feature%2Fnew", url)
307+
},
308+
},
251309
{
252310
testName: "Opens a link to new pull request on Bitbucket Server (SSH) with specific target",
253311
from: "feature/new",
@@ -365,6 +423,20 @@ func TestGetPullRequestURL(t *testing.T) {
365423
},
366424
expectedLoggedErrors: nil,
367425
},
426+
{
427+
testName: "Does not log error when config service domains are valid with extra slash",
428+
from: "feature/profile-page",
429+
remoteUrl: "git@bitbucket.org:/johndoe/social_network.git",
430+
configServiceDomains: map[string]string{
431+
// valid configuration for a custom service URL
432+
"git.work.com": "gitlab:code.work.com",
433+
},
434+
test: func(url string, err error) {
435+
assert.NoError(t, err)
436+
assert.Equal(t, "https://bitbucket.org/johndoe/social_network/pull-requests/new?source=feature%2Fprofile-page&t=1", url)
437+
},
438+
expectedLoggedErrors: nil,
439+
},
368440
{
369441
testName: "Does not log error when config service webDomain contains a port",
370442
from: "feature/profile-page",

0 commit comments

Comments
 (0)