diff --git a/src/main/java/jenkins/plugins/slack/ActiveNotifier.java b/src/main/java/jenkins/plugins/slack/ActiveNotifier.java index dd1f5034..5f04c8d1 100755 --- a/src/main/java/jenkins/plugins/slack/ActiveNotifier.java +++ b/src/main/java/jenkins/plugins/slack/ActiveNotifier.java @@ -230,7 +230,7 @@ String getCommitList(AbstractBuild r) { } String upProjectName = c.getUpstreamProject(); int buildNumber = c.getUpstreamBuild(); - AbstractProject project = Jenkins.getActiveInstance().getItemByFullName(upProjectName, AbstractProject.class); + AbstractProject project = Jenkins.get().getItemByFullName(upProjectName, AbstractProject.class); if (project != null) { AbstractBuild upBuild = project.getBuildByNumber(buildNumber); return getCommitList(upBuild); diff --git a/src/main/java/jenkins/plugins/slack/SlackNotifier.java b/src/main/java/jenkins/plugins/slack/SlackNotifier.java index 95078813..9a3aa479 100755 --- a/src/main/java/jenkins/plugins/slack/SlackNotifier.java +++ b/src/main/java/jenkins/plugins/slack/SlackNotifier.java @@ -550,7 +550,7 @@ public ListBoxModel doFillCommitInfoChoiceItems() { public ListBoxModel doFillTokenCredentialIdItems(@AncestorInPath Item context) { - Jenkins jenkins = Jenkins.getActiveInstance(); + Jenkins jenkins = Jenkins.get(); if(context == null && !jenkins.hasPermission(Jenkins.ADMINISTER) || context != null && !context.hasPermission(Item.EXTENDED_READ)) { @@ -829,7 +829,7 @@ public void onLoaded() { ItemConfigMigrator migrator = new ItemConfigMigrator(); - Jenkins jenkins = Jenkins.getActiveInstance(); + Jenkins jenkins = Jenkins.get(); List items = jenkins.getAllItems(); if (null != items) { diff --git a/src/main/java/jenkins/plugins/slack/StandardSlackService.java b/src/main/java/jenkins/plugins/slack/StandardSlackService.java index 21da7974..2eaa561e 100755 --- a/src/main/java/jenkins/plugins/slack/StandardSlackService.java +++ b/src/main/java/jenkins/plugins/slack/StandardSlackService.java @@ -195,7 +195,7 @@ private String getTokenToUse() { } private StringCredentials lookupCredentials(String credentialId) { - List credentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(StringCredentials.class, Jenkins.getInstance(), ACL.SYSTEM, Collections.emptyList()); + List credentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(StringCredentials.class, Jenkins.get(), ACL.SYSTEM, Collections.emptyList()); CredentialsMatcher matcher = CredentialsMatchers.withId(credentialId); return CredentialsMatchers.firstOrNull(credentials, matcher); } diff --git a/src/main/java/jenkins/plugins/slack/webhook/GetProjectLogCommand.java b/src/main/java/jenkins/plugins/slack/webhook/GetProjectLogCommand.java index 88a76612..260349eb 100644 --- a/src/main/java/jenkins/plugins/slack/webhook/GetProjectLogCommand.java +++ b/src/main/java/jenkins/plugins/slack/webhook/GetProjectLogCommand.java @@ -25,7 +25,7 @@ public SlackTextMessage execute(String... args) { List log; try (ACLContext ignored = ACL.as(ACL.SYSTEM)) { - Project project = Jenkins.getActiveInstance().getItemByFullName(projectName, Project.class); + Project project = Jenkins.get().getItemByFullName(projectName, Project.class); if (project == null) { return new SlackTextMessage("Could not find project (" + projectName + ")\n"); diff --git a/src/main/java/jenkins/plugins/slack/webhook/ListProjectsCommand.java b/src/main/java/jenkins/plugins/slack/webhook/ListProjectsCommand.java index 0005946d..88a7bb30 100644 --- a/src/main/java/jenkins/plugins/slack/webhook/ListProjectsCommand.java +++ b/src/main/java/jenkins/plugins/slack/webhook/ListProjectsCommand.java @@ -25,7 +25,7 @@ public ListProjectsCommand(SlackPostData data) { public SlackTextMessage execute(String... args) { List jobs; try (ACLContext ignored = ACL.as(ACL.SYSTEM)) { - Jenkins jenkins = Jenkins.getActiveInstance(); + Jenkins jenkins = Jenkins.get(); jobs = jenkins.getAllItems(AbstractProject.class); } diff --git a/src/main/java/jenkins/plugins/slack/webhook/ScheduleJobCommand.java b/src/main/java/jenkins/plugins/slack/webhook/ScheduleJobCommand.java index d9282747..b71b39cf 100644 --- a/src/main/java/jenkins/plugins/slack/webhook/ScheduleJobCommand.java +++ b/src/main/java/jenkins/plugins/slack/webhook/ScheduleJobCommand.java @@ -19,7 +19,7 @@ public SlackTextMessage execute(String... args) { String projectName = args[0]; try (ACLContext ignored = ACL.as(ACL.SYSTEM)) { - Jenkins jenkins = Jenkins.getActiveInstance(); + Jenkins jenkins = Jenkins.get(); Project project = jenkins.getItemByFullName(projectName, Project.class); diff --git a/src/main/java/jenkins/plugins/slack/workflow/SlackSendStep.java b/src/main/java/jenkins/plugins/slack/workflow/SlackSendStep.java index f9b53556..900e5d13 100644 --- a/src/main/java/jenkins/plugins/slack/workflow/SlackSendStep.java +++ b/src/main/java/jenkins/plugins/slack/workflow/SlackSendStep.java @@ -2,11 +2,13 @@ import com.cloudbees.plugins.credentials.common.StandardListBoxModel; import com.cloudbees.plugins.credentials.domains.HostnameRequirement; +import com.google.common.collect.ImmutableSet; import hudson.AbortException; import hudson.Extension; import hudson.Util; import hudson.model.Item; import hudson.model.Project; +import hudson.model.Run; import hudson.model.TaskListener; import hudson.security.ACL; import hudson.util.FormValidation; @@ -22,24 +24,26 @@ import net.sf.json.JSONObject; import net.sf.json.groovy.JsonSlurper; import org.jenkinsci.plugins.plaincredentials.StringCredentials; -import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl; -import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl; -import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution; -import org.jenkinsci.plugins.workflow.steps.StepContextParameter; +import org.jenkinsci.plugins.workflow.steps.Step; +import org.jenkinsci.plugins.workflow.steps.StepContext; +import org.jenkinsci.plugins.workflow.steps.StepDescriptor; +import org.jenkinsci.plugins.workflow.steps.StepExecution; +import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution; import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.QueryParameter; +import java.util.Objects; +import java.util.Set; import javax.annotation.Nonnull; -import javax.inject.Inject; import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials; /** * Workflow step to send a Slack channel notification. */ -public class SlackSendStep extends AbstractStepImpl { +public class SlackSendStep extends Step { private String message; private String color; @@ -161,11 +165,17 @@ public void setReplyBroadcast(boolean replyBroadcast) { public SlackSendStep() { } + @Override + public StepExecution start(StepContext context) { + return new SlackSendStepExecution(this, context); + } + @Extension - public static class DescriptorImpl extends AbstractStepDescriptorImpl { + public static class DescriptorImpl extends StepDescriptor { - public DescriptorImpl() { - super(SlackSendStepExecution.class); + @Override + public Set> getRequiredContext() { + return ImmutableSet.of(Run.class, TaskListener.class); } @Override @@ -181,7 +191,7 @@ public String getDisplayName() { public ListBoxModel doFillTokenCredentialIdItems(@AncestorInPath Project project) { - Jenkins jenkins = Jenkins.getActiveInstance(); + Jenkins jenkins = Jenkins.get(); if (project == null && !jenkins.hasPermission(Jenkins.ADMINISTER) || project != null && !project.hasPermission(Item.EXTENDED_READ)) { @@ -206,20 +216,21 @@ public FormValidation doCheckToken(@QueryParameter String value) { } } - public static class SlackSendStepExecution extends AbstractSynchronousNonBlockingStepExecution { + public static class SlackSendStepExecution extends SynchronousNonBlockingStepExecution { private static final long serialVersionUID = 1L; - @Inject - transient SlackSendStep step; + private transient final SlackSendStep step; - @StepContextParameter - transient TaskListener listener; + SlackSendStepExecution(SlackSendStep step, StepContext context) { + super(context); + this.step = step; + } @Override protected SlackResponse run() throws Exception { - Jenkins jenkins = Jenkins.getActiveInstance(); + Jenkins jenkins = Jenkins.get(); SlackNotifier.DescriptorImpl slackDesc = jenkins.getDescriptorByType(SlackNotifier.DescriptorImpl.class); @@ -232,6 +243,9 @@ protected SlackResponse run() throws Exception { String channel = step.channel != null ? step.channel : slackDesc.getRoom(); String color = step.color != null ? step.color : ""; + TaskListener listener = getContext().get(TaskListener.class); + Objects.requireNonNull(listener, "Listener is mandatory here"); + listener.getLogger().println(Messages.SlackSendStepValues( defaultIfEmpty(baseUrl), defaultIfEmpty(teamDomain), channel, defaultIfEmpty(color), botUser, defaultIfEmpty(tokenCredentialId)) diff --git a/src/test/java/jenkins/plugins/slack/workflow/SlackSendStepTest.java b/src/test/java/jenkins/plugins/slack/workflow/SlackSendStepTest.java index 358964e9..61f9ca0a 100644 --- a/src/test/java/jenkins/plugins/slack/workflow/SlackSendStepTest.java +++ b/src/test/java/jenkins/plugins/slack/workflow/SlackSendStepTest.java @@ -16,11 +16,10 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import java.io.IOException; import java.io.PrintStream; -import java.io.PrintWriter; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.anyBoolean; @@ -32,7 +31,7 @@ import static org.powermock.api.mockito.PowerMockito.spy; /** - * Traditional Unit tests, allows testing null Jenkins.getActiveInstance() + * Traditional Unit tests, allows testing null Jenkins.get() */ @RunWith(PowerMockRunner.class) @PrepareForTest({Jenkins.class,SlackSendStep.class}) @@ -43,8 +42,6 @@ public class SlackSendStepTest { @Mock PrintStream printStreamMock; @Mock - PrintWriter printWriterMock; - @Mock StepContext stepContextMock; @Mock SlackService slackServiceMock; @@ -54,14 +51,15 @@ public class SlackSendStepTest { SlackNotifier.DescriptorImpl slackDescMock; @Before - public void setUp() { + public void setUp() throws IOException, InterruptedException { PowerMockito.mockStatic(Jenkins.class); when(jenkins.getDescriptorByType(SlackNotifier.DescriptorImpl.class)).thenReturn(slackDescMock); + when(taskListenerMock.getLogger()).thenReturn(printStreamMock); + when(stepContextMock.get(TaskListener.class)).thenReturn(taskListenerMock); } @Test public void testStepOverrides() throws Exception { - SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution()); SlackSendStep slackSendStep = new SlackSendStep(); slackSendStep.setMessage("message"); slackSendStep.setToken("token"); @@ -71,11 +69,10 @@ public void testStepOverrides() throws Exception { slackSendStep.setTeamDomain("teamDomain"); slackSendStep.setChannel("channel"); slackSendStep.setColor("good"); - stepExecution.step = slackSendStep; - when(Jenkins.getActiveInstance()).thenReturn(jenkins); + SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution(slackSendStep, stepContextMock)); - stepExecution.listener = taskListenerMock; + when(Jenkins.get()).thenReturn(jenkins); when(slackDescMock.getToken()).thenReturn("differentToken"); when(slackDescMock.isBotUser()).thenReturn(false); @@ -89,27 +86,23 @@ public void testStepOverrides() throws Exception { stepExecution.run(); verify(stepExecution, times(1)).getSlackService("baseUrl/", "teamDomain", "token", "tokenCredentialId", true, "channel", false); verify(slackServiceMock, times(1)).publish("message", "good"); - assertFalse(stepExecution.step.isFailOnError()); } @Test public void testStepWithAttachments() throws Exception { - SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution()); - stepExecution.step = new SlackSendStep(); - stepExecution.step.setMessage("message"); + SlackSendStep step = new SlackSendStep(); + step.setMessage("message"); JSONArray attachments = new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonObject.put("title","Title of the message"); jsonObject.put("author_name","Name of the author"); jsonObject.put("author_icon","Avatar for author"); attachments.add(jsonObject); - stepExecution.step.setAttachments(attachments.toString()); + step.setAttachments(attachments.toString()); + SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution(step, stepContextMock)); ((JSONObject) attachments.get(0)).put("fallback", "message"); - when(Jenkins.getActiveInstance()).thenReturn(jenkins); - - stepExecution.listener = taskListenerMock; - + when(Jenkins.get()).thenReturn(jenkins); when(taskListenerMock.getLogger()).thenReturn(printStreamMock); doNothing().when(printStreamMock).println(); @@ -124,14 +117,11 @@ public void testStepWithAttachments() throws Exception { @Test public void testValuesForGlobalConfig() throws Exception { + SlackSendStep step = new SlackSendStep(); + step.setMessage("message"); - SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution()); - stepExecution.step = new SlackSendStep(); - stepExecution.step.setMessage("message"); - - when(Jenkins.getActiveInstance()).thenReturn(jenkins); - - stepExecution.listener = taskListenerMock; + SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution(step, stepContextMock)); + when(Jenkins.get()).thenReturn(jenkins); when(slackDescMock.getBaseUrl()).thenReturn("globalBaseUrl"); when(slackDescMock.getTeamDomain()).thenReturn("globalTeamDomain"); @@ -148,25 +138,17 @@ public void testValuesForGlobalConfig() throws Exception { stepExecution.run(); verify(stepExecution, times(1)).getSlackService("globalBaseUrl", "globalTeamDomain", "globalToken", "globalTokenCredentialId", false, "globalChannel", false); verify(slackServiceMock, times(1)).publish("message", ""); - assertNull(stepExecution.step.getBaseUrl()); - assertNull(stepExecution.step.getTeamDomain()); - assertNull(stepExecution.step.getToken()); - assertNull(stepExecution.step.getTokenCredentialId()); - assertNull(stepExecution.step.getChannel()); - assertNull(stepExecution.step.getColor()); } @Test public void testReplyBroadcast() throws Exception { + SlackSendStep step = new SlackSendStep(); + step.setMessage("message"); + step.setReplyBroadcast(true); - SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution()); - stepExecution.step = new SlackSendStep(); - stepExecution.step.setMessage("message"); - stepExecution.step.setReplyBroadcast(true); - - when(Jenkins.getActiveInstance()).thenReturn(jenkins); + SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution(step, stepContextMock)); - stepExecution.listener = taskListenerMock; + when(Jenkins.get()).thenReturn(jenkins); when(slackDescMock.getBaseUrl()).thenReturn("globalBaseUrl"); when(slackDescMock.getTeamDomain()).thenReturn("globalTeamDomain"); @@ -187,16 +169,12 @@ public void testReplyBroadcast() throws Exception { @Test public void testNonNullEmptyColor() throws Exception { + SlackSendStep step = new SlackSendStep(); + step.setMessage("message"); + step.setColor(""); - SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution()); - SlackSendStep slackSendStep = new SlackSendStep(); - slackSendStep.setMessage("message"); - slackSendStep.setColor(""); - stepExecution.step = slackSendStep; - - when(Jenkins.getActiveInstance()).thenReturn(jenkins); - - stepExecution.listener = taskListenerMock; + SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution(step, stepContextMock)); + when(Jenkins.get()).thenReturn(jenkins); when(taskListenerMock.getLogger()).thenReturn(printStreamMock); doNothing().when(printStreamMock).println(); @@ -205,27 +183,24 @@ public void testNonNullEmptyColor() throws Exception { stepExecution.run(); verify(slackServiceMock, times(1)).publish("message", ""); - assertNull(stepExecution.step.getColor()); } @Test public void testSlackResponseObject() throws Exception { + SlackSendStep step = new SlackSendStep(); + step.setMessage("message"); + step.setToken("token"); + step.setTokenCredentialId("tokenCredentialId"); + step.setBotUser(true); + step.setBaseUrl("baseUrl/"); + step.setTeamDomain("teamDomain"); + step.setChannel("channel"); + step.setColor("good"); - SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution()); - SlackSendStep slackSendStep = new SlackSendStep(); - slackSendStep.setMessage("message"); - slackSendStep.setToken("token"); - slackSendStep.setTokenCredentialId("tokenCredentialId"); - slackSendStep.setBotUser(true); - slackSendStep.setBaseUrl("baseUrl/"); - slackSendStep.setTeamDomain("teamDomain"); - slackSendStep.setChannel("channel"); - slackSendStep.setColor("good"); - stepExecution.step = slackSendStep; + SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution(step, stepContextMock)); - when(Jenkins.getActiveInstance()).thenReturn(jenkins); + when(Jenkins.get()).thenReturn(jenkins); - stepExecution.listener = taskListenerMock; when(taskListenerMock.getLogger()).thenReturn(printStreamMock); doNothing().when(printStreamMock).println(); @@ -250,22 +225,20 @@ public void testSlackResponseObject() throws Exception { @Test public void testSlackResponseObjectNullNonBotUser() throws Exception { + SlackSendStep step = new SlackSendStep(); + step.setMessage("message"); + step.setToken("token"); + step.setTokenCredentialId("tokenCredentialId"); + step.setBotUser(false); + step.setBaseUrl("baseUrl/"); + step.setTeamDomain("teamDomain"); + step.setChannel("channel"); + step.setColor("good"); - SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution()); - SlackSendStep slackSendStep = new SlackSendStep(); - slackSendStep.setMessage("message"); - slackSendStep.setToken("token"); - slackSendStep.setTokenCredentialId("tokenCredentialId"); - slackSendStep.setBotUser(false); - slackSendStep.setBaseUrl("baseUrl/"); - slackSendStep.setTeamDomain("teamDomain"); - slackSendStep.setChannel("channel"); - slackSendStep.setColor("good"); - stepExecution.step = slackSendStep; + SlackSendStep.SlackSendStepExecution stepExecution = spy(new SlackSendStep.SlackSendStepExecution(step, stepContextMock)); - when(Jenkins.getActiveInstance()).thenReturn(jenkins); + when(Jenkins.get()).thenReturn(jenkins); - stepExecution.listener = taskListenerMock; when(taskListenerMock.getLogger()).thenReturn(printStreamMock); doNothing().when(printStreamMock).println();