From 8e14f8ccaf4d208cc176739aab63a61600fb9fed Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 26 Mar 2023 19:00:38 +0530 Subject: feat(build): migrate to Crowdin V2 API --- .../passwordstore/gradle/crowdin/BuildOnApiTask.kt | 39 ++++++++++++++++++---- .../passwordstore/gradle/crowdin/CrowdinPlugin.kt | 5 --- .../gradle/crowdin/api/ListProjects.kt | 9 +++++ 3 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/api/ListProjects.kt (limited to 'build-logic/src/main') diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/BuildOnApiTask.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/BuildOnApiTask.kt index 070ec2e9..8e7edaa8 100644 --- a/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/BuildOnApiTask.kt +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/BuildOnApiTask.kt @@ -1,8 +1,13 @@ package app.passwordstore.gradle.crowdin +import app.passwordstore.gradle.crowdin.api.ListProjects +import com.squareup.moshi.Moshi +import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory import java.util.concurrent.TimeUnit +import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody import org.gradle.api.DefaultTask import org.gradle.api.provider.Property import org.gradle.api.tasks.Input @@ -26,15 +31,37 @@ abstract class BuildOnApiTask : DefaultTask() { .readTimeout(5, TimeUnit.MINUTES) .callTimeout(10, TimeUnit.MINUTES) .build() - val url = - CROWDIN_BUILD_API_URL.format(crowdinIdentifier.get(), crowdinLogin.get(), crowdinKey.get()) - val request = Request.Builder().url(url).get().build() - client.newCall(request).execute().close() + val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build() + val projectAdapter = moshi.adapter(ListProjects::class.java) + val projectRequest = + Request.Builder() + .url("$CROWDIN_BASE_URL/projects") + .header("Authorization", "Bearer ${crowdinKey.get()}") + .get() + .build() + client.newCall(projectRequest).execute().use { response -> + val projects = projectAdapter.fromJson(response.body!!.source()) + if (projects != null) { + val identifier = + projects.projects + .first { data -> data.project.identifier == crowdinIdentifier.get() } + .project + .id + .toString() + val buildRequest = + Request.Builder() + .url(CROWDIN_BUILD_API_URL.format(identifier)) + .header("Authorization", "Bearer ${crowdinKey.get()}") + .post("{}".toRequestBody("application/json".toMediaType())) + .build() + client.newCall(buildRequest).execute().close() + } + } } private companion object { - private const val CROWDIN_BUILD_API_URL = - "https://api.crowdin.com/api/project/%s/export?login=%s&account-key=%s" + private const val CROWDIN_BASE_URL = "https://api.crowdin.com/api/v2" + private const val CROWDIN_BUILD_API_URL = "$CROWDIN_BASE_URL/projects/%s/translations/builds" } } diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/CrowdinPlugin.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/CrowdinPlugin.kt index 5442e085..83705a74 100644 --- a/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/CrowdinPlugin.kt +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/CrowdinPlugin.kt @@ -13,11 +13,6 @@ import org.gradle.api.tasks.Delete import org.gradle.kotlin.dsl.create import org.gradle.kotlin.dsl.register -private const val EXCEPTION_MESSAGE = - """Applying `crowdin-plugin` requires a projectName to be configured via the "crowdin" extension.""" -private const val CROWDIN_BUILD_API_URL = - "https://api.crowdin.com/api/project/%s/export?login=%s&account-key=%s" - @Suppress("Unused") class CrowdinDownloadPlugin : Plugin { diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/api/ListProjects.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/api/ListProjects.kt new file mode 100644 index 00000000..e94205b6 --- /dev/null +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/api/ListProjects.kt @@ -0,0 +1,9 @@ +package app.passwordstore.gradle.crowdin.api + +import com.squareup.moshi.Json + +data class ListProjects(@Json(name = "data") val projects: List) + +data class ProjectData(@Json(name = "data") val project: Project) + +data class Project(val id: Long, val identifier: String) -- cgit v1.2.3