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

Remove usages of Guice #72

Merged
merged 3 commits into from
Jan 6, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
import hudson.model.ParametersDefinitionProperty;
import hudson.model.PasswordParameterValue;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import hudson.util.Secret;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import jenkins.model.Jenkins;
Expand All @@ -27,9 +32,11 @@
import org.jenkinsci.plugins.structs.describable.CustomDescribableModel;
import org.jenkinsci.plugins.structs.describable.DescribableModel;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
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.util.StaplerReferer;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
Expand All @@ -39,7 +46,7 @@
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

public class BuildTriggerStep extends AbstractStepImpl {
public class BuildTriggerStep extends Step {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is fine because it was not Serializable to begin with.


private final String job;
private List<ParameterValue> parameters;
Expand Down Expand Up @@ -88,12 +95,13 @@ public boolean isPropagate() {
this.propagate = propagate;
}

@Extension
public static class DescriptorImpl extends AbstractStepDescriptorImpl implements CustomDescribableModel {
@Override
public StepExecution start(StepContext context) throws Exception {
return new BuildTriggerStepExecution(this, context);
}

public DescriptorImpl() {
super(BuildTriggerStepExecution.class);
}
@Extension
public static class DescriptorImpl extends StepDescriptor implements CustomDescribableModel {

// Note: This is necessary because the JSON format of the parameters produced by config.jelly when
// using the snippet generator does not match what would be neccessary for databinding to work automatically.
Expand Down Expand Up @@ -189,6 +197,13 @@ private static <T> Map<String, Object> copyMapReplacingEntry(Map<String, ?> map,
return newMap;
}

@Override
public Set<? extends Class<?>> getRequiredContext() {
Set<Class<?>> context = new HashSet<>();
Collections.addAll(context, FlowNode.class, Run.class, TaskListener.class);
return Collections.unmodifiableSet(context);
}

@Override
public String getFunctionName() {
return "build";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import hudson.AbortException;
import hudson.Util;
import hudson.console.ModelHyperlinkNote;
Expand Down Expand Up @@ -34,7 +33,6 @@
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand All @@ -59,18 +57,18 @@ public class BuildTriggerStepExecution extends AbstractStepExecutionImpl {
"org.biouno.unochoice.ChoiceParameter",
"org.biouno.unochoice.DynamicReferenceParameter");

@StepContextParameter
private transient TaskListener listener;
@StepContextParameter private transient Run<?,?> invokingRun;
@StepContextParameter private transient FlowNode node;
private final transient BuildTriggerStep step;

@Inject(optional=true) transient BuildTriggerStep step;
public BuildTriggerStepExecution(BuildTriggerStep step, @Nonnull StepContext context) {
super(context);
this.step = step;
}

@SuppressWarnings({"unchecked", "rawtypes"}) // cannot get from ParameterizedJob back to ParameterizedJobMixIn trivially
@Override
public boolean start() throws Exception {
String job = step.getJob();
Item item = Jenkins.get().getItem(job, invokingRun.getParent(), Item.class);
Item item = Jenkins.get().getItem(job, getContext().get(Run.class).getParent(), Item.class);
if (item == null) {
throw new AbortException("No item named " + job + " found");
}
Expand All @@ -81,14 +79,14 @@ public boolean start() throws Exception {
}

List<Action> actions = new ArrayList<>();
actions.add(new CauseAction(new BuildUpstreamCause(node, invokingRun)));
actions.add(new BuildUpstreamNodeAction(node, invokingRun));
actions.add(new CauseAction(new BuildUpstreamCause(getContext().get(FlowNode.class), getContext().get(Run.class))));
actions.add(new BuildUpstreamNodeAction(getContext().get(FlowNode.class), getContext().get(Run.class)));

if (item instanceof ParameterizedJobMixIn.ParameterizedJob) {
final ParameterizedJobMixIn.ParameterizedJob project = (ParameterizedJobMixIn.ParameterizedJob) item;
listener.getLogger().println("Scheduling project: " + ModelHyperlinkNote.encodeTo(project));
getContext().get(TaskListener.class).getLogger().println("Scheduling project: " + ModelHyperlinkNote.encodeTo(project));

node.addAction(new LabelAction(Messages.BuildTriggerStepExecution_building_(project.getFullDisplayName())));
getContext().get(FlowNode.class).addAction(new LabelAction(Messages.BuildTriggerStepExecution_building_(project.getFullDisplayName())));

if (step.getWait()) {
StepContext context = getContext();
Expand All @@ -113,8 +111,8 @@ public boolean start() throws Exception {
throw new AbortException("Item type does not support parameters");
}
Queue.Task task = (Queue.Task) item;
listener.getLogger().println("Scheduling item: " + ModelHyperlinkNote.encodeTo(item));
node.addAction(new LabelAction(Messages.BuildTriggerStepExecution_building_(task.getFullDisplayName())));
getContext().get(TaskListener.class).getLogger().println("Scheduling item: " + ModelHyperlinkNote.encodeTo(item));
getContext().get(FlowNode.class).addAction(new LabelAction(Messages.BuildTriggerStepExecution_building_(task.getFullDisplayName())));
if (step.getWait()) {
StepContext context = getContext();
actions.add(new BuildTriggerAction(context, step.isPropagate()));
Expand Down Expand Up @@ -156,7 +154,7 @@ public boolean start() throws Exception {
}
}

private List<ParameterValue> completeDefaultParameters(List<ParameterValue> parameters, Job<?,?> project) throws AbortException {
private List<ParameterValue> completeDefaultParameters(List<ParameterValue> parameters, Job<?,?> project) throws IOException, InterruptedException {
Map<String,ParameterValue> allParameters = new LinkedHashMap<>();
for (ParameterValue pv : parameters) {
allParameters.put(pv.getName(), pv);
Expand Down Expand Up @@ -187,8 +185,8 @@ private List<ParameterValue> completeDefaultParameters(List<ParameterValue> para
// the parameter versus the definition is expected, so we want to do the conversion, but
// not log a warning.
if (!CHOICE_PARAMETER_DEFINITION_LIKE_CLASSES.contains(pDef.getClass().getName())) {
listener.getLogger().println(String.format("The parameter '%s' did not have the type expected by %s. Converting to %s.", pv.getName(), ModelHyperlinkNote.encodeTo(project), pDefDisplayName));
description = Messages.BuildTriggerStepExecution_convertedParameterDescription(description, pDefDisplayName, invokingRun.toString());
getContext().get(TaskListener.class).getLogger().println(String.format("The parameter '%s' did not have the type expected by %s. Converting to %s.", pv.getName(), ModelHyperlinkNote.encodeTo(project), pDefDisplayName));
description = Messages.BuildTriggerStepExecution_convertedParameterDescription(description, pDefDisplayName, getContext().get(Run.class).toString());
}
ParameterValue convertedValue = ((SimpleParameterDefinition) pDef).createValue((String) pv.getValue());
allParameters.put(pDef.getName(), convertedValue);
Expand Down