-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
SonatypeHttpApi.scala
169 lines (144 loc) · 4.8 KB
/
SonatypeHttpApi.scala
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
157
158
159
160
161
162
163
164
165
166
167
168
169
package mill.scalalib.publish
import java.util.Base64
import scala.annotation.tailrec
import scala.concurrent.duration._
import mill.main.BuildInfo
import requests.BaseSession
import ujson.ParseException
import requests.Session
class SonatypeHttpApi(
uri: String,
credentials: String,
readTimeout: Int,
connectTimeout: Int
) {
val http: Session = requests.Session(
readTimeout = readTimeout,
connectTimeout = connectTimeout,
maxRedirects = 0,
check = false
)
private val base64Creds = base64(credentials)
private val commonHeaders = Seq(
"Authorization" -> s"Basic $base64Creds",
"Accept" -> "application/json",
"Content-Type" -> "application/json",
"User-Agent" -> s"mill-${BuildInfo.millVersion}${BaseSession.defaultHeaders.get("User-Agent").map(" (" + _ + ")").getOrElse("")}"
)
// https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles.html
def getStagingProfileUri(groupId: String): String = {
val response = withRetry(
http.get(
s"$uri/staging/profiles",
headers = commonHeaders
)
)
if (!response.is2xx) {
throw new Exception(s"$uri/staging/profiles returned ${response.statusCode}")
}
val resourceUri =
ujson
.read(response.text())("data")
.arr
.find(profile => groupId.split('.').startsWith(profile("name").str.split('.')))
.map(_("resourceURI").str)
resourceUri.getOrElse(
throw new RuntimeException(
s"Could not find staging profile for groupId: ${groupId}"
)
)
}
def getStagingRepoState(stagingRepoId: String): String = {
val response = http.get(
s"${uri}/staging/repository/${stagingRepoId}",
headers = commonHeaders
)
try {
ujson.read(response.text())("type").str
} catch {
case e: ParseException =>
throw new RuntimeException(
s"Could not parse HTTP response. ${e.getMessage()}" + "\n" + s"Raw response: ${response}",
e
)
}
}
// https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_start.html
def createStagingRepo(profileUri: String, groupId: String): String = {
val response = withRetry(http.post(
s"${profileUri}/start",
headers = commonHeaders,
data = s"""{"data": {"description": "fresh staging profile for ${groupId}"}}"""
))
if (!response.is2xx) {
throw new RuntimeException(
s"$uri/staging/profiles returned ${response.statusCode}\n${response.text()}"
)
}
ujson.read(response.text())("data")("stagedRepositoryId").str
}
// https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_finish.html
def closeStagingRepo(profileUri: String, repositoryId: String): Boolean = {
val response = withRetry(
http.post(
s"${profileUri}/finish",
headers = commonHeaders,
data =
s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "closing staging repository"}}"""
)
)
response.statusCode == 201
}
// https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_promote.html
def promoteStagingRepo(profileUri: String, repositoryId: String): Boolean = {
val response = withRetry(
http.post(
s"${profileUri}/promote",
headers = commonHeaders,
data =
s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "promote staging repository"}}"""
)
)
response.statusCode == 201
}
// https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_drop.html
def dropStagingRepo(profileUri: String, repositoryId: String): Boolean = {
val response = withRetry(
http.post(
s"${profileUri}/drop",
headers = commonHeaders,
data =
s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "drop staging repository"}}"""
)
)
response.statusCode == 201
}
private val uploadTimeout = 5.minutes.toMillis.toInt
def upload(uri: String, data: Array[Byte]): requests.Response = {
http.put(
uri,
readTimeout = uploadTimeout,
headers = Seq(
"Content-Type" -> "application/binary",
"Authorization" -> s"Basic ${base64Creds}"
),
data = data
)
}
@tailrec
private def withRetry(
request: => requests.Response,
retries: Int = 10,
initialTimeout: FiniteDuration = 100.millis
): requests.Response = {
val resp = request
if (resp.is5xx && retries > 0) {
Thread.sleep(initialTimeout.toMillis)
withRetry(request, retries - 1, initialTimeout * 2)
} else {
resp
}
}
private def base64(s: String) =
new String(Base64.getEncoder.encode(s.getBytes))
}