From 6670ec42b3086b7e9175e4e8d52744a9c6371ed7 Mon Sep 17 00:00:00 2001 From: Anton Nazarov Date: Wed, 30 Nov 2022 12:06:57 +0300 Subject: [PATCH] fix strange error in duration from gitlab --- README.md | 8 ++++---- example/build.gradle.kts | 4 ++++ .../ru/litres/publish/samsung/DebugSetting.kt | 8 ++++++++ .../publish/samsung/SamsungPublishExtension.kt | 6 ++++++ .../publish/samsung/SamsungPublishPlugin.kt | 1 + .../publish/samsung/SamsungPublishTask.kt | 5 ++++- .../samsung/repository/UpdateAppRepository.kt | 11 ++++++++++- .../samsung/usecase/PublishBuildUseCase.kt | 13 ++++++++++--- .../publish/samsung/utils/JwtGenerator.kt | 18 ++++++++++-------- 9 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 plugin-build/plugin/src/main/java/ru/litres/publish/samsung/DebugSetting.kt diff --git a/README.md b/README.md index a166ac7..1ad3ad6 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ plugins { dependencies { classpath("ru.litres.plugin:plugin:{latest_version}") } - + //app build.gradle.kts apply(plugin = "ru.litres.plugin.publish.samsung") ``` @@ -59,7 +59,7 @@ plugins { dependencies { classpath "ru.litres.plugin:plugin:{latest_version}" } - + //app build.gradle.kts apply plugin: "ru.litres.plugin.publish.samsung" ``` @@ -113,11 +113,11 @@ samsungPublishConfig { ) //Service Account ID from service account - artifactDir.set(file("./build/output")) + serviceAccountId.set("....") //Directory where plugin should find release apk. //Plugin searches by extension .apk and gets first file - serviceAccountId.set("....") + artifactDir.set(file("./build/output")) //Object with app setting publishSetting { diff --git a/example/build.gradle.kts b/example/build.gradle.kts index 187ae3f..55abae9 100644 --- a/example/build.gradle.kts +++ b/example/build.gradle.kts @@ -15,4 +15,8 @@ samsungPublishConfig { publishSetting { contentId = "..." } + + debug { + dryMode = false + } } diff --git a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/DebugSetting.kt b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/DebugSetting.kt new file mode 100644 index 0000000..cc34e12 --- /dev/null +++ b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/DebugSetting.kt @@ -0,0 +1,8 @@ +package ru.litres.publish.samsung + +open class DebugSetting { + /** + * Mode for testing all before upload + */ + var dryMode: Boolean = false +} diff --git a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishExtension.kt b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishExtension.kt index 160d73f..ff55ea3 100644 --- a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishExtension.kt +++ b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishExtension.kt @@ -39,4 +39,10 @@ abstract class SamsungPublishExtension @Inject constructor( fun publishSetting(action: Action) { action.execute(publishSetting) } + + val debugSetting: DebugSetting = objects.newInstance(DebugSetting::class.java) + + fun debug(action: Action) { + action.execute(debugSetting) + } } diff --git a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishPlugin.kt b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishPlugin.kt index dc02117..95c36d9 100644 --- a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishPlugin.kt +++ b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishPlugin.kt @@ -17,6 +17,7 @@ class SamsungPublishPlugin : Plugin { it.privateKey.set(extension.privateKey) it.serviceAccountId.set(extension.serviceAccountId) it.publishSetting = extension.publishSetting + it.debugSetting = extension.debugSetting } } } diff --git a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishTask.kt b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishTask.kt index efdda00..71cae82 100644 --- a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishTask.kt +++ b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/SamsungPublishTask.kt @@ -23,6 +23,9 @@ abstract class SamsungPublishTask : DefaultTask() { @get:Input abstract var publishSetting: PublishSetting + @get:Input + abstract var debugSetting: DebugSetting + @Suppress("ThrowsCount") @TaskAction fun publish() { @@ -30,6 +33,6 @@ abstract class SamsungPublishTask : DefaultTask() { val serviceId = serviceAccountId.orNull ?: throw NotFoundRequiredField("serviceAccountId") val folderWithApk = artifactDir.orNull ?: throw NotFoundRequiredField("artifactDir") if (publishSetting.contentId == null) throw NotFoundRequiredField("contentId") - PublishBuildUseCase().invoke(serviceId, key, folderWithApk, publishSetting) + PublishBuildUseCase(debugSetting).invoke(serviceId, key, folderWithApk, publishSetting) } } diff --git a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/repository/UpdateAppRepository.kt b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/repository/UpdateAppRepository.kt index ac2507a..6f86efa 100644 --- a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/repository/UpdateAppRepository.kt +++ b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/repository/UpdateAppRepository.kt @@ -6,6 +6,7 @@ import com.github.kittinunf.fuel.serialization.kotlinxDeserializerOf import kotlinx.serialization.json.Json import kotlinx.serialization.json.encodeToJsonElement import kotlinx.serialization.json.jsonObject +import ru.litres.publish.samsung.DebugSetting import ru.litres.publish.samsung.PublishSetting import ru.litres.publish.samsung.exception.UploadApkException import ru.litres.publish.samsung.models.update.ApkFile @@ -18,10 +19,10 @@ import java.io.File import kotlin.math.roundToInt class UpdateAppRepository( + private val debugSetting: DebugSetting, private val networkClient: NetworkClient, private val uploadNetworkClient: NetworkClient ) { - fun update(apk: File, publishSetting: PublishSetting): Boolean { val sessionId = getUploadSessionId() val fileKey = uploadApk(sessionId, apk) @@ -37,6 +38,7 @@ class UpdateAppRepository( } private fun uploadApk(sessionId: String, file: File): String { + if (debugSetting.dryMode) return String() var prevProgress = 0 val uploadResult = uploadNetworkClient.upload(UPLOAD_APK, listOf(SESSION_ID_FIELD to sessionId)) .add { FileDataPart(file, name = "file") } @@ -66,6 +68,7 @@ class UpdateAppRepository( return key } + @Suppress("ReturnCount") private fun updateApplication(fileKey: String, publishSetting: PublishSetting): Boolean { val contentId = publishSetting.contentId ?: return false val paid = if (publishSetting.paid) YES_FIELD else NO_FIELD @@ -79,6 +82,12 @@ class UpdateAppRepository( ) val json = Json.encodeToJsonElement(data) + if (debugSetting.dryMode) { + println("Data for update: ") + println(data) + return true + } + val updateResult = networkClient.post(UPDATE_APPLICATION) .jsonBody(json.jsonObject.toString()) .responseObject(kotlinxDeserializerOf()) diff --git a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/usecase/PublishBuildUseCase.kt b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/usecase/PublishBuildUseCase.kt index 08d3936..c21766c 100644 --- a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/usecase/PublishBuildUseCase.kt +++ b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/usecase/PublishBuildUseCase.kt @@ -1,6 +1,7 @@ package ru.litres.publish.samsung.usecase import org.gradle.api.file.Directory +import ru.litres.publish.samsung.DebugSetting import ru.litres.publish.samsung.PublishSetting import ru.litres.publish.samsung.exception.UploadApkException import ru.litres.publish.samsung.network.NetworkClient @@ -13,6 +14,7 @@ import ru.litres.publish.samsung.utils.UPLOAD_API_BASE_URL import java.io.File class PublishBuildUseCase( + private val debugSetting: DebugSetting, private val networkClient: NetworkClient = NetworkClient(API_BASE_URL), private val uploadNetworkClient: NetworkClient = NetworkClient(UPLOAD_API_BASE_URL), private val jwtGenerator: JwtGenerator = JwtGenerator() @@ -31,7 +33,7 @@ class PublishBuildUseCase( uploadNetworkClient.appendCommonHeaders(mapOf(HEADER_SERVICE_ACCOUNT_ID to serviceId)) val generateTokenRepository = GenerateTokenRepository(networkClient, jwtGenerator) - val updateAppRepository = UpdateAppRepository(networkClient, uploadNetworkClient) + val updateAppRepository = UpdateAppRepository(debugSetting, networkClient, uploadNetworkClient) val accessToken = generateTokenRepository.getAccessToken(privateKey, serviceId) networkClient.setBearerAuth(accessToken) @@ -46,8 +48,13 @@ class PublishBuildUseCase( } } + // if dry mode - return fake file private fun Directory.findApkFile(): File { - return this.asFileTree.find { it.extension == "apk" } - ?: throw UploadApkException("Apk file not found in folder \"${this.asFile.absolutePath}\"") + return asFileTree.find { it.extension == "apk" } + ?: if (debugSetting.dryMode) { + File("/") + } else { + throw UploadApkException("Apk file not found in folder \"${this.asFile.absolutePath}\"") + } } } diff --git a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/utils/JwtGenerator.kt b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/utils/JwtGenerator.kt index c09c301..b5af397 100644 --- a/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/utils/JwtGenerator.kt +++ b/plugin-build/plugin/src/main/java/ru/litres/publish/samsung/utils/JwtGenerator.kt @@ -10,8 +10,6 @@ import java.io.StringReader import java.security.KeyFactory import java.security.spec.RSAPrivateCrtKeySpec import javax.management.openmbean.InvalidKeyException -import kotlin.time.DurationUnit -import kotlin.time.toDuration class JwtGenerator { @@ -23,15 +21,15 @@ class JwtGenerator { val kf = KeyFactory.getInstance("RSA") val key = kf.generatePrivate(keySpec) - val currentTime = System.currentTimeMillis().toDuration(DurationUnit.MILLISECONDS) - val expTime = currentTime.plus(TIME_TOKEN_ALIVE.toDuration(DurationUnit.MINUTES)) + val currentTime = System.currentTimeMillis().millisToSecond() + val expTime = currentTime + TIME_TOKEN_ALIVE return Jwts.builder() .signWith(key, SignatureAlgorithm.RS256) .claim(CLAIM_ISS, serviceAccountId) .claim(CLAIM_SCOPES, arrayOf(PUBLISHING_SCOPE)) - .claim(CLAIM_IAT, currentTime.inWholeSeconds) - .claim(CLAIM_EXP, expTime.inWholeSeconds) + .claim(CLAIM_IAT, currentTime) + .claim(CLAIM_EXP, expTime) .compact() } @@ -82,6 +80,8 @@ class JwtGenerator { ) } + private fun Long.millisToSecond() = this / MILLIS_IN_SECOND + companion object { private const val PUBLISHING_SCOPE = "publishing" @@ -95,7 +95,9 @@ class JwtGenerator { // expiration time in second private const val CLAIM_EXP = "exp" - // min - private const val TIME_TOKEN_ALIVE = 15 + // seconds + private const val TIME_TOKEN_ALIVE = 15 * 60 // 15 min + + private const val MILLIS_IN_SECOND = 1000 } }