Advanced-Portals/discord.gradle

76 lines
2.4 KiB
Groovy
Raw Normal View History

import org.apache.http.HttpEntity
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.apache.httpcomponents:httpmime:4.5.13"
}
}
apply from: 'env-variables.gradle'
def getReleaseChangelog() {
def changelogFile = file('CHANGELOG.md')
def changelog = "## [${changelogFile.text.split('\n## \\[')[1]}\n\n"// ${project.github}/blob/${branch}/CHANGELOG.md
return changelog;
}
task discordupload {
dependsOn(jar)
doLast {
String discordWebhook = System.getenv("DISCORD_WEBHOOK")
if(discordWebhook != null) {
println("Logging Into Discord")
CloseableHttpClient httpClient = HttpClients.createDefault()
HttpPost uploadFile = new HttpPost(discordWebhook)
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
if(!isRelease) {
builder.addTextBody("content", "New snapshot or testing build")
} else {
def maxLength = 2000
def content = "New release build\n" +
"```markdown\n" +
"${getReleaseChangelog()}\n" +
"```"
if (content.length() > maxLength) {
def trimmedChangelog = getReleaseChangelog().take(maxLength - 15) // Reserve space for ending message
content = "New release build\n" +
"```markdown\n" +
"${trimmedChangelog}\n" +
"``` [Truncated]"
}
builder.addTextBody("content", content)
}
builder.addBinaryBody("file", file(jar.archiveFile).newInputStream(), ContentType.APPLICATION_OCTET_STREAM, jar.archiveName)
HttpEntity multipart = builder.build()
uploadFile.setEntity(multipart)
CloseableHttpResponse response = httpClient.execute(uploadFile)
response.getEntity()
println("Posted build")
} else {
println("Discord webhook unspecified")
}
}
}