-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
ReleaseWebhook.kt
52 lines (44 loc) · 1.46 KB
/
ReleaseWebhook.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package task
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.append
import io.ktor.serialization.gson.gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
/** Task to send webhooks to discord. */
abstract class ReleaseWebhook : DefaultTask() {
/** Configured extension. */
@get:Input
lateinit var extension: WebhookExtension
/** Ktor client for easy requests. */
private val client = HttpClient(CIO) {
install(ContentNegotiation) {
gson()
}
}
@TaskAction
fun webhook() {
// The webhook url configured in the gradle.properties
val url = System.getenv("DISCORD_WEBHOOK")
runBlocking(Dispatchers.IO) {
val response = client.post(url) {
headers {
append(HttpHeaders.ContentType, ContentType.Application.Json)
}
setBody(extension.build())
}
// Should be using logger, but eh
println("Webhook result: ${response.status}")
}
}
}