Skip to content

Commit

Permalink
Global settings fix (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-j-nilsson authored Feb 26, 2021
1 parent eae1994 commit 3e07bdc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
32 changes: 30 additions & 2 deletions src/main/java/jenkins/plugins/office365connector/Webhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ 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() {
return url;
}

public String getName() {
return Util.fixEmptyAndTrim(StringUtils.isEmpty(name) ? getDescriptor().getName() : name);
return Util.fixEmptyAndTrim(StringUtils.isEmpty(name) ? getDescriptor().getGlobalName() : name);
}

@DataBoundSetter
Expand Down Expand Up @@ -180,6 +180,8 @@ public void setFactDefinitions(List<FactDefinition> factDefinitions) {
public static class DescriptorImpl extends Descriptor<Webhook> {
private String url;
private String name;
private String globalUrl;
private String globalName;

public DescriptorImpl() {
load();
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<j:jelly xmlns:j="jelly:core"
xmlns:f="/lib/form">
<f:entry title="URL" field="url">
<f:textbox default="${descriptor.url}"/>
<f:textbox default="${descriptor.globalUrl}"/>
</f:entry>
<f:entry title="Name" field="name">
<f:textbox default="${descriptor.name}"/>
<f:textbox default="${descriptor.globalName}"/>
</f:entry>

<f:advanced>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:section title="Office 365 Connector">
<f:entry title="URL" field="url">
<f:entry title="URL" field="globalUrl">
<f:textbox/>
</f:entry>
<f:entry title="Name" field="name">
<f:entry title="Name" field="globalName">
<f:textbox/>
</f:entry>
</f:section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("");

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e07bdc

Please sign in to comment.