|
35 | 35 |
|
36 | 36 | import static com.google.common.base.Charsets.UTF_8;
|
37 | 37 | import static java.lang.String.format;
|
38 |
| -import static org.apache.commons.lang3.StringUtils.isEmpty; |
| 38 | +import static org.apache.commons.lang3.StringUtils.isNotEmpty; |
39 | 39 | import static org.jenkinsci.plugins.github.config.GitHubServerConfig.allowedToManageHooks;
|
40 | 40 | import static org.jenkinsci.plugins.github.config.GitHubServerConfig.loginToGithub;
|
41 | 41 | import static org.jenkinsci.plugins.github.util.FluentIterableWrapper.from;
|
@@ -103,19 +103,15 @@ public void setOverrideHookUrl(boolean overrideHookUrl) {
|
103 | 103 | this.overrideHookUrl = overrideHookUrl;
|
104 | 104 | }
|
105 | 105 |
|
| 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 | + */ |
106 | 110 | 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(); |
119 | 115 | }
|
120 | 116 | }
|
121 | 117 |
|
@@ -213,4 +209,34 @@ public FormValidation doCheckHookUrl(@QueryParameter String value) {
|
213 | 209 | return FormValidation.error(e, "Failed to test a connection to %s", value);
|
214 | 210 | }
|
215 | 211 | }
|
| 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 | + } |
216 | 242 | }
|
0 commit comments