Skip to content

Commit b5db64c

Browse files
committed
check hook url for override before checking jenkins default url
+ typo fix in word malformed
1 parent fe0c542 commit b5db64c

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

src/main/java/org/jenkinsci/plugins/github/config/GitHubPluginConfig.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
import static com.google.common.base.Charsets.UTF_8;
3737
import static java.lang.String.format;
38-
import static org.apache.commons.lang3.StringUtils.isEmpty;
38+
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
3939
import static org.jenkinsci.plugins.github.config.GitHubServerConfig.allowedToManageHooks;
4040
import static org.jenkinsci.plugins.github.config.GitHubServerConfig.loginToGithub;
4141
import static org.jenkinsci.plugins.github.util.FluentIterableWrapper.from;
@@ -103,19 +103,15 @@ public void setOverrideHookUrl(boolean overrideHookUrl) {
103103
this.overrideHookUrl = overrideHookUrl;
104104
}
105105

106+
/**
107+
* @return hook url used as endpoint to search and write auto-managed hooks in GH
108+
* @throws GHPluginConfigException if default jenkins url is malformed
109+
*/
106110
public URL getHookUrl() throws GHPluginConfigException {
107-
try {
108-
String jenkinsUrl = Jenkins.getInstance().getRootUrl();
109-
110-
if (isEmpty(jenkinsUrl)) {
111-
throw new GHPluginConfigException(Messages.global_config_url_is_empty());
112-
}
113-
114-
return hookUrl != null
115-
? hookUrl
116-
: new URL(jenkinsUrl + GitHubWebHook.get().getUrlName() + '/');
117-
} catch (MalformedURLException e) {
118-
throw new GHPluginConfigException(Messages.global_config_hook_url_is_mailformed(e.getMessage()));
111+
if (hookUrl != null) {
112+
return hookUrl;
113+
} else {
114+
return constructDefaultUrl();
119115
}
120116
}
121117

@@ -213,4 +209,34 @@ public FormValidation doCheckHookUrl(@QueryParameter String value) {
213209
return FormValidation.error(e, "Failed to test a connection to %s", value);
214210
}
215211
}
212+
213+
/**
214+
* Used by default in {@link #getHookUrl()}
215+
*
216+
* @return url to be used in GH hooks configuration as main endpoint
217+
* @throws GHPluginConfigException if jenkins root url empty of malformed
218+
*/
219+
private URL constructDefaultUrl() {
220+
String jenkinsUrl = Jenkins.getInstance().getRootUrl();
221+
validateConfig(isNotEmpty(jenkinsUrl), Messages.global_config_url_is_empty());
222+
try {
223+
return new URL(jenkinsUrl + GitHubWebHook.get().getUrlName() + '/');
224+
} catch (MalformedURLException e) {
225+
throw new GHPluginConfigException(Messages.global_config_hook_url_is_malformed(e.getMessage()));
226+
}
227+
}
228+
229+
/**
230+
* Util method just to hide one more if for better readability
231+
*
232+
* @param state to check. If false, then exception will be thrown
233+
* @param message message to describe exception in case of false state
234+
*
235+
* @throws GHPluginConfigException if state is false
236+
*/
237+
private void validateConfig(boolean state, String message) {
238+
if (!state) {
239+
throw new GHPluginConfigException(message);
240+
}
241+
}
216242
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
global.config.url.is.empty=Jenkins URL is empty. Set explicitly Jenkins URL in global configuration or in GitHub plugin configuration to manage hooks.
2-
global.config.hook.url.is.mailformed=Mailformed GH hook url in global configuration ({0})
2+
global.config.hook.url.is.malformed=Malformed GH hook url in global configuration ({0}). Please check Jenkins URL is valid and ends with slash or use overrided hook url

0 commit comments

Comments
 (0)