-
Notifications
You must be signed in to change notification settings - Fork 57
/
NexusService.groovy
180 lines (149 loc) · 6.72 KB
/
NexusService.groovy
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
170
171
172
173
174
175
176
177
178
179
180
package org.ods.services
@Grab(group='com.konghq', module='unirest-java', version='2.4.03', classifier='standalone')
import com.cloudbees.groovy.cps.NonCPS
import kong.unirest.Unirest
import org.apache.http.client.utils.URIBuilder
class NexusService {
static final String NEXUS_REPO_EXISTS_KEY = 'nexusRepoExists'
final URI baseURL
final String username
final String password
NexusService(String baseURL, String username, String password) {
if (!baseURL?.trim()) {
throw new IllegalArgumentException("Error: unable to connect to Nexus. 'baseURL' is undefined.")
}
if (!username?.trim()) {
throw new IllegalArgumentException("Error: unable to connect to Nexus. 'username' is undefined.")
}
if (!password?.trim()) {
throw new IllegalArgumentException("Error: unable to connect to Nexus. 'password' is undefined.")
}
try {
this.baseURL = new URIBuilder(baseURL).build()
} catch (e) {
throw new IllegalArgumentException(
"Error: unable to connect to Nexus. '${baseURL}' is not a valid URI."
).initCause(e)
}
this.username = username
this.password = password
}
static NexusService newFromEnv(def env) {
def c = readConfigFromEnv(env)
new NexusService(c.nexusUrl, c.nexusUsername, c.nexusPassword)
}
static Map readConfigFromEnv(def env) {
def config = [:]
if (env.NEXUS_URL?.trim()) {
config.nexusUrl = env.NEXUS_URL.trim()
} else if (env.NEXUS_HOST?.trim()) {
// Historically, the NEXUS_HOST variable contains the scheme.
config.nexusUrl = env.NEXUS_HOST.trim()
} else {
throw new IllegalArgumentException("Environment variable 'NEXUS_URL' is required")
}
if (env.NEXUS_USERNAME?.trim()) {
config.nexusUsername = env.NEXUS_USERNAME.trim()
} else {
throw new IllegalArgumentException('NEXUS_USERNAME is required, but not set')
}
if (env.NEXUS_PASSWORD?.trim()) {
config.nexusPassword = env.NEXUS_PASSWORD.trim()
} else {
throw new IllegalArgumentException('NEXUS_PASSWORD is required, but not set')
}
config
}
@NonCPS
URI storeArtifact(String repository, String directory, String name, byte[] artifact, String contentType) {
Map nexusParams = [
'raw.directory': directory,
'raw.asset1.filename': name,
]
return storeComplextArtifact(repository, artifact, contentType, 'raw', nexusParams)
}
URI storeArtifactFromFile(
String repository,
String directory,
String name,
File artifact,
String contentType) {
return storeArtifact(repository, directory, name, artifact.getBytes(), contentType)
}
@SuppressWarnings('LineLength')
@NonCPS
URI storeComplextArtifact(String repository, byte[] artifact, String contentType, String repositoryType, Map nexusParams = [ : ]) {
def restCall = Unirest.post("${this.baseURL}/service/rest/v1/components?repository={repository}")
.routeParam('repository', repository)
.basicAuth(this.username, this.password)
nexusParams.each { key, value ->
restCall = restCall.field(key, value)
}
restCall = restCall.field(
repositoryType == 'raw' || repositoryType == 'maven2' ? "${repositoryType}.asset1" : "${repositoryType}.asset",
new ByteArrayInputStream(artifact), contentType)
def response = restCall.asString()
response.ifSuccess {
if (response.getStatus() != 204) {
throw new RuntimeException(
'Error: unable to store artifact. ' +
"Nexus responded with code: '${response.getStatus()}' and message: '${response.getBody()}'."
)
}
}
response.ifFailure {
def message = 'Error: unable to store artifact. ' +
"Nexus responded with code: '${response.getStatus()}' and message: '${response.getBody()}'."
if (response.getStatus() == 404) {
message = "Error: unable to store artifact. Nexus could not be found at: '${this.baseURL}'."
}
throw new RuntimeException(message)
}
if (repositoryType == 'raw') {
return this.baseURL.resolve("/repository/${repository}/${nexusParams['raw.directory']}/" +
"${nexusParams['raw.asset1.filename']}")
}
return this.baseURL.resolve("/repository/${repository}")
}
@SuppressWarnings(['LineLength', 'JavaIoPackageAccess'])
@NonCPS
Map<URI, File> retrieveArtifact(String nexusRepository, String nexusDirectory, String name, String extractionPath) {
// https://nexus3-ods....../repository/leva-documentation/odsst-WIP/DTP-odsst-WIP-108.zip
String urlToDownload = "${this.baseURL}/repository/${nexusRepository}/${nexusDirectory}/${name}"
def restCall = Unirest.get("${urlToDownload}")
.basicAuth(this.username, this.password)
// hurray - unirest, in case file exists - don't do anything.
File artifactExists = new File("${extractionPath}/${name}")
if (artifactExists) {
artifactExists.delete()
}
def response = restCall.asFile("${extractionPath}/${name}")
response.ifFailure {
def message = 'Error: unable to get artifact. ' +
"Nexus responded with code: '${response.getStatus()}' and message: '${response.getBody()}'." +
" The url called was: ${urlToDownload}"
if (response.getStatus() == 404) {
message = "Error: unable to get artifact. Nexus could not be found at: '${urlToDownload}'."
}
// very weird, we get a 200 as failure with a good artifact, wtf.
if (response.getStatus() != 200) {
throw new RuntimeException(message)
}
}
return [
uri: this.baseURL.resolve("/repository/${nexusRepository}/${nexusDirectory}/${name}"),
content: response.getBody(),
]
}
boolean groupExists(String nexusRepository, String groupName) {
String urlToDownload =
"${this.baseURL}/service/rest/v1/search?repository=${nexusRepository}&group=/${groupName}"
def response = Unirest.get("${urlToDownload}")
.basicAuth(this.username, this.password)
.asString()
response.ifFailure {
throw new RuntimeException ("Could not retrieve data from '${urlToDownload}'")
}
return !response.getBody().contains('\"items\" : [ ]')
}
}