Skip to content

Commit 5968973

Browse files
authored
ensure root URL ends with slash (#71)
1 parent 0a8dcec commit 5968973

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabHookCreator.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package io.jenkins.plugins.gitlabbranchsource;
22

33
import com.damnhandy.uri.template.UriTemplate;
4+
import com.damnhandy.uri.template.UriTemplateBuilder;
45
import io.jenkins.plugins.gitlabserverconfig.credentials.PersonalAccessToken;
56
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
67
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers;
78
import java.net.MalformedURLException;
89
import java.net.URL;
910
import java.util.logging.Level;
1011
import java.util.logging.Logger;
11-
import jenkins.model.JenkinsLocationConfiguration;
12+
import jenkins.model.Jenkins;
1213
import jenkins.scm.api.SCMNavigatorOwner;
1314
import org.apache.commons.lang.StringUtils;
1415
import org.gitlab4j.api.GitLabApi;
@@ -145,18 +146,18 @@ public static void createSystemHookWhenMissing(GitLabServer server,
145146
}
146147

147148
public static String getHookUrl(boolean isWebHook) {
148-
JenkinsLocationConfiguration locationConfiguration = JenkinsLocationConfiguration.get();
149-
String rootUrl = locationConfiguration.getUrl();
149+
String rootUrl = Jenkins.get().getRootUrl();
150150
if (StringUtils.isBlank(rootUrl)) {
151151
return "";
152152
}
153153
checkURL(rootUrl);
154-
String pronoun = "/gitlab-systemhook";
154+
UriTemplateBuilder templateBuilder = UriTemplate.buildFromTemplate(rootUrl);
155155
if (isWebHook) {
156-
pronoun = "/gitlab-webhook";
156+
templateBuilder.literal("gitlab-webhook");
157+
} else {
158+
templateBuilder.literal("gitlab-systemhook");
157159
}
158-
return UriTemplate.buildFromTemplate(rootUrl).literal(pronoun).literal("/post").build()
159-
.expand();
160+
return templateBuilder.literal("/post").build().expand();
160161
}
161162

162163
static void checkURL(String url) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.jenkins.plugins.gitlabbranchsource;
2+
3+
import java.util.Arrays;
4+
import jenkins.model.JenkinsLocationConfiguration;
5+
import org.junit.ClassRule;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.junit.runners.Parameterized;
9+
import org.junit.runners.Parameterized.Parameters;
10+
import org.jvnet.hudson.test.JenkinsRule;
11+
12+
import static org.hamcrest.Matchers.containsString;
13+
import static org.hamcrest.Matchers.not;
14+
import static org.hamcrest.core.Is.is;
15+
import static org.junit.Assert.assertThat;
16+
17+
@RunWith(Parameterized.class)
18+
public class GitLabHookCreatorParameterizedTest {
19+
20+
@ClassRule
21+
public static JenkinsRule r = new JenkinsRule();
22+
23+
private final String jenkinsUrl;
24+
private final boolean hookType;
25+
private final String expectedPath;
26+
27+
@Parameters(name = "check {0}")
28+
public static Iterable<Object[]> data() {
29+
return Arrays.asList(new Object[][]{
30+
{"intranet.local:8080", false, "/gitlab-systemhook/post"},
31+
{"intranet.local", true, "/gitlab-webhook/post"},
32+
{"www.mydomain.com:8000", true, "/gitlab-webhook/post"},
33+
{"www.mydomain.com", false, "/gitlab-systemhook/post"}
34+
});
35+
}
36+
37+
public GitLabHookCreatorParameterizedTest(String jenkinsUrl, boolean hookType, String expectedPath) {
38+
this.jenkinsUrl = jenkinsUrl;
39+
this.hookType = hookType;
40+
this.expectedPath = expectedPath;
41+
}
42+
43+
@Test
44+
public void hookUrl() {
45+
Arrays.asList("http://", "https://").forEach(
46+
proto -> {
47+
String expected = proto + jenkinsUrl + expectedPath;
48+
JenkinsLocationConfiguration.get().setUrl(proto + jenkinsUrl);
49+
String hookUrl = GitLabHookCreator.getHookUrl(hookType);
50+
GitLabHookCreator.checkURL(hookUrl);
51+
assertThat(hookUrl.replaceAll(proto, ""), not(containsString("//")));
52+
assertThat(hookUrl, is(expected));
53+
});
54+
}
55+
}

0 commit comments

Comments
 (0)