Skip to content

Commit 8130516

Browse files
committed
feat(cli): ensure category is preserved during update
1 parent 92934fd commit 8130516

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

cli/weblate-cli/src/main/kotlin/net/thunderbird/cli/weblate/WeblateCli.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.github.ajalt.clikt.parameters.options.required
99
import java.io.File
1010
import net.thunderbird.cli.weblate.api.Component
1111
import net.thunderbird.cli.weblate.api.ComponentConfig
12+
import net.thunderbird.cli.weblate.api.ComponentPatch
1213
import net.thunderbird.cli.weblate.api.WeblateClient
1314

1415
@Suppress("TooGenericExceptionCaught")
@@ -68,7 +69,14 @@ class WeblateCli : CliktCommand(
6869
diffs.forEach { println(" $it") }
6970
if (!dryRun) {
7071
try {
71-
val result = client.patchComponent(token, component.info.url, goldenConfig)
72+
val result = client.patchComponent(
73+
token,
74+
component.info.url,
75+
ComponentPatch(
76+
category = component.info.category,
77+
config = goldenConfig,
78+
),
79+
)
7280
if (result) {
7381
println(" ✅ Updated component config successfully")
7482
} else {

cli/weblate-cli/src/main/kotlin/net/thunderbird/cli/weblate/api/ComponentInfo.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import kotlinx.serialization.Serializable
99
* @property name The name of the component.
1010
* @property url The URL of the component in Weblate.
1111
* @property slug The slug identifier of the component (suitable for matching/include lists)
12+
* @property category The category of the component
1213
*/
1314
@Serializable
1415
data class ComponentInfo(
1516
val id: Int,
1617
val name: String,
1718
val slug: String,
1819
val url: String,
20+
val category: String?,
1921
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.thunderbird.cli.weblate.api
2+
3+
import kotlinx.serialization.KSerializer
4+
import kotlinx.serialization.Serializable
5+
import kotlinx.serialization.descriptors.SerialDescriptor
6+
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
7+
import kotlinx.serialization.descriptors.element
8+
import kotlinx.serialization.encoding.Decoder
9+
import kotlinx.serialization.encoding.Encoder
10+
import kotlinx.serialization.encoding.encodeStructure
11+
12+
/**
13+
* Weblate Component Patch
14+
*
15+
* We need the category to prevent the API from resetting it to undefined when we update the config.
16+
*
17+
* @property category The category of the component
18+
* @property config The configuration of the component to be updated
19+
*/
20+
@Serializable(with = ComponentPatch.ComponentPatchSerializer::class)
21+
data class ComponentPatch(
22+
val category: String?,
23+
val config: ComponentConfig,
24+
) {
25+
companion object ComponentPatchSerializer : KSerializer<ComponentPatch> {
26+
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("ComponentPatch") {
27+
element<String>("category")
28+
element("config", ComponentConfig.serializer().descriptor)
29+
}
30+
31+
override fun deserialize(decoder: Decoder): ComponentPatch {
32+
error("Deserialization is not supported for ComponentPatch")
33+
}
34+
35+
override fun serialize(encoder: Encoder, value: ComponentPatch) {
36+
encoder.encodeStructure(descriptor) {
37+
if (value.category != null) {
38+
encodeStringElement(descriptor, 0, value.category)
39+
encodeSerializableElement(descriptor, 1, ComponentConfig.serializer(), value.config)
40+
} else {
41+
encodeSerializableElement(descriptor, 0, ComponentConfig.serializer(), value.config)
42+
}
43+
}
44+
}
45+
}
46+
}

cli/weblate-cli/src/main/kotlin/net/thunderbird/cli/weblate/api/WeblateClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class WeblateClient(
4141
return components
4242
}
4343

44-
fun patchComponent(token: String, url: String, patch: ComponentConfig): Boolean {
44+
fun patchComponent(token: String, url: String, patch: ComponentPatch): Boolean {
4545
var success = false
4646

4747
runBlocking {

0 commit comments

Comments
 (0)