Skip to content

Commit

Permalink
add full support for project versions
Browse files Browse the repository at this point in the history
  • Loading branch information
HighKo committed Jun 6, 2024
1 parent 58c086f commit 039653b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ data class ProjectVersion(
@field:NotNull val id: Int,
@field:NotNull val name: String,
@field:NotNull val avatarUrl: String,
// @field:NotNull val url: String, // http api produces weird values, so we ignore it for now
@field:NotNull val url: String,
)

Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class HttpInsightObjectOperator(private val context: HttpInsightClientContext) :
}
VERSION -> {
val version = apiAttribute.objectAttributeValues.mapNotNull { av: ObjectAttributeValueApiResponse ->
av.version?.run { ProjectVersion(id.toInt(), name = name, avatarUrl = avatarUrl) }
av.version?.run { ProjectVersion(id.toInt(), name = name, avatarUrl = avatarUrl, url = url) }
}
InsightAttribute.Version(attributeId, version, schema)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.atlassian.jira.config.properties.ApplicationProperties
import com.atlassian.jira.user.util.UserManager
import com.linkedplanet.kotlinatlassianclientcore.common.api.StatusAttribute
import com.linkedplanet.kotlinatlassianclientcore.common.api.StatusCategory
import com.linkedplanet.kotlinatlassianclientcore.common.api.ProjectVersion
import com.linkedplanet.kotlinatlassianclientcore.common.api.ConfluencePage
import com.linkedplanet.kotlinatlassianclientcore.common.api.JiraGroup
import com.linkedplanet.kotlinatlassianclientcore.common.api.JiraProject
Expand All @@ -54,6 +53,7 @@ import com.linkedplanet.kotlininsightclient.api.model.ReferencedObject
import com.linkedplanet.kotlininsightclient.api.model.ReferencedObjectType
import com.linkedplanet.kotlininsightclient.sdk.SdkInsightObjectTypeOperator.typeAttributeBeanToSchema
import com.linkedplanet.kotlininsightclient.sdk.services.ReverseEngineeredDateTimeFormatterInJira
import com.linkedplanet.kotlininsightclient.sdk.services.ReverseEngineeredVersionAssembler
import com.linkedplanet.kotlininsightclient.sdk.util.catchAsInsightClientError
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ConfigureFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade
Expand Down Expand Up @@ -90,6 +90,7 @@ object SdkInsightObjectOperator : InsightObjectOperator {
private val baseUrl by lazy { getOSGiComponentInstanceOfType(ApplicationProperties::class.java).getString("jira.baseurl")!! }
private val projectService by lazy { getOSGiComponentInstanceOfType(ProjectService::class.java) }

private val versionAssembler = ReverseEngineeredVersionAssembler()
private val dateTimeFormatter = ReverseEngineeredDateTimeFormatterInJira()
private val avatarService = ComponentAccessor.getAvatarService()
private val jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext()
Expand Down Expand Up @@ -254,7 +255,7 @@ object SdkInsightObjectOperator : InsightObjectOperator {
objectAttributeBeanFactory.createStatusAttributeValue(ota) { statusId != null && it.id == statusId }
}
is InsightAttribute.Version -> {
val versionIds = attr.versions.map { it.id.toLong() } // TODO:hg convert to long?
val versionIds = attr.versions.map { it.id.toLong() }
objectAttributeBeanFactory.createVersionAttributeValue(ota) { versionIds.contains(it.id()) }
}
is InsightAttribute.Confluence -> {
Expand Down Expand Up @@ -411,18 +412,16 @@ object SdkInsightObjectOperator : InsightObjectOperator {
}
Type.GROUP -> {
val groups = objectAttributeBean.objectAttributeValueBeans.mapNotNull { attribute ->
JiraGroup(attribute.textValue, avatarUrlFor("group-logo.jpg"))
JiraGroup(
attribute.textValue,
"$baseUrl/download/resources/com.riadalabs.jira.plugins.insight/images/${"group-logo.jpg"}"
)
}
InsightAttribute.Group(attributeId, groups, schema)
}
Type.VERSION -> { // TODO: add full support
Type.VERSION -> {
val versions = objectAttributeBean.objectAttributeValueBeans.mapNotNull { attribute: ObjectAttributeValueBean ->
ProjectVersion(
id = attribute.integerValue,
// we would need access to VersionAssemblerInJira or VersionService access to resolve name and url
name = "Unknown", // see hidden class VersionAssemblerInJira.class in plugins:insight:10.4.2
avatarUrl = avatarUrlFor("version-logo.png")
)
versionAssembler.assembleVersion(attribute.integerValue.toLong())
}
InsightAttribute.Version(attributeId, versions, schema)
}
Expand Down Expand Up @@ -462,9 +461,6 @@ object SdkInsightObjectOperator : InsightObjectOperator {
}
}

private fun avatarUrlFor(fileName: String) =
"$baseUrl/download/resources/com.riadalabs.jira.plugins.insight/images/$fileName"

private suspend fun handleDefaultValue(
id: InsightAttributeId,
schema: ObjectTypeSchemaAttribute,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*-
* #%L
* kotlin-insight-client-sdk
* %%
* Copyright (C) 2022 - 2023 linked-planet GmbH
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.linkedplanet.kotlininsightclient.sdk.services

import com.atlassian.jira.bc.project.ProjectService
import com.atlassian.jira.bc.project.version.VersionService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor.getOSGiComponentInstanceOfType
import com.atlassian.jira.config.properties.ApplicationProperties
import com.linkedplanet.kotlinatlassianclientcore.common.api.ProjectVersion
import com.atlassian.jira.project.version.Version

/**
* Kotlin Version of com.riadalabs.jira.plugins.insight.channel.web.api.rest.services.version.VersionAssemblerInJira
*/
class ReverseEngineeredVersionAssembler {

private val projectService by lazy { getOSGiComponentInstanceOfType(ProjectService::class.java) }
private val versionService by lazy { getOSGiComponentInstanceOfType(VersionService::class.java) }
private val baseUrl = getOSGiComponentInstanceOfType(ApplicationProperties::class.java).getString("jira.baseurl")!!
private val jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext()
private fun user() = jiraAuthenticationContext.loggedInUser

fun assembleVersion(id: Long): ProjectVersion {
val version: Version = versionService.getVersionById(user(), id).version
?: return createEmptyVersion(id)
val project = projectService.getProjectById(user(), version.projectId).project
return ProjectVersion(
id = id.toInt(),
name = version.name,
avatarUrl = "$baseUrl/download/resources/com.riadalabs.jira.plugins.insight/images/version-logo.png",
url = "$baseUrl/browse/${project?.key ?: -1}/fixforversion/${id}/?selectedTab=com.riadalabs.jira.plugins.insight:rlabs-version-summary-panel"
)
}

private fun createEmptyVersion(id: Long) = ProjectVersion(
id = id.toInt(),
name = "Unknown",
avatarUrl = "$baseUrl/download/resources/com.riadalabs.jira.plugins.insight/images/version-logo.png",
url = "javascript:void(0);" // returns undefined when called
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -798,36 +798,37 @@ interface InsightObjectOperatorTest {

@Test
fun testVersionCrud() = runBlocking {
suspend fun getVersionAttributes(objectId: InsightObjectId): Pair<List<ProjectVersion>?, List<ProjectVersion>> {
suspend fun getVersionAttributes(objectId: InsightObjectId): Pair<List<ProjectVersion>, List<ProjectVersion>> {
val insightObject = insightObjectOperator.getObjectById(objectId, ::identity).orFail()!!
val attrVersion = insightObject.getAttributeAs<InsightAttribute.Version>(TestVersionVersion.attributeId)?.versions
val attrVersion = insightObject.getAttributeAs<InsightAttribute.Version>(TestVersionVersion.attributeId)?.versions!!
val attrVersionList = insightObject.getVersionList(TestVersionVersions.attributeId)
return Pair(attrVersion, attrVersionList)
}

val objectName = "createdByUnitTest"
autoClean(clean = { deleteObjectByName(InsightObjectType.Version.id, objectName).orFail() }) {
val version1 = ProjectVersion(-1, "Unknown", "") // no name resolution for now
val version2 = ProjectVersion(-2, "Unknown", "") // no name resolution for now
autoClean(clean = { deleteObjectByName(InsightObjectType.Version.id, objectName).orFail() }) {
// Versions need to be created inside the project settings before being available for selection
val avatarUrl="http://localhost:2990/download/resources/com.riadalabs.jira.plugins.insight/images/version-logo.png"
fun versionUrl(id: Long) = "http://localhost:2990/browse/TEST/fixforversion/${id}/?selectedTab=com.riadalabs.jira.plugins.insight:rlabs-version-summary-panel"
val version1 = ProjectVersion(10001, "v0.9 beta", avatarUrl, versionUrl(10001))
val version2 = ProjectVersion(10000, "v1.0", avatarUrl, versionUrl(10000))
val objectId = insightObjectOperator.createInsightObject(
InsightObjectType.Version.id,
TestVersionName.attributeId toValue objectName,
TestVersionVersion.attributeId toVersion version1,
TestVersionVersions.attributeId toVersions listOf(version1)
).orFail()
val (attrVersion, attrVersions) = getVersionAttributes(objectId)
assertThat(attrVersion?.firstOrNull()?.id, equalTo(version1.id))
assertThat(attrVersions.firstOrNull()?.name, equalTo(version1.name))
assertThat(attrVersions.firstOrNull()?.avatarUrl, endsWith("version-logo.png"))
// assertThat(attrVersions.firstOrNull()?.url, startsWith("http")) // this always failed
assertThat(attrVersion.firstOrNull(), equalTo(version1))
assertThat(attrVersions.firstOrNull(), equalTo(version1))

insightObjectOperator.updateInsightObject(objectId,
TestVersionVersion.attributeId toVersion version2,
TestVersionVersions.attributeId toVersions listOf(version2, version1),
toDomain = ::identity )
val (updatedAttrVersion, updatedAttrVersions) = getVersionAttributes(objectId)
assertThat(updatedAttrVersion?.firstOrNull()?.id, equalTo(version2.id))
assertThat(updatedAttrVersions.map { it.id }.toSet(), equalTo(setOf(version1.id, version2.id)))
assertThat(updatedAttrVersion.firstOrNull(), equalTo(version2))
assertThat(updatedAttrVersions.toSet(), equalTo(setOf(version1, version2)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions test-resources/jira-generated-test-resources.zip
Git LFS file not shown

0 comments on commit 039653b

Please sign in to comment.