Skip to content

Commit

Permalink
Global settings (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-j-nilsson authored Feb 23, 2021
1 parent 3825eca commit eae1994
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 43 deletions.
Binary file added .README/globalconfig.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .README/globalconfigdefault.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Plugin is used to send actionable messages in [Outlook](http://outlook.com), [Of
### Jenkins configuration
![Configuration](https://github.com/jenkinsci/office-365-connector-plugin/raw/master/.README/config.png)

### Jenkins global configuration
![GlobalConfiguration](.README/globalconfig.png?raw=true)

#### Global configuration values used as default in jobs
![GlobalConfigurationDefault](.README/globalconfigdefault.png?raw=true)

### Microsoft Teams
#### With Jenkins plugin
![Teams](https://github.com/jenkinsci/office-365-connector-plugin/raw/master/.README/teams.png)
Expand All @@ -30,7 +36,11 @@ Plugin is used to send actionable messages in [Outlook](http://outlook.com), [Of

1. Install this plugin on your Jenkins server
2. Configure it in your Jenkins job and add webhook URL obtained from office 365 connector.


The plugin can also be configured through global settings under Jenkins -> Manage Jenkins -> Configure System.
The values in the global settings will be used as default when using the plugin for a job.
These settings can then be overriden in the job. Changing the values in the global settings will however not
update any settings in an existing job.
## Examples

### DSL
Expand Down
47 changes: 42 additions & 5 deletions src/main/java/jenkins/plugins/office365connector/Webhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@

import java.util.Collections;
import java.util.List;

import javax.annotation.Nonnull;
import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.util.FormValidation;
import javax.annotation.Nonnull;
import jenkins.plugins.office365connector.model.FactDefinition;
import jenkins.plugins.office365connector.model.Macro;
import jenkins.plugins.office365connector.utils.FormUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

public class Webhook extends AbstractDescribableImpl<Webhook> {

Expand All @@ -52,18 +54,22 @@ public class Webhook extends AbstractDescribableImpl<Webhook> {

private List<FactDefinition> factDefinitions = Collections.emptyList();

@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}

@DataBoundConstructor
public Webhook(String url) {
this.url = url;
this.url = StringUtils.isEmpty(url) ? getDescriptor().getUrl() : url;
}

public String getUrl() {
return url;
}

public String getName() {
// when the job is created and the name is not provided, then getName() returns null
return Util.fixEmptyAndTrim(name);
return Util.fixEmptyAndTrim(StringUtils.isEmpty(name) ? getDescriptor().getName() : name);
}

@DataBoundSetter
Expand Down Expand Up @@ -172,6 +178,12 @@ public void setFactDefinitions(List<FactDefinition> factDefinitions) {

@Extension
public static class DescriptorImpl extends Descriptor<Webhook> {
private String url;
private String name;

public DescriptorImpl() {
load();
}

@Nonnull
@Override
Expand All @@ -186,5 +198,30 @@ public int getDefaultTimeout() {
public FormValidation doCheckUrl(@QueryParameter String value) {
return FormUtils.formValidateUrl(value);
}

public String getUrl() {
return url;
}

@DataBoundSetter
public void setUrl(String url) {
this.url = url;
}

public String getName() {
return name;
}

@DataBoundSetter
public void setName(String name) {
this.name = name;
}

@Override
public boolean configure(StaplerRequest req, JSONObject formData) {
req.bindJSON(this, formData);
save();
return true;
}
}
}
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/>
<f:textbox default="${descriptor.url}"/>
</f:entry>
<f:entry title="Name" field="name">
<f:textbox/>
<f:textbox default="${descriptor.name}"/>
</f:entry>

<f:advanced>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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:textbox/>
</f:entry>
<f:entry title="Name" field="name">
<f:textbox/>
</f:entry>
</f:section>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
package jenkins.plugins.office365connector;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import hudson.FilePath;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import jenkins.plugins.office365connector.model.Macro;
import jenkins.plugins.office365connector.workflow.AbstractTest;
import mockit.Deencapsulation;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyObject;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

@PowerMockIgnore("jdk.internal.reflect.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest({TokenMacro.class, FilePath.class})
@PrepareForTest({TokenMacro.class, FilePath.class, Jenkins.class})
public class DecisionMakerTest extends AbstractTest {

@Before
public void setup() throws Exception {
Webhook.DescriptorImpl mockDescriptor = mock(Webhook.DescriptorImpl.class);
when(mockDescriptor.getName()).thenReturn("test");

Jenkins mockJenkins = mock(Jenkins.class);
mockStatic(Jenkins.class);
Mockito.when(Jenkins.get()).thenReturn(mockJenkins);
Mockito.when(mockJenkins.getDescriptorOrDie(anyObject())).thenReturn(mockDescriptor);
}

@Test
public void DecisionMaker_OnEmptyPreviousBuild_StoresParameters() {

Expand Down Expand Up @@ -553,7 +567,7 @@ public void evaluateMacro_OnInvalidMacro_ThrowsException() {
mockStatic(FilePath.class);

try {
when(TokenMacro.expandAll(Matchers.any(), Matchers.any(), Matchers.any(), Matchers.any()))
when(TokenMacro.expandAll(any(), any(), any(), any()))
.thenThrow(new MacroEvaluationException("ups!"));
} catch (MacroEvaluationException | IOException | InterruptedException e) {
throw new IllegalArgumentException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
package jenkins.plugins.office365connector;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kohsuke.stapler.StaplerRequest;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

/**
* @author Damian Szczepanik (damianszczepanik@github)
*/
@PowerMockIgnore("jdk.internal.reflect.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest(Jenkins.class)
public class WebhookDescriptorImplTest {

private final Webhook.DescriptorImpl descriptor = new Webhook.DescriptorImpl();
private final WebhookStub.DescriptorImplStub descriptor = new WebhookStub.DescriptorImplStub();

@Before
public void setUp() {
mockStatic(Jenkins.class);
Jenkins jenkins = mock(Jenkins.class);
File rootDir = new File(".");
when(jenkins.getRootDir()).thenReturn(rootDir);
when(Jenkins.get()).thenReturn(jenkins);
}

@Test
public void getDisplayName_ReturnsName() {
Expand Down Expand Up @@ -44,4 +68,44 @@ public void doCheckUrl_ValidatesUrl() {
// then
assertThat(result).isEqualTo(FormValidation.ok());
}

@Test
public void getName_ReturnsName() {

// given
String name = "test";

// when
descriptor.setName(name);

// then
assertThat(descriptor.getName()).isEqualTo(name);
}

@Test
public void getUrl_ReturnsUrl() {

// given
String url = "test.com";

// when
descriptor.setUrl(url);

// then
assertThat(descriptor.getUrl()).isEqualTo(url);
}

@Test
public void configure_ReturnsTrue() {

// given
StaplerRequest staplerRequest = mock(StaplerRequest.class);
when(staplerRequest.bindJSON(any(), any())).thenReturn("");

// when
boolean isConfigured = descriptor.configure(staplerRequest, null);

// then
assertThat(isConfigured).isTrue();
}
}
16 changes: 16 additions & 0 deletions src/test/java/jenkins/plugins/office365connector/WebhookStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jenkins.plugins.office365connector;


public class WebhookStub extends Webhook {

public WebhookStub(String url) {
super(url);
}

public static class DescriptorImplStub extends Webhook.DescriptorImpl {

@Override
public synchronized void load() {
}
}
}
Loading

0 comments on commit eae1994

Please sign in to comment.