Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.

Commit e3e6512

Browse files
Merge pull request #22 from sap-production/reviewRemoteAddress
review remote address
2 parents 6966468 + 0bce588 commit e3e6512

File tree

5 files changed

+33
-41
lines changed

5 files changed

+33
-41
lines changed

src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteBuildConfiguration.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class RemoteBuildConfiguration extends Builder implements SimpleBuildStep
9292
*/
9393
private final static Auth2 DEFAULT_AUTH = NullAuth.INSTANCE;
9494

95-
95+
9696
private static final int DEFAULT_POLLINTERVALL = 10;
9797
private static final String paramerizedBuildUrl = "/buildWithParameters";
9898
private static final String normalBuildUrl = "/build";
@@ -502,7 +502,10 @@ private String buildTriggerUrl(String jobNameOrUrl, String securityToken, Collec
502502

503503
if (context.effectiveRemoteServer.getHasBuildTokenRootSupport()) {
504504
// start building the proper URL based on known capabiltiies of the remote server
505-
triggerUrlString = context.effectiveRemoteServer.getRemoteAddress();
505+
if (context.effectiveRemoteServer.getAddress() == null) {
506+
throw new AbortException("The remote server address can not be empty, or it must be overridden on the job configuration.");
507+
}
508+
triggerUrlString = context.effectiveRemoteServer.getAddress();
506509
triggerUrlString += buildTokenRootUrl;
507510
triggerUrlString += getBuildTypeUrl(isRemoteJobParameterized);
508511
query = addToQueryString(query, "job=" + encodeValue(jobNameOrUrl)); //TODO: does it work with full URL?
@@ -815,7 +818,10 @@ public void performWaitForBuild(BuildContext context, Handle handle)
815818
private QueueItemData getQueueItemData(@Nonnull String queueId, @Nonnull BuildContext context)
816819
throws IOException {
817820

818-
String queueQuery = String.format("%s/queue/item/%s/api/json/", context.effectiveRemoteServer.getRemoteAddress(), queueId);
821+
if (context.effectiveRemoteServer.getAddress() == null) {
822+
throw new AbortException("The remote server address can not be empty, or it must be overridden on the job configuration.");
823+
}
824+
String queueQuery = String.format("%s/queue/item/%s/api/json/", context.effectiveRemoteServer.getAddress(), queueId);
819825
ConnectionResponse response = sendHTTPCall( queueQuery, "GET", context, 1 );
820826
JSONObject queueResponse = response.getBody();
821827

@@ -1187,7 +1193,10 @@ private String readInputStream(HttpURLConnection connection) throws IOException
11871193
@Nonnull
11881194
private JenkinsCrumb getCrumb(BuildContext context) throws IOException
11891195
{
1190-
String address = context.effectiveRemoteServer.getRemoteAddress();
1196+
String address = context.effectiveRemoteServer.getAddress();
1197+
if (address == null) {
1198+
throw new AbortException("The remote server address can not be empty, or it must be overridden on the job configuration.");
1199+
}
11911200
URL crumbProviderUrl;
11921201
try {
11931202
String xpathValue = URLEncoder.encode("concat(//crumbRequestField,\":\",//crumb)", "UTF-8");
@@ -1461,15 +1470,18 @@ private boolean isRemoteJobParameterized(JSONObject remoteJobMetadata) throws IO
14611470
return isParameterized;
14621471
}
14631472

1464-
protected static String generateJobUrl(RemoteJenkinsServer remoteServer, String jobNameOrUrl)
1473+
protected static String generateJobUrl(RemoteJenkinsServer remoteServer, String jobNameOrUrl) throws AbortException
14651474
{
14661475
if(isEmpty(jobNameOrUrl)) throw new IllegalArgumentException("Invalid job name/url: " + jobNameOrUrl);
14671476
String remoteJobUrl;
14681477
String _jobNameOrUrl = jobNameOrUrl.trim();
14691478
if(FormValidationUtils.isURL(_jobNameOrUrl)) {
14701479
remoteJobUrl = _jobNameOrUrl;
14711480
} else {
1472-
remoteJobUrl = remoteServer.getRemoteAddress();
1481+
remoteJobUrl = remoteServer.getAddress();
1482+
if (remoteJobUrl == null) {
1483+
throw new AbortException("The remote server address can not be empty, or it must be overridden on the job configuration.");
1484+
}
14731485
while(remoteJobUrl.endsWith("/")) remoteJobUrl = remoteJobUrl.substring(0, remoteJobUrl.length()-1);
14741486

14751487
String[] split = _jobNameOrUrl.trim().split("/");

src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteJenkinsServer.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,6 @@ public DescriptorImpl getDescriptor() {
135135
return (DescriptorImpl) super.getDescriptor();
136136
}
137137

138-
/**
139-
* @return the remote server address
140-
* @throws RuntimeException
141-
* if the address of the remote server was not set
142-
*/
143-
@Nonnull
144-
public String getRemoteAddress() {
145-
if (address == null) {
146-
throw new RuntimeException("The remote address can not be empty.");
147-
} else {
148-
try {
149-
new URL(address);
150-
} catch (MalformedURLException e) {
151-
throw new RuntimeException("Malformed address (" + address + "). Remember to indicate the protocol, i.e. http, https, etc.");
152-
}
153-
}
154-
return address;
155-
}
156138

157139
@Extension
158140
public static class DescriptorImpl extends Descriptor<RemoteJenkinsServer> {

src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/pipeline/Handle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public void setBuildData(BuildData buildData)
335335
public String toString() {
336336

337337
StringBuilder sb = new StringBuilder();
338-
sb.append(String.format("Handle [job=%s, remoteServerURL=%s, queueId=%s", remoteBuildConfiguration.getJob(), effectiveRemoteServer.getRemoteAddress(), queueId));
338+
sb.append(String.format("Handle [job=%s, remoteServerURL=%s, queueId=%s", remoteBuildConfiguration.getJob(), effectiveRemoteServer.getAddress(), queueId));
339339
if(buildStatus != null) sb.append(String.format(", buildStatus=%s", buildStatus));
340340
if(buildData != null) sb.append(String.format(", buildNumber=%s, buildUrl=%s", buildData.getBuildNumber(), buildData.getURL()));
341341
sb.append("]");

src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteBuildConfigurationTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public void testJobUrlHandling_withJobNameAndRemoteUrl() throws IOException {
193193
config.setJob("MyJob");
194194
config.setRemoteJenkinsUrl("http://test:8080");
195195
assertEquals("MyJob", config.getJob());
196-
assertEquals("http://test:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
196+
assertEquals("http://test:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
197197
}
198198

199199
@Test @WithoutJenkins
@@ -204,7 +204,7 @@ public void testJobUrlHandling_withJobNameAndRemoteName() throws IOException {
204204

205205
config.setRemoteJenkinsName("remoteJenkinsName");
206206
assertEquals("MyJob", config.getJob());
207-
assertEquals("http://test:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
207+
assertEquals("http://test:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
208208
}
209209

210210
@Test @WithoutJenkins
@@ -215,15 +215,15 @@ public void testJobUrlHandling_withMultiFolderJobNameAndRemoteName() throws IOEx
215215

216216
config.setRemoteJenkinsName("remoteJenkinsName");
217217
assertEquals("A/B/C/D/MyJob", config.getJob());
218-
assertEquals("http://test:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
218+
assertEquals("http://test:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
219219
}
220220

221221
@Test @WithoutJenkins
222222
public void testJobUrlHandling_withJobUrl() throws IOException {
223223
RemoteBuildConfiguration config = new RemoteBuildConfiguration();
224224
config.setJob("http://test:8080/job/folder/job/MyJob");
225225
assertEquals("http://test:8080/job/folder/job/MyJob", config.getJob()); //The value configured for "job"
226-
assertEquals("http://test:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
226+
assertEquals("http://test:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
227227
}
228228

229229
@Test @WithoutJenkins
@@ -233,7 +233,7 @@ public void testJobUrlHandling_withJobUrlAndRemoteUrl() throws IOException {
233233
config.setJob("http://testA:8080/job/folder/job/MyJobA");
234234
config.setRemoteJenkinsUrl("http://testB:8080");
235235
assertEquals("http://testA:8080/job/folder/job/MyJobA", config.getJob()); //The value configured for "job"
236-
assertEquals("http://testA:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
236+
assertEquals("http://testA:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
237237
}
238238

239239
@Test @WithoutJenkins
@@ -245,7 +245,7 @@ public void testJobUrlHandling_withJobUrlAndRemoteName() throws IOException {
245245

246246
config.setRemoteJenkinsName("remoteJenkinsName");
247247
assertEquals("http://testA:8080/job/folder/job/MyJobA", config.getJob()); //The value configured for "job"
248-
assertEquals("http://testA:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
248+
assertEquals("http://testA:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
249249
}
250250

251251
@Test @WithoutJenkins
@@ -270,12 +270,12 @@ public void testRemoteUrlOverridesRemoteName() throws IOException {
270270
config = mockGlobalRemoteHost(config, "remoteJenkinsName", "http://globallyConfigured:8080");
271271

272272
config.setRemoteJenkinsName("remoteJenkinsName");
273-
assertEquals("http://globallyConfigured:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
273+
assertEquals("http://globallyConfigured:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
274274

275275
//Now override remote host URL
276276
config.setRemoteJenkinsUrl("http://locallyOverridden:8080");
277277
assertEquals("MyJob", config.getJob());
278-
assertEquals("http://locallyOverridden:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
278+
assertEquals("http://locallyOverridden:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
279279
}
280280

281281
@Test @WithoutJenkins
@@ -312,7 +312,7 @@ public void testEvaluateEffectiveRemoteHost_globalConfigMissing_localOverrideHos
312312

313313
config.setRemoteJenkinsName("notConfiguredRemoteHost");
314314
config.setRemoteJenkinsUrl("http://locallyOverridden:8080");
315-
assertEquals("http://locallyOverridden:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
315+
assertEquals("http://locallyOverridden:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
316316
}
317317

318318
@Test @WithoutJenkins
@@ -322,15 +322,15 @@ public void testEvaluateEffectiveRemoteHost_globalConfigMissing_localOverrideJob
322322
config = mockGlobalRemoteHost(config, "remoteJenkinsName", "http://globallyConfigured:8080");
323323

324324
config.setRemoteJenkinsName("notConfiguredRemoteHost");
325-
assertEquals("http://localJobUrl:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
325+
assertEquals("http://localJobUrl:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
326326
}
327327

328328
@Test @WithoutJenkins
329329
public void testEvaluateEffectiveRemoteHost_localOverrideHostURL() throws IOException {
330330
RemoteBuildConfiguration config = new RemoteBuildConfiguration();
331331
config.setJob("MyJob");
332332
config.setRemoteJenkinsUrl("http://hostname:8080");
333-
assertEquals("http://hostname:8080", config.evaluateEffectiveRemoteHost(null).getRemoteAddress());
333+
assertEquals("http://hostname:8080", config.evaluateEffectiveRemoteHost(null).getAddress());
334334
}
335335

336336
@Test @WithoutJenkins
@@ -384,7 +384,7 @@ public void testRemoveHashParameters() {
384384
}
385385

386386
@Test @WithoutJenkins
387-
public void testGenerateJobUrl() throws MalformedURLException {
387+
public void testGenerateJobUrl() throws MalformedURLException, AbortException {
388388
RemoteJenkinsServer remoteServer = new RemoteJenkinsServer();
389389
remoteServer.setAddress("https://server:8080/jenkins");
390390

@@ -411,8 +411,8 @@ public void testGenerateJobUrl() throws MalformedURLException {
411411
try {
412412
RemoteJenkinsServer missingUrl = new RemoteJenkinsServer();
413413
RemoteBuildConfiguration.generateJobUrl(missingUrl, "JobName");
414-
Assert.fail("Expected RuntimeException");
415-
} catch(RuntimeException e) {}
414+
Assert.fail("Expected AbortException");
415+
} catch(AbortException e) {}
416416

417417
}
418418

src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteJenkinsServerTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public void testCloneBehaviour() throws Exception {
3737
verifyEqualsHashCode(server, clone);
3838
assertEquals("address", ADDRESS, clone.getAddress());
3939
assertEquals("address", server.getAddress(), clone.getAddress());
40-
assertEquals("remoteAddress", ADDRESS, clone.getRemoteAddress());
41-
assertEquals("remoteAddress", server.getRemoteAddress(), clone.getRemoteAddress());
4240
assertEquals("auth2", server.getAuth2(), clone.getAuth2());
4341
assertEquals("displayName", DISPLAY_NAME, clone.getDisplayName());
4442
assertEquals("displayName", server.getDisplayName(), clone.getDisplayName());

0 commit comments

Comments
 (0)