Skip to content

Commit

Permalink
VersionControlSystem: Add support for splitting Bitbucket URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
sschuberth committed Dec 6, 2017
1 parent 58abaf7 commit 44183fc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 23 deletions.
75 changes: 52 additions & 23 deletions downloader/src/main/kotlin/VersionControlSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,67 @@ abstract class VersionControlSystem {
return VcsInfo("", vcsUrl, "", "")
}

if (uri.host.endsWith("github.com")) {
var url = uri.scheme + "://" + uri.authority
return when {
uri.host.endsWith("github.com") -> {
var url = uri.scheme + "://" + uri.authority

// Append the first two path components that denote the user and project to the base URL.
val pathIterator = Paths.get(uri.path).iterator()
if (pathIterator.hasNext()) {
url += "/${pathIterator.next()}"
}
if (pathIterator.hasNext()) {
url += "/${pathIterator.next()}"
// Append the first two path components that denote the user and project to the base URL.
val pathIterator = Paths.get(uri.path).iterator()
if (pathIterator.hasNext()) {
url += "/${pathIterator.next()}"
}
if (pathIterator.hasNext()) {
url += "/${pathIterator.next()}"

// GitHub only hosts Git repositories.
if (!url.endsWith(".git")) {
url += ".git"
// GitHub only hosts Git repositories.
if (!url.endsWith(".git")) {
url += ".git"
}
}
}

var revision = ""
var path = ""
var revision = ""
var path = ""

if (pathIterator.hasNext() && pathIterator.next().toString() in listOf("blob", "tree")) {
if (pathIterator.hasNext()) {
revision = pathIterator.next().toString()
path = uri.path.substringAfter(revision).trimStart('/').removeSuffix(".git")
if (pathIterator.hasNext() && pathIterator.next().toString() in listOf("blob", "tree")) {
if (pathIterator.hasNext()) {
revision = pathIterator.next().toString()
path = uri.path.substringAfter(revision).trimStart('/').removeSuffix(".git")
}
}

VcsInfo("Git", url, revision, path)
}
uri.host.endsWith("bitbucket.org") -> {
var url = uri.scheme + "://" + uri.authority

return VcsInfo("Git", url, revision, path)
}
// Append the first two path components that denote the user and project to the base URL.
val pathIterator = Paths.get(uri.path).iterator()
if (pathIterator.hasNext()) {
url += "/${pathIterator.next()}"
}
if (pathIterator.hasNext()) {
url += "/${pathIterator.next()}"
}

var revision = ""
var path = ""

if (pathIterator.hasNext() && pathIterator.next().toString() == "src") {
if (pathIterator.hasNext()) {
revision = pathIterator.next().toString()
path = uri.path.substringAfter(revision).trimStart('/').removeSuffix(".git")
}
}

// Fall back to returning just the original URL.
return VcsInfo("", vcsUrl, "", "")
val provider = forUrl(url)?.toString() ?: ""
if (provider == "Git") {
url += ".git"
}

VcsInfo(provider, url, revision, path)
}
else -> VcsInfo("", vcsUrl, "", "")
}
}
}

Expand Down
39 changes: 39 additions & 0 deletions downloader/src/test/kotlin/VersionControlSystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,44 @@ class VersionControlSystemTest : WordSpec({
)
actual shouldBe expected
}

"not modify Bitbucket URLs without a path" {
val actual = VersionControlSystem.splitUrl(
"https://bitbucket.org/paniq/masagin"
)
val expected = VcsInfo(
"Mercurial",
"https://bitbucket.org/paniq/masagin",
"",
""
)
actual shouldBe expected
}

"properly split Bitbucket tree URLs" {
val actual = VersionControlSystem.splitUrl(
"https://bitbucket.org/yevster/spdxtraxample/src/287aebc/src/java/com/yevster/example/?at=master"
)
val expected = VcsInfo(
"Git",
"https://bitbucket.org/yevster/spdxtraxample.git",
"287aebc",
"src/java/com/yevster/example/"
)
actual shouldBe expected
}

"properly split Bitbucket blob URLs" {
val actual = VersionControlSystem.splitUrl(
"https://bitbucket.org/yevster/spdxtraxample/src/287aebc/README.md?at=master"
)
val expected = VcsInfo(
"Git",
"https://bitbucket.org/yevster/spdxtraxample.git",
"287aebc",
"README.md"
)
actual shouldBe expected
}
}
})

0 comments on commit 44183fc

Please sign in to comment.