Skip to content

Commit

Permalink
Return links to a new UI which is now a default.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andronnix committed Nov 29, 2024
1 parent d4fc53e commit fbbd3da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import retrofit2.converter.gson.GsonConverterFactory
import java.io.*
import java.net.HttpURLConnection
import java.net.URI
import java.net.URLEncoder
import java.time.Duration
import java.time.Instant
import java.time.ZoneOffset
Expand Down Expand Up @@ -224,6 +223,8 @@ internal class TeamCityCoroutinesInstanceImpl(
.cookieJar(nodeSelector.toCookieJar())
.build()

internal val webLinks = WebLinks(serverUrl)

internal val service = Retrofit.Builder()
.client(client)
.baseUrl("$serverUrl$serverUrlBase")
Expand Down Expand Up @@ -1043,14 +1044,10 @@ private class ProjectImpl(
if (isFullBean) runBlocking { "Project(id=$idString,name=${getName()})" } else "Project(id=$idString)"

override fun getHomeUrl(branch: String?): String =
getUserUrlPage(instance.serverUrl, "project.html", projectId = id, branch = branch)
instance.webLinks.projectPage(id, branch = branch)

override fun getTestHomeUrl(testId: TestId): String = getUserUrlPage(
instance.serverUrl, "project.html",
projectId = id,
testNameId = testId,
tab = "testDetails"
)
override fun getTestHomeUrl(testId: TestId): String =
instance.webLinks.testHistoryPage(testId, id)

override fun getMutes(): Flow<Mute> = lazyPagingFlow(
instance = instance,
Expand Down Expand Up @@ -1179,9 +1176,8 @@ private class BuildConfigurationImpl(
override fun toString(): String =
if (isFullBean) runBlocking { "BuildConfiguration(id=$idString,name=${getName()})" } else "BuildConfiguration(id=$idString)"

override fun getHomeUrl(branch: String?): String = getUserUrlPage(
instance.serverUrl, "viewType.html", buildTypeId = id, branch = branch
)
override fun getHomeUrl(branch: String?): String =
instance.webLinks.buildConfigurationPage(id, branch = branch)

override val id = BuildConfigurationId(idString)
private val name = SuspendingLazy { notnull { it.name } }
Expand Down Expand Up @@ -1332,12 +1328,7 @@ private class ChangeImpl(
override fun getHomeUrl(
specificBuildConfigurationId: BuildConfigurationId?,
includePersonalBuilds: Boolean?
): String = getUserUrlPage(
instance.serverUrl, "viewModification.html",
modId = id,
buildTypeId = specificBuildConfigurationId,
personal = includePersonalBuilds
)
): String = instance.webLinks.changePage(id, specificBuildConfigurationId, includePersonalBuilds)

override suspend fun firstBuilds(): List<Build> =
instance.service
Expand Down Expand Up @@ -1436,10 +1427,7 @@ private class UserImpl(

override suspend fun getName(): String? = name.getValue()

override fun getHomeUrl(): String = getUserUrlPage(
instance.serverUrl, "admin/editUser.html",
userId = id
)
override fun getHomeUrl(): String = instance.webLinks.userPage(id)

override fun toString(): String =
if (isFullBean) runBlocking { "User(id=${id.stringId}, username=${getUsername()})" } else "User(id=${id.stringId})"
Expand Down Expand Up @@ -1807,10 +1795,7 @@ private class BuildImpl(
?.map(::PropertyImpl) ?: emptyList()
}

override fun getHomeUrl(): String = getUserUrlPage(
instance.serverUrl, "viewLog.html",
buildId = id
)
override fun getHomeUrl(): String = instance.webLinks.buildPage(id)

override suspend fun getStatusText(): String? = statusText.getValue()

Expand Down Expand Up @@ -2559,37 +2544,6 @@ private fun convertToJavaRegexp(pattern: String): Regex {
return pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".").toRegex()
}

private fun String.urlencode(): String = URLEncoder.encode(this, "UTF-8")

private fun getUserUrlPage(
serverUrl: String,
pageName: String,
tab: String? = null,
projectId: ProjectId? = null,
buildId: BuildId? = null,
testNameId: TestId? = null,
userId: UserId? = null,
modId: ChangeId? = null,
personal: Boolean? = null,
buildTypeId: BuildConfigurationId? = null,
branch: String? = null
): String {
val params = mutableListOf<String>()

tab?.let { params.add("tab=" + tab.urlencode()) }
projectId?.let { params.add("projectId=" + projectId.stringId.urlencode()) }
buildId?.let { params.add("buildId=" + buildId.stringId.urlencode()) }
testNameId?.let { params.add("testNameId=" + testNameId.stringId.urlencode()) }
userId?.let { params.add("userId=" + userId.stringId.urlencode()) }
modId?.let { params.add("modId=" + modId.stringId.urlencode()) }
personal?.let { params.add("personal=" + if (personal) "true" else "false") }
buildTypeId?.let { params.add("buildTypeId=" + buildTypeId.stringId.urlencode()) }
branch?.let { params.add("branch=" + branch.urlencode()) }

return "$serverUrl/$pageName" +
if (params.isNotEmpty()) "?${params.joinToString("&")}" else ""
}

private fun saveToFile(body: ResponseBody, file: File) {
file.parentFile?.mkdirs()
body.byteStream().use { input ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jetbrains.teamcity.rest.coroutines

import org.jetbrains.teamcity.rest.*
import java.net.URLEncoder

class WebLinks(private val serverUrl: String) {
fun buildConfigurationPage(id: BuildConfigurationId, branch: String? = null) =
"$serverUrl/buildConfiguration/$id" + if (branch != null) "?${branch.urlencode()}" else ""

fun buildPage(id: BuildId, configurationId: BuildConfigurationId? = null) =
if (configurationId != null) {
"$serverUrl/buildConfiguration/$configurationId/$id"
} else {
"$serverUrl/build/$id"
}

fun changePage(id: ChangeId, configurationId: BuildConfigurationId? = null, personal: Boolean? = null): String {
val params = mutableListOf<String>()
if (configurationId != null)
params.add("buildTypeId=$configurationId")
if (personal != null)
params.add("personal=$personal")

return "$serverUrl/change/$id" +
if (params.isNotEmpty()) params.joinToString("&", prefix = "?") else ""
}

fun projectPage(id: ProjectId, branch: String? = null) =
"$serverUrl/project/$id" + if (branch != null) "?${branch.urlencode()}" else ""

fun testHistoryPage(id: TestId, projectId: ProjectId) =
"$serverUrl/test/$id?currentProjectId=$projectId"

fun userPage(id: UserId) =
"$serverUrl/admin/editUser.html?userId=$id"
}

private fun String.urlencode(): String = URLEncoder.encode(this, "UTF-8")

0 comments on commit fbbd3da

Please sign in to comment.