Skip to content
Closed
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
16 changes: 16 additions & 0 deletions pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<concurrency>1</concurrency>
<java.level>8</java.level>
<workflow.version>1.14.2</workflow.version>
<configuration-as-code.version>1.12</configuration-as-code.version>
</properties>

<repositories>
Expand Down Expand Up @@ -190,6 +191,21 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cloudbees/jenkins/GitHubPushTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,11 @@ public void clearCredentials() {
}

/**
* @deprecated use {@link GitHubPluginConfig#isOverrideHookURL()}
* @deprecated use {@link GitHubPluginConfig#isOverrideHookUrl()}
*/
@Deprecated
public boolean hasOverrideURL() {
return GitHubPlugin.configuration().isOverrideHookURL();
return GitHubPlugin.configuration().isOverrideHookUrl();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.github.GitHub;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.interceptor.RequirePOST;
Expand All @@ -40,6 +41,7 @@

import static com.google.common.base.Charsets.UTF_8;
import static java.lang.String.format;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.jenkinsci.plugins.github.config.GitHubServerConfig.allowedToManageHooks;
import static org.jenkinsci.plugins.github.config.GitHubServerConfig.loginToGithub;
Expand Down Expand Up @@ -68,8 +70,6 @@ public class GitHubPluginConfig extends GlobalConfiguration {
private URL hookUrl;
private HookSecretConfig hookSecretConfig = new HookSecretConfig(null);

private transient boolean overrideHookUrl;

/**
* Used to get current instance identity.
* It compared with same value when testing hook url availability in {@link #doCheckHookUrl(String)}
Expand All @@ -87,6 +87,7 @@ public GitHubPluginConfig(List<GitHubServerConfig> configs) {
}

@SuppressWarnings("unused")
@DataBoundSetter
public void setConfigs(List<GitHubServerConfig> configs) {
this.configs = configs;
}
Expand All @@ -99,16 +100,19 @@ public boolean isManageHooks() {
return from(getConfigs()).filter(allowedToManageHooks()).first().isPresent();
}

public void setHookUrl(URL hookUrl) {
if (overrideHookUrl) {
this.hookUrl = hookUrl;
} else {
@DataBoundSetter
public void setHookUrl(String hookUrl) {
if (isEmpty(hookUrl)) {
this.hookUrl = null;
} else {
this.hookUrl = parseHookUrl(hookUrl);
}
}

@DataBoundSetter
@Deprecated
public void setOverrideHookUrl(boolean overrideHookUrl) {
this.overrideHookUrl = overrideHookUrl;

}

/**
Expand All @@ -123,10 +127,16 @@ public URL getHookUrl() throws GHPluginConfigException {
}
}

public boolean isOverrideHookURL() {
@SuppressWarnings("unused")
public boolean isOverrideHookUrl() {
return hookUrl != null;
}

@Deprecated
public boolean isOverrideHookURL() {
return isOverrideHookUrl();
}

/**
* Filters all stored configs against given predicate then
* logs in as the given user and returns the non null connection objects
Expand Down Expand Up @@ -265,7 +275,16 @@ public HookSecretConfig getHookSecretConfig() {
return hookSecretConfig;
}

@DataBoundSetter
public void setHookSecretConfig(HookSecretConfig hookSecretConfig) {
this.hookSecretConfig = hookSecretConfig;
}

private URL parseHookUrl(String hookUrl) {
try {
return new URL(hookUrl);
} catch (MalformedURLException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void migrate() throws IOException {
if (descriptor.getDeprecatedHookUrl() != null) {
LOGGER.warn("Migration for old GitHub Plugin hook url started");
GitHubPlugin.configuration().setOverrideHookUrl(true);
GitHubPlugin.configuration().setHookUrl(descriptor.getDeprecatedHookUrl());
GitHubPlugin.configuration().setHookUrl(descriptor.getDeprecatedHookUrl().toString());
descriptor.clearDeprecatedHookUrl();
descriptor.save();
GitHubPlugin.configuration().save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ f.section(title: descriptor.displayName) {
table(width: "100%", style: "margin-left: 7px;") {
f.optionalBlock(title: _("Specify another hook URL for GitHub configuration"),
inline: true,
field: "overrideHookUrl",
checked: instance.overrideHookURL) {
checked: instance.isOverrideHookUrl) {
Comment thread
er1c marked this conversation as resolved.
f.entry(field: "hookUrl") {
f.textbox(checkMethod: "post")
}
Expand Down
31 changes: 28 additions & 3 deletions src/test/java/com/cloudbees/jenkins/GlobalConfigSubmitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@Ignore("Have troubles with memory consumption")
public class GlobalConfigSubmitTest {

public static final String OVERRIDE_HOOK_URL_CHECKBOX = "_.overrideHookUrl";
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/";
Expand All @@ -33,14 +33,39 @@ public class GlobalConfigSubmitTest {
public JenkinsRule jenkins = new JenkinsRule();

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

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

assertThat(GitHubPlugin.configuration().isOverrideHookURL(), is(true));
assertThat(GitHubPlugin.configuration().getHookUrl(), equalTo(new URL(WEBHOOK_URL)));
}

@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).setValueAttribute("http://foo");
jenkins.submit(form);

assertThat(GitHubPlugin.configuration().getHookUrl(), equalTo(new URL(WEBHOOK_URL)));
}

@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).setValueAttribute("");
jenkins.submit(form);

assertThat(GitHubPlugin.configuration().getHookUrl(), equalTo(new URL(WEBHOOK_URL)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.jenkinsci.plugins.github.config;

import io.jenkins.plugins.casc.ConfigurationAsCode;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.jenkinsci.plugins.github.test.GitHubServerConfigMatcher.withClientCacheSize;
import static org.jenkinsci.plugins.github.test.GitHubServerConfigMatcher.withCredsId;
import static org.jenkinsci.plugins.github.test.GitHubServerConfigMatcher.withApiUrl;
import static org.jenkinsci.plugins.github.test.GitHubServerConfigMatcher.withIsManageHooks;
import static org.jenkinsci.plugins.github.test.GitHubServerConfigMatcher.withName;

public class ConfigAsCodeTest {

@Rule
public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void shouldSupportConfigurationAsCode() throws Exception {

GitHubPluginConfig gitHubPluginConfig = GitHubPluginConfig.all().get(GitHubPluginConfig.class);

/** Test Global Config Properties */

assertThat(
"getHookUrl() is configured",
gitHubPluginConfig.getHookUrl().toString(),
is("http://some.com/github-webhook/secret-path")
);

assertThat(
"getHookSecretConfig().getCredentialsId() is configured",
gitHubPluginConfig.getHookSecretConfig().getCredentialsId(),
is("hook_secret_cred_id")
);

/** Test GitHub Server Configs */

assertThat("configs are loaded", gitHubPluginConfig.getConfigs(), hasSize(2));

assertThat("configs are set", gitHubPluginConfig.getConfigs(), hasItems(
both(withName(is("Public GitHub")))
.and(withApiUrl(is("https://api.github.com")))
.and(withCredsId(is("public_cred_id")))
.and(withClientCacheSize(is(20)))
.and(withIsManageHooks(is(true))),
both(withName(is("Private GitHub")))
.and(withApiUrl(is("https://api.some.com")))
.and(withCredsId(is("private_cred_id")))
.and(withClientCacheSize(is(40)))
.and(withIsManageHooks(is(false)))
));
}

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void export_configuration() throws Exception {
/* TODO (From JCASC): Need to provide some YAML assertion library so that the resulting exported yaml

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

stream can be checked for expected content. */
ConfigurationAsCode.get().export(System.out);

@KostyaSha KostyaSha Jun 16, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

never user System.out, use loggers if you want to print something.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public void shouldNotThrowExcMalformedHookUrlInOldConfig() throws IOException {
assertThat("self hook url", trigger.getDescriptor().getDeprecatedHookUrl(), nullValue());
assertThat("imported hook url", valueOf(trigger.getDescriptor().getHookUrl()),
containsString(Jenkins.getInstance().getRootUrl() + GitHubWebHook.URLNAME));
assertThat("in plugin - override", GitHubPlugin.configuration().isOverrideHookURL(), is(false));
assertThat("in plugin - override", GitHubPlugin.configuration().isOverrideHookUrl(), is(false));
}

@Test
@LocalData
public void shouldMigrateHookUrl() {
assertThat("in plugin - override", GitHubPlugin.configuration().isOverrideHookURL(), is(true));
assertThat("in plugin - override", GitHubPlugin.configuration().isOverrideHookUrl(), is(true));
assertThat("in plugin", valueOf(GitHubPlugin.configuration().getHookUrl()), is(HOOK_FROM_LOCAL_DATA));

assertThat("should nullify hook url after migration",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ protected String featureValueOf(GitHubServerConfig actual) {
};
}

public static Matcher<GitHubServerConfig> withClientCacheSize(Matcher<Integer> matcher) {
return new FeatureMatcher<GitHubServerConfig, Integer>(matcher, "client cache size", "") {
@Override
protected Integer featureValueOf(GitHubServerConfig actual) {
return actual.getClientCacheSize();
}
};
}

public static Matcher<GitHubServerConfig> withCredsId(Matcher<String> matcher) {
return new FeatureMatcher<GitHubServerConfig, String>(matcher, "credentials id", "") {
@Override
protected String featureValueOf(GitHubServerConfig actual) {
return actual.getCredentialsId();
}
};
}

public static Matcher<GitHubServerConfig> withCredsWithToken(String token) {
return new FeatureMatcher<GitHubServerConfig, String>(is(token), "token in creds", "") {
@Override
Expand All @@ -31,4 +49,22 @@ protected String featureValueOf(GitHubServerConfig actual) {
}
};
}

public static Matcher<GitHubServerConfig> withIsManageHooks(Matcher<Boolean> matcher) {
return new FeatureMatcher<GitHubServerConfig, Boolean>(matcher, "is manage hooks", "") {
@Override
protected Boolean featureValueOf(GitHubServerConfig actual) {
return actual.isManageHooks();
}
};
}

public static Matcher<GitHubServerConfig> withName(Matcher<String> matcher) {
return new FeatureMatcher<GitHubServerConfig, String>(matcher, "name", "") {
@Override
protected String featureValueOf(GitHubServerConfig actual) {
return actual.getName();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
unclassified:

githubpluginconfig:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe another more minimal test which shows what you need if you set manage hooks false and no hook url?

hookUrl: "http://some.com/github-webhook/secret-path"
hookSecretConfig:
credentialsId: "hook_secret_cred_id"
configs:
- credentialsId: "public_cred_id"
name: "Public GitHub"
apiUrl: "https://api.github.com"
manageHooks: true
clientCacheSize: 20
- credentialsId: "private_cred_id"
name: "Private GitHub"
apiUrl: "https://api.some.com"
manageHooks: false
clientCacheSize: 40