aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2023-03-26 19:00:38 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2023-03-26 19:00:38 +0530
commit8e14f8ccaf4d208cc176739aab63a61600fb9fed (patch)
tree51bcf1615a0aea7eebd73f53c4622033cb22069a
parentcb0a2fe6ee57613aa1136c602c3d2e959edfcadf (diff)
feat(build): migrate to Crowdin V2 API
-rw-r--r--.github/workflows/sync_crowdin.yml1
-rw-r--r--build-logic/build.gradle.kts2
-rw-r--r--build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/BuildOnApiTask.kt39
-rw-r--r--build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/CrowdinPlugin.kt5
-rw-r--r--build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/api/ListProjects.kt9
-rw-r--r--gradle/libs.versions.toml2
6 files changed, 46 insertions, 12 deletions
diff --git a/.github/workflows/sync_crowdin.yml b/.github/workflows/sync_crowdin.yml
index 610f56e2..600afbe6 100644
--- a/.github/workflows/sync_crowdin.yml
+++ b/.github/workflows/sync_crowdin.yml
@@ -25,7 +25,6 @@ jobs:
env:
CROWDIN_LOGIN: ${{ secrets.CROWDIN_LOGIN }}
CROWDIN_PROJECT_KEY: ${{ secrets.CROWDIN_PROJECT_KEY }}
- CROWDIN_PROJECT_IDENTIFIER: ${{ secrets.CROWDIN_PROJECT_IDENTIFIER }}
- name: Check if PR is required
run: if [[ $(git status -s) != '' ]]; then echo "UPDATED=true" >> $GITHUB_ENV; fi
diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts
index 53f3d57b..6efbd1e6 100644
--- a/build-logic/build.gradle.kts
+++ b/build-logic/build.gradle.kts
@@ -98,6 +98,8 @@ dependencies {
implementation(libs.build.ktfmt)
implementation(libs.build.mavenpublish)
implementation(libs.build.metalava)
+ implementation(libs.build.moshi)
+ implementation(libs.build.moshi.kotlin)
implementation(libs.build.okhttp)
implementation(libs.build.r8)
implementation(libs.build.semver)
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<Project> {
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<ProjectData>)
+
+data class ProjectData(@Json(name = "data") val project: Project)
+
+data class Project(val id: Long, val identifier: String)
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index 766e531b..f5ed37b3 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -43,6 +43,8 @@ build-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.r
build-ktfmt = "com.facebook:ktfmt:0.43"
build-mavenpublish = "com.vanniktech:gradle-maven-publish-plugin:0.25.1"
build-metalava = "me.tylerbwong.gradle.metalava:plugin:0.3.3"
+build-moshi = "com.squareup.moshi:moshi:1.14.0"
+build-moshi-kotlin = "com.squareup.moshi:moshi-kotlin:1.14.0"
build-okhttp = "com.squareup.okhttp3:okhttp:4.10.0"
build-r8 = "com.android.tools:r8:8.0.34"
build-semver = "com.vdurmont:semver4j:3.1.0"