From 24dd2fc5fc5eb4d09c0b5cc77f6809dafa41eec0 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Tue, 9 Jan 2018 07:45:07 +0100 Subject: [PATCH 1/3] Added support for Bitbucket Server with different root context Please see the unit tests for more details. Basically, take the context parts before 'scm' and add them to the new URL before the other parts. Instead of taking fixed indexes for the path elements, base everything off the index of the found 'scm' path element. --- git-open | 17 +++++++++++++---- test/git-open.bats | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/git-open b/git-open index d952c3a..935fba2 100755 --- a/git-open +++ b/git-open @@ -126,10 +126,19 @@ fi if [[ "$domain" == 'bitbucket.org' ]]; then # Bitbucket, see https://github.com/paulirish/git-open/issues/80 for why ?at is needed. providerBranchRef="/src?at=$branch" -elif [[ ${pathargs[0]} == 'scm' ]]; then - # Bitbucket server, which starts with 'scm' - # Replace the first element, 'scm', with 'projects'. Keep the first argument, the string 'repos', and finally the rest of the arguments. - pathargs=('projects' ${pathargs[1]} 'repos' "${pathargs[@]:2}") +elif [[ "${#pathargs[@]}" -ge 3 && ${pathargs[${#pathargs[@]} - 3]} == 'scm' ]]; then + # Bitbucket server always has /scm/ as the third to last segment in the url path, e.g. /scm/ppp/test-repo.git + # Anything before the 'scm' is part of the server's root context + + # Check whether there are other context parts before the 'scm' part + pathPref=() + if [[ "${#pathargs[@]}" -gt 3 ]]; then + # If there are other context parts, add them, up to (but not including) the found 'scm' + pathPref=("${pathargs[*]:0:${#pathargs[@]} - 3}") + fi + + # Replace the 'scm' element, with 'projects'. Keep the first argument, the string 'repos', and finally the rest of the arguments. + pathargs=(${pathPref[@]} 'projects' ${pathargs[${#pathargs[@]} - 2]} 'repos' "${pathargs[@]:${#pathargs[@]} - 1}") IFS='/' urlpath="${pathargs[*]}" providerBranchRef="/browse?at=$branch" elif [[ "${#pathargs[@]}" -ge '2' && ${pathargs[${#pathargs[@]} - 2]} == '_git' ]]; then diff --git a/test/git-open.bats b/test/git-open.bats index 3f90fb4..0f750d2 100755 --- a/test/git-open.bats +++ b/test/git-open.bats @@ -269,6 +269,35 @@ setup() { } + +@test "bitbucket server with different root context" { + # https://github.com/paulirish/git-open/pull/15 + git remote set-url origin "https://user@bitbucket.example.com/git/scm/ppp/test-repo.git" + run ../git-open + assert_output "https://bitbucket.example.com/git/projects/ppp/repos/test-repo" +} + + +@test "bitbucket server with different root context with multiple parts" { + # https://github.com/paulirish/git-open/pull/15 + git remote set-url origin "https://user@bitbucket.example.com/really/long/root/context/scm/ppp/test-repo.git" + run ../git-open + assert_output "https://bitbucket.example.com/really/long/root/context/projects/ppp/repos/test-repo" +} + + +@test "bitbucket: Bitbucket Server private user repos with different root context" { + # https://github.com/paulirish/git-open/pull/83#issuecomment-309968538 + git remote set-url origin "https://mybb.domain.com/root/context/scm/~first.last/rrr.git" + git checkout -B "develop" + run ../git-open + assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=develop" || + assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=refs%2Fheads%2Fdevelop" || + assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=refs/heads/develop" + +} + + ## ## GitLab ## From 119ada7f69a41776e0aa687b7f889604767d4bda Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Wed, 10 Jan 2018 07:53:44 +0100 Subject: [PATCH 2/3] Removed an unnecessary if statement --- git-open | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/git-open b/git-open index 935fba2..630bb67 100755 --- a/git-open +++ b/git-open @@ -130,12 +130,8 @@ elif [[ "${#pathargs[@]}" -ge 3 && ${pathargs[${#pathargs[@]} - 3]} == 'scm' ]]; # Bitbucket server always has /scm/ as the third to last segment in the url path, e.g. /scm/ppp/test-repo.git # Anything before the 'scm' is part of the server's root context - # Check whether there are other context parts before the 'scm' part - pathPref=() - if [[ "${#pathargs[@]}" -gt 3 ]]; then - # If there are other context parts, add them, up to (but not including) the found 'scm' - pathPref=("${pathargs[*]:0:${#pathargs[@]} - 3}") - fi + # If there are other context parts, add them, up to (but not including) the found 'scm' + pathPref=("${pathargs[*]:0:${#pathargs[@]} - 3}") # Replace the 'scm' element, with 'projects'. Keep the first argument, the string 'repos', and finally the rest of the arguments. pathargs=(${pathPref[@]} 'projects' ${pathargs[${#pathargs[@]} - 2]} 'repos' "${pathargs[@]:${#pathargs[@]} - 1}") From 140edbbceefdeff41ab8ae90bb2b95e68788b3bb Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Thu, 11 Jan 2018 08:18:42 +0100 Subject: [PATCH 3/3] Fixed test names to match existing tests Also removed some duplicated tests that were introduced during a previous merge. --- test/git-open.bats | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/test/git-open.bats b/test/git-open.bats index 0f750d2..b726f5d 100755 --- a/test/git-open.bats +++ b/test/git-open.bats @@ -214,22 +214,6 @@ setup() { refute_output --partial "@" } -@test "bitbucket server" { - # https://github.com/paulirish/git-open/pull/15 - git remote set-url origin "https://user@bitbucket.example.com/scm/ppp/test-repo.git" - run ../git-open - assert_output "https://bitbucket.example.com/projects/ppp/repos/test-repo" -} - -@test "bitbucket server branch" { - # https://github.com/paulirish/git-open/pull/15 - git remote set-url origin "https://user@bitbucket.example.com/scm/ppp/test-repo.git" - git checkout -B "bb-server" - run ../git-open - assert_output "https://bitbucket.example.com/projects/ppp/repos/test-repo/browse?at=bb-server" -} - - @test "bitbucket: Bitbucket Server" { # https://github.com/paulirish/git-open/issues/77#issuecomment-309044010 git remote set-url origin "https://user@mybb.domain.com/scm/ppp/rrr.git" @@ -270,19 +254,23 @@ setup() { } -@test "bitbucket server with different root context" { +@test "bitbucket: Bitbucket Server with different root context" { # https://github.com/paulirish/git-open/pull/15 git remote set-url origin "https://user@bitbucket.example.com/git/scm/ppp/test-repo.git" run ../git-open - assert_output "https://bitbucket.example.com/git/projects/ppp/repos/test-repo" + assert_output "https://bitbucket.example.com/git/projects/ppp/repos/test-repo" || + assert_output "https://bitbucket.example.com/git/projects/ppp/repos/test-repo/?at=master" || + assert_output "https://bitbucket.example.com/git/projects/ppp/repos/test-repo/?at=refs%2Fheads%2Fmaster" } -@test "bitbucket server with different root context with multiple parts" { +@test "bitbucket: Bitbucket Server with different root context with multiple parts" { # https://github.com/paulirish/git-open/pull/15 git remote set-url origin "https://user@bitbucket.example.com/really/long/root/context/scm/ppp/test-repo.git" run ../git-open - assert_output "https://bitbucket.example.com/really/long/root/context/projects/ppp/repos/test-repo" + assert_output "https://bitbucket.example.com/really/long/root/context/projects/ppp/repos/test-repo" || + assert_output "https://bitbucket.example.com/really/long/root/context/projects/ppp/repos/test-repo/?at=master" || + assert_output "https://bitbucket.example.com/really/long/root/context/projects/ppp/repos/test-repo/?at=refs%2Fheads%2Fmaster" } @@ -294,7 +282,6 @@ setup() { assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=develop" || assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=refs%2Fheads%2Fdevelop" || assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=refs/heads/develop" - }