From 3e07bdc8c6835b77c387b8040edb8c3d5b3b6d97 Mon Sep 17 00:00:00 2001 From: Johan Nilsson Date: Fri, 26 Feb 2021 13:04:17 +0100 Subject: [PATCH] Global settings fix (#242) --- .../plugins/office365connector/Webhook.java | 32 +++++++++++++++++-- .../office365connector/Webhook/config.jelly | 4 +-- .../office365connector/Webhook/global.jelly | 4 +-- .../WebhookDescriptorImplTest.java | 26 +++++++++++++++ .../office365connector/WebhookTest.java | 4 +-- 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/main/java/jenkins/plugins/office365connector/Webhook.java b/src/main/java/jenkins/plugins/office365connector/Webhook.java index 0eb8922..e26b686 100644 --- a/src/main/java/jenkins/plugins/office365connector/Webhook.java +++ b/src/main/java/jenkins/plugins/office365connector/Webhook.java @@ -61,7 +61,7 @@ public DescriptorImpl getDescriptor() { @DataBoundConstructor public Webhook(String url) { - this.url = StringUtils.isEmpty(url) ? getDescriptor().getUrl() : url; + this.url = StringUtils.isEmpty(url) ? getDescriptor().getGlobalUrl() : url; } public String getUrl() { @@ -69,7 +69,7 @@ public String getUrl() { } public String getName() { - return Util.fixEmptyAndTrim(StringUtils.isEmpty(name) ? getDescriptor().getName() : name); + return Util.fixEmptyAndTrim(StringUtils.isEmpty(name) ? getDescriptor().getGlobalName() : name); } @DataBoundSetter @@ -180,6 +180,8 @@ public void setFactDefinitions(List factDefinitions) { public static class DescriptorImpl extends Descriptor { private String url; private String name; + private String globalUrl; + private String globalName; public DescriptorImpl() { load(); @@ -199,6 +201,14 @@ public FormValidation doCheckUrl(@QueryParameter String value) { return FormUtils.formValidateUrl(value); } + public FormValidation doCheckGlobalUrl(@QueryParameter String value) { + if(StringUtils.isNotBlank(value)) { + return FormUtils.formValidateUrl(value); + } else { + return FormValidation.ok(); + } + } + public String getUrl() { return url; } @@ -217,6 +227,24 @@ public void setName(String name) { this.name = name; } + public String getGlobalUrl() { + return globalUrl; + } + + @DataBoundSetter + public void setGlobalUrl(String url) { + this.globalUrl = url; + } + + public String getGlobalName() { + return globalName; + } + + @DataBoundSetter + public void setGlobalName(String name) { + this.globalName = name; + } + @Override public boolean configure(StaplerRequest req, JSONObject formData) { req.bindJSON(this, formData); diff --git a/src/main/resources/jenkins/plugins/office365connector/Webhook/config.jelly b/src/main/resources/jenkins/plugins/office365connector/Webhook/config.jelly index b91292a..6917e9a 100644 --- a/src/main/resources/jenkins/plugins/office365connector/Webhook/config.jelly +++ b/src/main/resources/jenkins/plugins/office365connector/Webhook/config.jelly @@ -2,10 +2,10 @@ - + - + diff --git a/src/main/resources/jenkins/plugins/office365connector/Webhook/global.jelly b/src/main/resources/jenkins/plugins/office365connector/Webhook/global.jelly index 927f09c..09b3ecb 100755 --- a/src/main/resources/jenkins/plugins/office365connector/Webhook/global.jelly +++ b/src/main/resources/jenkins/plugins/office365connector/Webhook/global.jelly @@ -1,10 +1,10 @@ - + - + diff --git a/src/test/java/jenkins/plugins/office365connector/WebhookDescriptorImplTest.java b/src/test/java/jenkins/plugins/office365connector/WebhookDescriptorImplTest.java index 67bdd98..a2b3168 100644 --- a/src/test/java/jenkins/plugins/office365connector/WebhookDescriptorImplTest.java +++ b/src/test/java/jenkins/plugins/office365connector/WebhookDescriptorImplTest.java @@ -108,4 +108,30 @@ public void configure_ReturnsTrue() { // then assertThat(isConfigured).isTrue(); } + + @Test + public void doCheckGlobalUrl_ValidatesUrl() { + + // given + String validUrl = "http://myJenkins.abc"; + + // when + FormValidation result = descriptor.doCheckGlobalUrl(validUrl); + + // then + assertThat(result).isEqualTo(FormValidation.ok()); + } + + @Test + public void doCheckGlobalUrl_ValidatesUrl_WhenBlank() { + + // given + String validUrl = ""; + + // when + FormValidation result = descriptor.doCheckGlobalUrl(validUrl); + + // then + assertThat(result).isEqualTo(FormValidation.ok()); + } } diff --git a/src/test/java/jenkins/plugins/office365connector/WebhookTest.java b/src/test/java/jenkins/plugins/office365connector/WebhookTest.java index c12555e..9bdd45d 100644 --- a/src/test/java/jenkins/plugins/office365connector/WebhookTest.java +++ b/src/test/java/jenkins/plugins/office365connector/WebhookTest.java @@ -47,7 +47,7 @@ public void getUrl_WithEmptyLocalUrlReturnsGlobalUrl() { Jenkins jenkins = mock(Jenkins.class); Webhook.DescriptorImpl mockDescriptor = mock(Webhook.DescriptorImpl.class); when(Jenkins.get()).thenReturn(jenkins); - when(mockDescriptor.getUrl()).thenReturn(globalUrl); + when(mockDescriptor.getGlobalUrl()).thenReturn(globalUrl); when(jenkins.getDescriptorOrDie(Webhook.class)).thenReturn(mockDescriptor); Webhook webhook = new Webhook(""); @@ -104,7 +104,7 @@ public void getName_WithEmptyLocalNameReturnsGlobalName() { Jenkins jenkins = mock(Jenkins.class); Webhook.DescriptorImpl mockDescriptor = mock(Webhook.DescriptorImpl.class); when(Jenkins.get()).thenReturn(jenkins); - when(mockDescriptor.getName()).thenReturn(globalName); + when(mockDescriptor.getGlobalName()).thenReturn(globalName); when(jenkins.getDescriptorOrDie(Webhook.class)).thenReturn(mockDescriptor); // when