-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsonatype-api.gradle
More file actions
156 lines (137 loc) · 5.96 KB
/
sonatype-api.gradle
File metadata and controls
156 lines (137 loc) · 5.96 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 公共方法:获取认证信息
def getSonatypeCredentials() {
def username = project.findProperty('ossrhUsername') ?: System.getenv('OSSRH_USERNAME')
def password = project.findProperty('ossrhPassword') ?: System.getenv('OSSRH_PASSWORD')
if (!username || !password) {
throw new GradleException('OSSRH credentials are required for Sonatype API operations')
}
def auth = "${username}:${password}".bytes.encodeBase64().toString()
return "Bearer ${auth}"
}
def callSonatypeApi(String method, String url, Closure responseHandler = null) {
try {
def connection = new URL("https://ossrh-staging-api.central.sonatype.com" + url).openConnection() as HttpURLConnection
connection.requestMethod = method
connection.setRequestProperty("Authorization", getSonatypeCredentials())
if (method == "POST") {
connection.setRequestProperty("Content-Type", "application/json")
connection.doOutput = true
}
def responseCode = connection.responseCode
def responseMessage = connection.responseMessage
if (responseHandler) {
responseHandler.call(connection, responseCode, responseMessage)
} else {
if (responseCode >= 200 && responseCode < 300) {
println "Sonatype API call successful: ${responseCode} ${responseMessage}"
def inputStream = connection.inputStream
if (inputStream) {
println "${inputStream.text}"
}
} else {
println "Sonatype API call failed: ${responseCode} ${responseMessage}"
def errorStream = connection.errorStream
if (errorStream) {
def errorText = errorStream.text
println "Error details: ${errorText}"
}
}
}
return responseCode
} catch (Exception e) {
throw new GradleException("Failed to call Sonatype API: ${e.message}", e)
}
}
// list sonatype staging repositories
tasks.register('sonatypeList') {
group = 'publishing'
description = 'List Sonatype staging repositories'
onlyIf {
!project.version.endsWith('SNAPSHOT')
}
doLast {
def url = "/manual/search/repositories?ip=any&profile_id=${project.group}"
callSonatypeApi("GET", url)
}
}
// notify sonatype upload
tasks.register('sonatypeUpload') {
group = 'publishing'
description = 'Notify Sonatype that upload is complete'
onlyIf {
!project.version.endsWith('SNAPSHOT')
}
doLast {
def url ="/manual/upload/defaultRepository/${project.group}"
callSonatypeApi("POST", url) { connection, responseCode, responseMessage ->
if (responseCode >= 200 && responseCode < 300) {
println "Successfully notified Sonatype Staging API: ${responseCode} ${responseMessage}"
} else {
println "Failed to notify Sonatype Staging API: ${responseCode} ${responseMessage}"
def errorText
def errorStream = connection.errorStream
if (errorStream) {
errorText = errorStream.text
println "Error details: ${errorText}"
} else {
errorText = "No error details available"
}
throw new GradleException("Failed to notify Sonatype Staging API: ${responseCode} ${responseMessage}\nError details: ${errorText}")
}
}
}
}
tasks.register('sonatypeUploadRepository') {
group = 'publishing'
description = 'Upload to a specific Sonatype repository (requires -PrepositoryKey=<key>)'
onlyIf {
if (!project.hasProperty('repositoryKey')) {
throw new GradleException('Repository key is required. Use -PrepositoryKey=<key>')
}
return true
}
doLast {
def repositoryKey = project.property('repositoryKey')
def url = "/manual/upload/repository/${repositoryKey}"
callSonatypeApi("POST", url) { connection, responseCode, responseMessage ->
if (responseCode >= 200 && responseCode < 300) {
println "Successfully uploaded to repository ${repositoryKey}: ${responseCode} ${responseMessage}"
} else {
println "Failed to upload to repository ${repositoryKey}: ${responseCode} ${responseMessage}"
def errorStream = connection.errorStream
if (errorStream) {
def errorText = errorStream.text
println "Error details: ${errorText}"
}
throw new GradleException("Failed to upload to repository ${repositoryKey}: ${responseCode} ${responseMessage}")
}
}
}
}
tasks.register('sonatypeDropRepository') {
group = 'publishing'
description = 'Drop a specific Sonatype repository (requires -PrepositoryKey=<key>)'
onlyIf {
if (!project.hasProperty('repositoryKey')) {
throw new GradleException('Repository key is required. Use -PrepositoryKey=<key>')
}
return true
}
doLast {
def repositoryKey = project.property('repositoryKey')
def url = "/manual/drop/repository/${repositoryKey}"
callSonatypeApi("DELETE", url) { connection, responseCode, responseMessage ->
if (responseCode >= 200 && responseCode < 300) {
println "Successfully dropped repository ${repositoryKey}: ${responseCode} ${responseMessage}"
} else {
println "Failed to drop repository ${repositoryKey}: ${responseCode} ${responseMessage}"
def errorStream = connection.errorStream
if (errorStream) {
def errorText = errorStream.text
println "Error details: ${errorText}"
}
throw new GradleException("Failed to drop repository ${repositoryKey}: ${responseCode} ${responseMessage}")
}
}
}
}