Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-58942] Honor -Djenkins.hook.url=… if defined #240

Merged
merged 4 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.4.1</version>
<version>2.16.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,11 @@ public class GitHubOrgWebHook {
private static final Logger LOGGER = Logger.getLogger(GitHubOrgWebHook.class.getName());
private static final List<GHEvent> EVENTS = Arrays.asList(GHEvent.REPOSITORY, GHEvent.PUSH, GHEvent.PULL_REQUEST, GHEvent.PULL_REQUEST_REVIEW_COMMENT);

/**
* Verify if exists a webhook by its URL.
*/
private static boolean existsHook(GHOrganization org, String url) throws IOException {
for (GHHook hook : org.getHooks()) {
if (hook.getConfig().get("url").equals(url)) {
return true;
}
}
return false;
}

public static void register(GitHub hub, String orgName) throws IOException {
String rootUrl = Jenkins.getActiveInstance().getRootUrl();
String rootUrl = System.getProperty("jenkins.hook.url");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jglick Does that automatically convert from the JENKINS_HOOK_URL env value as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I switched to the system property

  • for consistency with other configuration in Jenkins
  • because for my purposes it must be mutable inside the JVM, which environment variables are not (AFAIK)

In the long term, this probably belongs in JenkinsLocationConfiguration, but requiring such a core dependency would make this potentially unmergeable for months, so I thought it would be acceptable to start with an experimental system property.

if (rootUrl == null) {
rootUrl = Jenkins.get().getRootUrl();
}
if (rootUrl == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* The MIT License
*
* Copyright 2019 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.github_branch_source;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.kohsuke.github.GitHub;

public class GitHubOrgWebHookTest {

@Rule public JenkinsRule r = new JenkinsRule();
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());

@Issue("JENKINS-58942")
@Test public void registerCustom() throws Exception {
System.setProperty("jenkins.hook.url", "https://mycorp/hook-proxy/");
wireMockRule.stubFor(get(urlEqualTo("/api/users/myorg")).willReturn(aResponse().withBody("{\"login\":\"myorg\"}")));
wireMockRule.stubFor(get(urlEqualTo("/api/orgs/myorg")).willReturn(aResponse().withBody("{\"login\":\"myorg\",\"html_url\":\"https://github.com/myorg\"}")));
wireMockRule.stubFor(get(urlEqualTo("/api/orgs/myorg/hooks")).willReturn(aResponse().withBody("[]")));
wireMockRule.stubFor(post(urlEqualTo("/api/orgs/myorg/hooks")).withRequestBody(matchingJsonPath("$.config.url", equalTo("https://mycorp/hook-proxy/github-webhook/"))).willReturn(aResponse().withBody("{}")));
GitHub hub = Connector.connect("http://localhost:" + wireMockRule.port() + "/api/", null);
try {
GitHubOrgWebHook.register(hub, "myorg");
} finally {
Connector.release(hub);
}
}

}