Skip to content

Commit 376f176

Browse files
authored
Merge pull request #240 from jglick/JENKINS_HOOK_URL
[JENKINS-58942] Honor -Djenkins.hook.url=… if defined
2 parents da418a6 + 9941d05 commit 376f176

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
<dependency>
155155
<groupId>com.github.tomakehurst</groupId>
156156
<artifactId>wiremock-standalone</artifactId>
157-
<version>2.4.1</version>
157+
<version>2.16.0</version>
158158
<scope>test</scope>
159159
</dependency>
160160
<dependency>

src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubOrgWebHook.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,11 @@ public class GitHubOrgWebHook {
4848
private static final Logger LOGGER = Logger.getLogger(GitHubOrgWebHook.class.getName());
4949
private static final List<GHEvent> EVENTS = Arrays.asList(GHEvent.REPOSITORY, GHEvent.PUSH, GHEvent.PULL_REQUEST, GHEvent.PULL_REQUEST_REVIEW_COMMENT);
5050

51-
/**
52-
* Verify if exists a webhook by its URL.
53-
*/
54-
private static boolean existsHook(GHOrganization org, String url) throws IOException {
55-
for (GHHook hook : org.getHooks()) {
56-
if (hook.getConfig().get("url").equals(url)) {
57-
return true;
58-
}
59-
}
60-
return false;
61-
}
62-
6351
public static void register(GitHub hub, String orgName) throws IOException {
64-
String rootUrl = Jenkins.getActiveInstance().getRootUrl();
52+
String rootUrl = System.getProperty("jenkins.hook.url");
53+
if (rootUrl == null) {
54+
rootUrl = Jenkins.get().getRootUrl();
55+
}
6556
if (rootUrl == null) {
6657
return;
6758
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2019 CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.jenkinsci.plugins.github_branch_source;
26+
27+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
28+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
29+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
import org.jvnet.hudson.test.Issue;
33+
import org.jvnet.hudson.test.JenkinsRule;
34+
import org.kohsuke.github.GitHub;
35+
36+
public class GitHubOrgWebHookTest {
37+
38+
@Rule public JenkinsRule r = new JenkinsRule();
39+
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
40+
41+
@Issue("JENKINS-58942")
42+
@Test public void registerCustom() throws Exception {
43+
System.setProperty("jenkins.hook.url", "https://mycorp/hook-proxy/");
44+
wireMockRule.stubFor(get(urlEqualTo("/api/users/myorg")).willReturn(aResponse().withBody("{\"login\":\"myorg\"}")));
45+
wireMockRule.stubFor(get(urlEqualTo("/api/orgs/myorg")).willReturn(aResponse().withBody("{\"login\":\"myorg\",\"html_url\":\"https://github.com/myorg\"}")));
46+
wireMockRule.stubFor(get(urlEqualTo("/api/orgs/myorg/hooks")).willReturn(aResponse().withBody("[]")));
47+
wireMockRule.stubFor(post(urlEqualTo("/api/orgs/myorg/hooks")).withRequestBody(matchingJsonPath("$.config.url", equalTo("https://mycorp/hook-proxy/github-webhook/"))).willReturn(aResponse().withBody("{}")));
48+
GitHub hub = Connector.connect("http://localhost:" + wireMockRule.port() + "/api/", null);
49+
try {
50+
GitHubOrgWebHook.register(hub, "myorg");
51+
} finally {
52+
Connector.release(hub);
53+
}
54+
}
55+
56+
}

0 commit comments

Comments
 (0)