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-66079] Allow setting hookUrl from the global configuration page #376

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
hookSecretConfigs = null; // form binding might omit empty lists
try {
req.bindJSON(this, json);
// There is a type mismatch between the getter and the setter, so the bindJSON doesn't save
// the hookUrl properly
if (json != null && json.containsKey("hookUrl")) {
this.setHookUrl(json.getString("hookUrl"));
}
} catch (Exception e) {
LOGGER.debug("Problem while submitting form for GitHub Plugin ({})", e.getMessage(), e);
LOGGER.trace("GH form data: {}", json.toString());
Expand Down
120 changes: 69 additions & 51 deletions src/test/java/com/cloudbees/jenkins/GlobalConfigSubmitTest.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,100 @@
package com.cloudbees.jenkins;

import jenkins.model.Jenkins;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.html.HtmlTextInput;
import org.jenkinsci.plugins.github.GitHubPlugin;
import org.junit.Ignore;
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URL;
import org.jvnet.hudson.test.JenkinsSessionRule;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
* Test Class for {@link GitHubPushTrigger}.
*
* @author Seiji Sogabe
* @author Seiji Sogabe, Frank Genois
*/
@Ignore("Have troubles with memory consumption")
public class GlobalConfigSubmitTest {

public static final String OVERRIDE_HOOK_URL_CHECKBOX = "_.isOverrideHookUrl";
public static final String HOOK_URL_INPUT = "_.hookUrl";

private static final String WEBHOOK_URL = "http://jenkinsci.example.com/jenkins/github-webhook/";

@Rule
public JenkinsRule jenkins = new JenkinsRule();
public JenkinsSessionRule sessions = new JenkinsSessionRule();

@Test
public void shouldSetHookUrl() throws Exception {
HtmlForm form = globalConfig();

form.getInputByName(OVERRIDE_HOOK_URL_CHECKBOX).setChecked(true);
form.getInputByName(HOOK_URL_INPUT).setValue(WEBHOOK_URL);
jenkins.submit(form);

assertThat(GitHubPlugin.configuration().getHookUrl(), equalTo(new URL(WEBHOOK_URL)));
public void shouldBeAbleToParseAnEmptyConfig() throws Throwable {
sessions.then(r -> {
GitHubPluginConfig githubPluginConfig = GitHubPlugin.configuration();
assertThat("empty config by default", githubPluginConfig.getConfigs().isEmpty());
assertFalse("no override hook url by default", githubPluginConfig.isOverrideHookUrl());
assertEquals(
"uses default url by default",
getDefaultHookUrl(),
githubPluginConfig.getHookUrl().toString()
);
});
}

@Test
public void shouldNotSetHookUrl() throws Exception {
GitHubPlugin.configuration().setHookUrl(WEBHOOK_URL);

HtmlForm form = globalConfig();

form.getInputByName(OVERRIDE_HOOK_URL_CHECKBOX).setChecked(false);
form.getInputByName(HOOK_URL_INPUT).setValue("http://foo");
jenkins.submit(form);

assertThat(GitHubPlugin.configuration().getHookUrl(), equalTo(new URL(WEBHOOK_URL)));
public void shouldBeAbleToSetCustomHook() throws Throwable {
sessions.then(r -> {
HtmlForm config = r.createWebClient().goTo("configure").getFormByName("config");
HtmlTextInput textbox = config.getInputByName(HOOK_URL_INPUT);
textbox.setText("http://jenkinsci.example.com/jenkins/github-webhook/");
r.submit(config);

assertEquals(
"global config page let us edit the webhook url",
"http://jenkinsci.example.com/jenkins/github-webhook/",
GitHubPlugin.configuration().getHookUrl().toString()
);
});

sessions.then(r -> {
assertEquals(
"webhook url still present after restart of Jenkins",
"http://jenkinsci.example.com/jenkins/github-webhook/",
GitHubPlugin.configuration().getHookUrl().toString()
);
});
}

@Test
public void shouldNotOverrideAPreviousHookUrlIfNotChecked() throws Exception {
GitHubPlugin.configuration().setHookUrl(WEBHOOK_URL);

HtmlForm form = globalConfig();

form.getInputByName(OVERRIDE_HOOK_URL_CHECKBOX).setChecked(false);
form.getInputByName(HOOK_URL_INPUT).setValue("");
jenkins.submit(form);

assertThat(GitHubPlugin.configuration().getHookUrl(), equalTo(new URL(WEBHOOK_URL)));
public void shouldSetDefaultHookUrlWhenLeftEmpty() throws Throwable {
sessions.then(r -> {
HtmlForm config = r.createWebClient().goTo("configure").getFormByName("config");
HtmlTextInput textbox = config.getInputByName(HOOK_URL_INPUT);
textbox.setText("");
r.submit(config);

assertEquals(
"global config page let us edit the webhook url",
getDefaultHookUrl(),
GitHubPlugin.configuration().getHookUrl().toString()
);
});
}

public HtmlForm globalConfig() throws IOException, SAXException {
JenkinsRule.WebClient client = configureWebClient();
HtmlPage p = client.goTo("configure");
return p.getFormByName("config");
@Test
public void shouldSetDefaultHookUrlWhenGivenMalformedUrl() throws Throwable {
sessions.then(r -> {
HtmlForm config = r.createWebClient().goTo("configure").getFormByName("config");
HtmlTextInput textbox = config.getInputByName(HOOK_URL_INPUT);
textbox.setText("I'm not a URL!");
r.submit(config);

assertEquals(
"global config page let us edit the webhook url",
getDefaultHookUrl(),
GitHubPlugin.configuration().getHookUrl().toString()
);
});
}

private JenkinsRule.WebClient configureWebClient() {
JenkinsRule.WebClient client = jenkins.createWebClient();
client.setJavaScriptEnabled(true);
return client;
public String getDefaultHookUrl() {
return Jenkins.getInstance().getRootUrl() + "github-webhook/";
}
}