Skip to content

Commit b823b1e

Browse files
committed
feat: add branch filter to controllers related to keys/translations & handle branch in services
1 parent 7881986 commit b823b1e

File tree

22 files changed

+211
-42
lines changed

22 files changed

+211
-42
lines changed

backend/api/src/main/kotlin/io/tolgee/api/v2/controllers/translation/CreateOrUpdateTranslationsFacade.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CreateOrUpdateTranslationsFacade(
3232
@RequestBody @Valid
3333
dto: SetTranslationsWithKeyDto,
3434
): SetTranslationsResponseModel {
35-
val key = keyService.find(projectHolder.projectEntity.id, dto.key, dto.namespace) ?: return create(dto)
35+
val key = keyService.find(projectHolder.projectEntity.id, dto.key, dto.namespace, dto.branch) ?: return create(dto)
3636
return setTranslations(dto, key)
3737
}
3838

@@ -74,7 +74,7 @@ class CreateOrUpdateTranslationsFacade(
7474
dto: SetTranslationsWithKeyDto,
7575
key: Key? = null,
7676
): SetTranslationsResponseModel {
77-
val keyNotNull = key ?: keyService.get(projectHolder.project.id, dto.key, dto.namespace)
77+
val keyNotNull = key ?: keyService.get(projectHolder.project.id, dto.key, dto.namespace, dto.branch)
7878
securityService.checkLanguageTranslatePermissionsByTag(
7979
dto.translations.keys,
8080
projectHolder.project.id,

backend/api/src/main/kotlin/io/tolgee/component/KeyComplexEditHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class KeyComplexEditHelper(
148148
}
149149

150150
if (isKeyNameModified || isNamespaceChanged) {
151-
edited = keyService.edit(key, dto.name, dto.namespace)
151+
edited = keyService.edit(key, dto.name, dto.namespace, dto.branch)
152152
}
153153

154154
return keyWithDataModelAssembler.toModel(edited)

backend/api/src/main/kotlin/io/tolgee/hateoas/contentDelivery/ContentDeliveryConfigModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ class ContentDeliveryConfigModel(
3737
override var messageFormat: ExportMessageFormat? = null
3838
override var supportArrays: Boolean = false
3939
override var fileStructureTemplate: String? = null
40+
override var filterBranch: String? = null
4041
}

backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/translations/v2TranslationsController/TranslationsControllerModificationTest.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.tolgee.dtos.request.translation.SetTranslationsWithKeyDto
77
import io.tolgee.fixtures.andAssertThatJson
88
import io.tolgee.fixtures.andIsBadRequest
99
import io.tolgee.fixtures.andIsForbidden
10+
import io.tolgee.fixtures.andIsNotFound
1011
import io.tolgee.fixtures.andIsOk
1112
import io.tolgee.fixtures.andPrettyPrint
1213
import io.tolgee.fixtures.isValidId
@@ -390,6 +391,56 @@ class TranslationsControllerModificationTest : ProjectAuthControllerTest("/v2/pr
390391
testOutdated(translation, true)
391392
}
392393

394+
@ProjectJWTAuthTestMethod
395+
@Test
396+
fun `sets translations for existing key in branch`() {
397+
saveTestData()
398+
performProjectAuthPut(
399+
"/translations",
400+
SetTranslationsWithKeyDto(
401+
"branch key",
402+
null,
403+
mutableMapOf("en" to "English branch key"),
404+
branch = "test-branch",
405+
),
406+
).andIsOk
407+
.andAssertThatJson {
408+
node("translations.en.text").isEqualTo("English branch key")
409+
node("translations.en.id").isValidId
410+
node("keyId").isValidId
411+
node("keyName").isEqualTo("branch key")
412+
}
413+
}
414+
415+
@ProjectJWTAuthTestMethod
416+
@Test
417+
fun `cannot set translations for key in branch without branch provided`() {
418+
saveTestData()
419+
performProjectAuthPut(
420+
"/translations",
421+
SetTranslationsWithKeyDto(
422+
"branch key",
423+
null,
424+
mutableMapOf("en" to "Cannot do that"),
425+
),
426+
).andIsNotFound
427+
}
428+
429+
@ProjectJWTAuthTestMethod
430+
@Test
431+
fun `cannot set translations for key in default branch with different branch provided`() {
432+
saveTestData()
433+
performProjectAuthPut(
434+
"/translations",
435+
SetTranslationsWithKeyDto(
436+
"A key",
437+
null,
438+
mutableMapOf("en" to "Cannot do that"),
439+
branch = "test-branch",
440+
),
441+
).andIsNotFound
442+
}
443+
393444
private fun testOutdated(
394445
translation: Translation,
395446
state: Boolean,

backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/v2ImportController/SingleStepImportControllerTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class SingleStepImportControllerTest : ProjectAuthControllerTest("/v2/projects/"
111111
listOf(Pair(jsonFileName, simpleJson)),
112112
params = mapOf("createNewKeys" to false),
113113
)
114+
114115
executeInNewTransaction {
115116
keyService.find(testData.project.id, "test", null).assert.isNull()
116117
}

backend/app/src/test/kotlin/io/tolgee/service/LanguageServiceTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,6 @@ class LanguageServiceTest : AbstractSpringTest() {
175175
)
176176
.setParameter("type", ActivityType.HARD_DELETE_LANGUAGE)
177177
.resultList
178-
result.assert.hasSize(3)
178+
result.assert.hasSize(6)
179179
}
180180
}

backend/data/src/main/kotlin/io/tolgee/development/testDataBuilder/TestDataService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ class TestDataService(
284284
savePermissions(builder)
285285
saveMtServiceConfigs(builder)
286286
saveAllNamespaces(builder)
287+
saveBranches(builder)
287288
saveKeyData(builder)
288289
saveTranslationData(builder)
289290
saveImportData(builder)
@@ -304,7 +305,6 @@ class TestDataService(
304305
saveAiPlaygroundResults(builder)
305306
saveLabels(builder)
306307
saveSuggestions(builder)
307-
saveBranches(builder)
308308
}
309309

310310
private fun saveImportSettings(builder: ProjectBuilder) {

backend/data/src/main/kotlin/io/tolgee/development/testDataBuilder/data/TranslationsTestData.kt

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,34 @@ class TranslationsTestData {
5959
originalName = "Deutsch"
6060
}.self
6161

62-
addKey {
63-
name = "A key"
64-
aKey = this
62+
aKey = addBasicKey()
63+
aKeyGermanTranslation = aKey.translations.first()
64+
addBranch {
65+
name = "test-branch"
66+
project = this@project.self
6567
}.build {
66-
setDescription("A key description")
67-
addTranslation {
68-
language = germanLanguage
69-
text = "Z translation"
70-
state = TranslationState.REVIEWED
71-
auto = true
72-
outdated = true
73-
mtProvider = MtServiceType.GOOGLE
74-
aKeyGermanTranslation = this
68+
addKey {
69+
name = "branch key"
70+
branch = this@build.self
7571
}.build {
76-
addComment {
77-
author = user
78-
text = "Comment"
79-
state = TranslationCommentState.RESOLVED
72+
addTranslation {
73+
language = germanLanguage
74+
text = "Branched german key."
75+
}
76+
addTranslation {
77+
language = englishLanguage
78+
text = "Branched english key."
8079
}
8180
}
82-
addTag("Cool tag")
81+
}
82+
// create same key as in different branch
83+
addBranch {
84+
name = "from-default"
85+
project = this@project.self
86+
}.build {
87+
addBasicKey().apply {
88+
branch = self
89+
}
8390
}
8491

8592
val zKeyBuilder =
@@ -101,6 +108,30 @@ class TranslationsTestData {
101108
}.self
102109
}
103110

111+
private fun ProjectBuilder.addBasicKey(): Key {
112+
return addKey {
113+
name = "A key"
114+
}.build key@{
115+
setDescription("A key description")
116+
addTranslation {
117+
language = germanLanguage
118+
text = "Z translation"
119+
state = TranslationState.REVIEWED
120+
auto = true
121+
outdated = true
122+
mtProvider = MtServiceType.GOOGLE
123+
self.translations.add(this)
124+
}.build {
125+
addComment {
126+
author = user
127+
text = "Comment"
128+
state = TranslationCommentState.RESOLVED
129+
}
130+
}
131+
addTag("Cool tag")
132+
}.self
133+
}
134+
104135
fun addKeysViewOnlyUser() {
105136
root.apply {
106137
addUserAccountWithoutOrganization {

backend/data/src/main/kotlin/io/tolgee/dtos/ExportParamsDocs.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,8 @@ but in Android format. (e.g., en-rUS)
7070
const val HTML_ESCAPE_DESCRIPTION = """If true, HTML tags are escaped in the exported file. (Supported in the XLIFF format only).
7171
7272
e.g. Key <b>hello</b> will be exported as &lt;b&gt;hello&lt;/b&gt;"""
73+
74+
const val FILTER_BRANCH_DESCRIPTION = """Filter translations with branch.
75+
76+
By default, default branch is exported."""
7377
}

backend/data/src/main/kotlin/io/tolgee/dtos/IExportParams.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import io.tolgee.dtos.ExportParamsDocs.LANGUAGES_EXAMPLE
1919
import io.tolgee.dtos.ExportParamsDocs.MESSAGE_FORMAT_DESCRIPTION
2020
import io.tolgee.dtos.ExportParamsDocs.STRUCTURE_DELIMITER_DESCRIPTION
2121
import io.tolgee.dtos.ExportParamsDocs.SUPPORT_ARRAYS_DESCRIPTION
22+
import io.tolgee.dtos.ExportParamsDocs.FILTER_BRANCH_DESCRIPTION
2223
import io.tolgee.formats.ExportFormat
2324
import io.tolgee.formats.ExportMessageFormat
2425
import io.tolgee.model.enums.TranslationState
@@ -100,6 +101,11 @@ interface IExportParams {
100101
)
101102
var escapeHtml: Boolean?
102103

104+
@get:Schema(
105+
description = FILTER_BRANCH_DESCRIPTION,
106+
)
107+
var filterBranch: String?
108+
103109
fun copyPropsFrom(other: IExportParams) {
104110
this.languages = other.languages
105111
this.format = other.format

0 commit comments

Comments
 (0)