Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup deprecation #491

Merged
merged 4 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/jenkins/plugins/slack/ActiveNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/jenkins/plugins/slack/SlackNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -829,7 +829,7 @@ public void onLoaded() {

ItemConfigMigrator migrator = new ItemConfigMigrator();

Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.get();

List<Item> items = jenkins.getAllItems();
if (null != items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private String getTokenToUse() {
}

private StringCredentials lookupCredentials(String credentialId) {
List<StringCredentials> credentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(StringCredentials.class, Jenkins.getInstance(), ACL.SYSTEM, Collections.emptyList());
List<StringCredentials> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SlackTextMessage execute(String... args) {
List<String> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ListProjectsCommand(SlackPostData data) {
public SlackTextMessage execute(String... args) {
List<AbstractProject> jobs;
try (ACLContext ignored = ACL.as(ACL.SYSTEM)) {
Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.get();

jobs = jenkins.getAllItems(AbstractProject.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
46 changes: 30 additions & 16 deletions src/main/java/jenkins/plugins/slack/workflow/SlackSendStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(Run.class, TaskListener.class);
}

@Override
Expand All @@ -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)) {
Expand All @@ -206,20 +216,21 @@ public FormValidation doCheckToken(@QueryParameter String value) {
}
}

public static class SlackSendStepExecution extends AbstractSynchronousNonBlockingStepExecution<SlackResponse> {
public static class SlackSendStepExecution extends SynchronousNonBlockingStepExecution<SlackResponse> {

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);

Expand All @@ -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))
Expand Down
Loading