-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathdiscord.gradle
96 lines (73 loc) · 3.66 KB
/
discord.gradle
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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: rootProject.file('env-variables.gradle')
apply from: rootProject.file('changelog-util.gradle')
task discordupload {
group = 'Distribute'
dependsOn(jar)
doLast {
String discordWebhook = isRelease ? System.getenv("DISCORD_RELEASE_WEBHOOK") : System.getenv("DISCORD_WEBHOOK")
if(discordWebhook != null) {
def jarFile = file(jar.archiveFile)
long fileSizeInBytes = jarFile.length()
double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0)
println("Selected file: ${jarFile.name}")
println("File size: ${String.format("%.2f", fileSizeInMB)} MB")
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" +
"${getReleaseChangelog()}\n"
def downloadLink = "\n\n[Download here](https://modrinth.com/plugin/advanced-portals)"
if(isRelease && content.length() + downloadLink.length() <= maxLength) {
content += downloadLink
}
if (content.length() + downloadLink.length() > maxLength) {
def afterMessage = "[See more on GitHub](<https://github.com/sekwah41/Advanced-Portals/releases>)\n\n"
def truncatedContent = content.replaceAll(/(http[s]?:\/\/[^\s)]+)/, '<$1>').take(maxLength - afterMessage.length() - 1)
def lastFullLine = truncatedContent.lastIndexOf("\n")
content = "${truncatedContent.take(lastFullLine)}\n"
content += afterMessage
content += downloadLink
}
builder.addTextBody("content", content, ContentType.create("text/plain", "UTF-8"))
}
if(!isRelease) {
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()
if(response.getStatusLine().getStatusCode() != 200) {
println("Failed to post build to Discord.")
println("Status Code: " + response.getStatusLine().getStatusCode())
println("Reason Phrase: " + response.getStatusLine().getReasonPhrase())
String responseContent = response.getEntity() != null ?
org.apache.http.util.EntityUtils.toString(response.getEntity()) : "No content"
println("Response Content: " + responseContent)
throw new RuntimeException("Failed to post build to Discord")
}
println("Posted build")
} else {
println("Discord webhook unspecified")
}
}
}