-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task to upload apk into Telegram
- Loading branch information
Showing
13 changed files
with
260 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...droid/build/publish/plugin/task/telegram/changelog/sender/api/TelegramWebhookSenderApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...de/android/build/publish/plugin/task/telegram/changelog/work/SendTelegramChangelogWork.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
.../kode/android/build/publish/plugin/task/telegram/distribution/TelegramDistributionTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ru.kode.android.build.publish.plugin.task.telegram.distribution | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.plugins.BasePlugin | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.InputFile | ||
import org.gradle.api.tasks.Optional | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.tasks.options.Option | ||
import org.gradle.workers.WorkQueue | ||
import org.gradle.workers.WorkerExecutor | ||
import ru.kode.android.build.publish.plugin.task.telegram.distribution.work.TelegramUploadWork | ||
import javax.inject.Inject | ||
|
||
abstract class TelegramDistributionTask | ||
@Inject | ||
constructor( | ||
private val workerExecutor: WorkerExecutor, | ||
) : DefaultTask() { | ||
init { | ||
description = "Task to send apk to Telegram" | ||
group = BasePlugin.BUILD_GROUP | ||
} | ||
|
||
@get:InputFile | ||
@get:Option( | ||
option = "buildVariantOutputFile", | ||
description = "Artifact output file (absolute path is expected)", | ||
) | ||
abstract val buildVariantOutputFile: RegularFileProperty | ||
|
||
@get:Input | ||
@get:Option(option = "botId", description = "Bot id where webhook posted") | ||
abstract val botId: Property<String> | ||
|
||
@get:Input | ||
@get:Option(option = "chatId", description = "Chat id where webhook posted") | ||
abstract val chatId: Property<String> | ||
|
||
@get:Input | ||
@get:Optional | ||
@get:Option(option = "topicId", description = "Unique identifier for the target message thread") | ||
abstract val topicId: Property<String> | ||
|
||
@TaskAction | ||
fun upload() { | ||
val outputFile = buildVariantOutputFile.asFile.get() | ||
val botId = botId.get() | ||
val chatId = chatId.get() | ||
val topicId = topicId.orNull | ||
val workQueue: WorkQueue = workerExecutor.noIsolation() | ||
workQueue.submit(TelegramUploadWork::class.java) { parameters -> | ||
parameters.outputFile.set(outputFile) | ||
parameters.botId.set(botId) | ||
parameters.chatId.set(chatId) | ||
parameters.topicId.set(topicId) | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...n/java/ru/kode/android/build/publish/plugin/task/telegram/distribution/api/TelegramApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ru.kode.android.build.publish.plugin.task.telegram.distribution.api | ||
|
||
import okhttp3.MultipartBody | ||
import okhttp3.RequestBody | ||
import retrofit2.Call | ||
import retrofit2.http.Multipart | ||
import retrofit2.http.POST | ||
import retrofit2.http.Part | ||
import retrofit2.http.PartMap | ||
import retrofit2.http.Url | ||
import ru.kode.android.build.publish.plugin.task.telegram.distribution.entity.TelegramUploadResponse | ||
|
||
internal interface TelegramApi { | ||
@POST | ||
@Multipart | ||
fun upload( | ||
@Url webhookUrl: String, | ||
@PartMap params: HashMap<String, RequestBody>, | ||
@Part filePart: MultipartBody.Part, | ||
): Call<TelegramUploadResponse> | ||
} |
10 changes: 10 additions & 0 deletions
10
.../android/build/publish/plugin/task/telegram/distribution/entity/TelegramUploadResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ru.kode.android.build.publish.plugin.task.telegram.distribution.entity | ||
|
||
import com.squareup.moshi.JsonClass | ||
|
||
@JsonClass(generateAdapter = true) | ||
@Suppress("ConstructorParameterNaming") // network model | ||
internal data class TelegramUploadResponse( | ||
val ok: Boolean, | ||
val error_code: String?, | ||
) |
72 changes: 72 additions & 0 deletions
72
...kode/android/build/publish/plugin/task/telegram/distribution/uploader/TelegramUploader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ru.kode.android.build.publish.plugin.task.telegram.distribution.uploader | ||
|
||
import com.squareup.moshi.Moshi | ||
import okhttp3.MultipartBody | ||
import okhttp3.OkHttpClient | ||
import okhttp3.RequestBody.Companion.asRequestBody | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.gradle.api.logging.Logger | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.moshi.MoshiConverterFactory | ||
import ru.kode.android.build.publish.plugin.task.slack.distribution.uploader.createPartFromString | ||
import ru.kode.android.build.publish.plugin.task.telegram.distribution.api.TelegramApi | ||
import ru.kode.android.build.publish.plugin.util.UploadException | ||
import ru.kode.android.build.publish.plugin.util.executeOrThrow | ||
import java.io.File | ||
import java.util.concurrent.TimeUnit | ||
|
||
internal class TelegramUploader(logger: Logger) { | ||
private val client = | ||
OkHttpClient.Builder() | ||
.connectTimeout(HTTP_CONNECT_TIMEOUT_MINUTES, TimeUnit.MINUTES) | ||
.readTimeout(HTTP_CONNECT_TIMEOUT_MINUTES, TimeUnit.MINUTES) | ||
.writeTimeout(HTTP_CONNECT_TIMEOUT_MINUTES, TimeUnit.MINUTES) | ||
.apply { | ||
val loggingInterceptor = HttpLoggingInterceptor { message -> logger.info(message) } | ||
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | ||
addNetworkInterceptor(loggingInterceptor) | ||
} | ||
.build() | ||
|
||
private val moshi = Moshi.Builder().build() | ||
|
||
private val api = | ||
Retrofit.Builder() | ||
.baseUrl(STUB_BASE_URL) | ||
.client(client) | ||
.addConverterFactory(MoshiConverterFactory.create(moshi)) | ||
.build() | ||
.create(TelegramApi::class.java) | ||
|
||
fun upload( | ||
webhookUrl: String, | ||
chatId: String, | ||
topicId: String?, | ||
file: File, | ||
) { | ||
val filePart = | ||
MultipartBody.Part.createFormData( | ||
"document", | ||
file.name, | ||
file.asRequestBody(), | ||
) | ||
val params = | ||
if (topicId != null) { | ||
hashMapOf( | ||
"message_thread_id" to createPartFromString(topicId), | ||
"chat_id" to createPartFromString(chatId), | ||
) | ||
} else { | ||
hashMapOf( | ||
"chat_id" to createPartFromString(chatId), | ||
) | ||
} | ||
val response = api.upload(webhookUrl, params, filePart).executeOrThrow() | ||
if (!response.ok) { | ||
throw UploadException("Telegram uploading failed ${response.error_code}") | ||
} | ||
} | ||
} | ||
|
||
private const val HTTP_CONNECT_TIMEOUT_MINUTES = 3L | ||
private const val STUB_BASE_URL = "http://localhost/" |
41 changes: 41 additions & 0 deletions
41
...u/kode/android/build/publish/plugin/task/telegram/distribution/work/TelegramUploadWork.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ru.kode.android.build.publish.plugin.task.telegram.distribution.work | ||
|
||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.logging.Logging | ||
import org.gradle.api.provider.Property | ||
import org.gradle.workers.WorkAction | ||
import org.gradle.workers.WorkParameters | ||
import ru.kode.android.build.publish.plugin.task.telegram.distribution.uploader.TelegramUploader | ||
import ru.kode.android.build.publish.plugin.util.UploadStreamTimeoutException | ||
|
||
interface TelegramUploadParameters : WorkParameters { | ||
val outputFile: RegularFileProperty | ||
val botId: Property<String> | ||
val chatId: Property<String> | ||
val topicId: Property<String> | ||
} | ||
|
||
abstract class TelegramUploadWork : WorkAction<TelegramUploadParameters> { | ||
private val logger = Logging.getLogger(this::class.java) | ||
private val uploader = TelegramUploader(logger) | ||
|
||
@Suppress("SwallowedException") // see logs below | ||
override fun execute() { | ||
try { | ||
val url = SEND_DOCUMENT_WEB_HOOK.format(parameters.botId.get()) | ||
uploader.upload( | ||
url, | ||
parameters.chatId.get(), | ||
parameters.topicId.orNull, | ||
parameters.outputFile.asFile.get(), | ||
) | ||
} catch (ex: UploadStreamTimeoutException) { | ||
logger.error( | ||
"telegram upload failed with timeout exception, " + | ||
"but probably uploaded", | ||
) | ||
} | ||
} | ||
} | ||
|
||
private const val SEND_DOCUMENT_WEB_HOOK = "https://api.telegram.org/bot%s/sendDocument" |