Skip to content

Commit

Permalink
fix: enabledFeatures on self-hosted instances & Sentry false positives (
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar authored Dec 18, 2024
1 parent 63d64b5 commit 9be6420
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.tolgee.hateoas.ee

import com.fasterxml.jackson.annotation.JsonGetter
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonSetter
import io.tolgee.constants.Feature
import org.springframework.hateoas.RepresentationModel
import org.springframework.hateoas.server.core.Relation
Expand All @@ -10,10 +13,26 @@ open class SelfHostedEePlanModel(
val id: Long = 0,
var name: String = "",
val public: Boolean = true,
val enabledFeatures: Array<Feature> = arrayOf(),
@JsonIgnore
var enabledFeatures: Array<Feature> = arrayOf(),
val prices: PlanPricesModel,
val includedUsage: PlanIncludedUsageModel = PlanIncludedUsageModel(),
val hasYearlyPrice: Boolean = false,
val free: Boolean,
val nonCommercial: Boolean,
) : RepresentationModel<SelfHostedEePlanModel>()
) : RepresentationModel<SelfHostedEePlanModel>() {
/**
* We need to provide this setter so unrecognized features are ignored in situation
* that self-hosted instance is not upgraded to version containing the new features introduced
* in Tolgee cloud.
*/
@JsonSetter("enabledFeatures")
fun setJsonEnabledFeatures(features: Set<String>) {
this.enabledFeatures = features.mapNotNull { Feature.findByName(it) }.toTypedArray()
}

@JsonGetter("enabledFeatures")
fun getJsonEnabledFeatures(): Array<Feature> {
return this.enabledFeatures
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SentryBeforeSendCallback : SentryOptions.BeforeSendCallback {
listOf(
"FailedDontRequeueException",
"ClientAbortException",
"AsyncRequestNotUsableException",
)
}

Expand Down
8 changes: 8 additions & 0 deletions backend/data/src/main/kotlin/io/tolgee/constants/Feature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ enum class Feature {
TASKS,
SSO,
ORDER_TRANSLATION,

;

companion object {
fun findByName(name: String): Feature? {
return entries.find { it.name == name }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,13 @@ class ApiKeyService(
}

private fun logTransactionIsolation() {
val isolationLevel =
entityManager.createNativeQuery("show transaction_isolation")
.singleResult as String
val message = "Transaction isolation level: $isolationLevel"
Sentry.addBreadcrumb(message)
if (logger.isDebugEnabled) {
val isolationLevel =
entityManager.createNativeQuery("show transaction_isolation")
.singleResult as String
logger.debug("Transaction isolation level: $isolationLevel")
logger.debug(message)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.tolgee.ee.unit

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import io.tolgee.constants.Feature
import io.tolgee.hateoas.ee.PlanPricesModel
import io.tolgee.hateoas.ee.SelfHostedEePlanModel
import io.tolgee.testing.assert
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class SelfHostedEePlanModelTest {
@Autowired
private lateinit var objectMapper: ObjectMapper

@Test
fun `parses plan response with unknown features`() {
val parsed =
objectMapper.readValue<SelfHostedEePlanModel>(
"""
{
"enabledFeatures": ["I've made this up", "ASSISTED_UPDATES"],
"prices": $pricesJson,
"free": false,
"nonCommercial": false
}
""".trimIndent(),
)

parsed.enabledFeatures.toList().assert.containsExactly(Feature.ASSISTED_UPDATES)
}

val pricesJson: String
get() {
return objectMapper.writeValueAsString(PlanPricesModel())
}
}

0 comments on commit 9be6420

Please sign in to comment.