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.
This commit is contained in:
Nils Winkler 2018-01-09 07:45:07 +01:00
parent 8caa9ad1da
commit 24dd2fc5fc
2 changed files with 42 additions and 4 deletions

View file

@ -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

View file

@ -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
##