Skip to content

Commit

Permalink
Merge pull request #2 from Litres/fix_jvm_problem
Browse files Browse the repository at this point in the history
Fix strange error in duration from gitlab
  • Loading branch information
Toxa2033 authored Nov 30, 2022
2 parents 781162a + 6670ec4 commit dee44d2
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ plugins {
dependencies {
classpath("ru.litres.plugin:plugin:{latest_version}")
}

//app build.gradle.kts
apply(plugin = "ru.litres.plugin.publish.samsung")
```
Expand All @@ -59,7 +59,7 @@ plugins {
dependencies {
classpath "ru.litres.plugin:plugin:{latest_version}"
}
//app build.gradle.kts
apply plugin: "ru.litres.plugin.publish.samsung"
```
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ samsungPublishConfig {
publishSetting {
contentId = "..."
}

debug {
dryMode = false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.litres.publish.samsung

open class DebugSetting {
/**
* Mode for testing all before upload
*/
var dryMode: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ abstract class SamsungPublishExtension @Inject constructor(
fun publishSetting(action: Action<PublishSetting>) {
action.execute(publishSetting)
}

val debugSetting: DebugSetting = objects.newInstance(DebugSetting::class.java)

fun debug(action: Action<DebugSetting>) {
action.execute(debugSetting)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SamsungPublishPlugin : Plugin<Project> {
it.privateKey.set(extension.privateKey)
it.serviceAccountId.set(extension.serviceAccountId)
it.publishSetting = extension.publishSetting
it.debugSetting = extension.debugSetting
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ abstract class SamsungPublishTask : DefaultTask() {
@get:Input
abstract var publishSetting: PublishSetting

@get:Input
abstract var debugSetting: DebugSetting

@Suppress("ThrowsCount")
@TaskAction
fun publish() {
val key = privateKey.orNull ?: throw NotFoundRequiredField("privateKey")
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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") }
Expand Down Expand Up @@ -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
Expand All @@ -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<UpdateDataResponse>(kotlinxDeserializerOf())
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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}\"")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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()
}

Expand Down Expand Up @@ -82,6 +80,8 @@ class JwtGenerator {
)
}

private fun Long.millisToSecond() = this / MILLIS_IN_SECOND

companion object {
private const val PUBLISHING_SCOPE = "publishing"

Expand All @@ -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
}
}

0 comments on commit dee44d2

Please sign in to comment.