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

chore: remove reflection #101

Merged
merged 3 commits into from
Feb 13, 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
31 changes: 15 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,26 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
</dependency>

<!-- Test Deps -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<optional>true</optional>
</dependency>

<!-- Test Deps -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -121,11 +131,6 @@
<artifactId>workflow-cps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
Expand All @@ -136,12 +141,6 @@
<artifactId>workflow-basic-steps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;


import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;

/**
* Token that expands variables from the build environment.
Expand Down Expand Up @@ -51,31 +56,16 @@ public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, Strin
@SuppressFBWarnings("REC_CATCH_EXCEPTION")
private String getEnvVarFromWorkflowRun(Run<?,?> run) {
try {
Class<?> workflowRunClass = run.getClass();

if(workflowRunClass.getName().contains("WorkflowRun")) {
// get the FlowExecution object for this run
Method getExecution = workflowRunClass.getMethod("getExecution");
Object execution = getExecution.invoke(run);

// get a list of executions (as a future)
Method current = execution.getClass().getMethod("getCurrentExecutions", boolean.class);
Object currentExecutionsFuture = current.invoke(execution, true);
// get the actual executions list
Method get = currentExecutionsFuture.getClass().getMethod("get");
Object actualExecutions = get.invoke(currentExecutionsFuture);

// retrieve the first execution, this should be the TokenMacroStep$Execution instance
Method getItem = List.class.getMethod("get", int.class);
Object tokenMacroStepExecution = getItem.invoke(actualExecutions, 0);
Method getContext = tokenMacroStepExecution.getClass().getMethod("getContext");
Object context = getContext.invoke(tokenMacroStepExecution);

// now we can get the EnvVars context item
Method contextGet = context.getClass().getMethod("get", Class.class);
Map<String, String> envVars = (Map<String,String>)contextGet.invoke(context, EnvVars.class);
if(envVars != null && envVars.containsKey(var)) {
return envVars.get(var);
WorkflowRun workflowRun = (WorkflowRun) run;

FlowExecution execution = workflowRun.getExecution();
if(execution != null) {
List<StepExecution> actualExecutions = execution.getCurrentExecutions(true).get();

StepContext context = actualExecutions.get(0).getContext();
Map<String, String> vars = context.get(EnvVars.class);
if(vars != null && vars.containsKey(var)) {
return vars.get(var);
}
}
} catch(Exception e) {
Expand All @@ -88,7 +78,11 @@ private String getEnvVarFromWorkflowRun(Run<?,?> run) {
public String evaluate(Run<?, ?> run, FilePath workspace, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {

String res = getEnvVarFromWorkflowRun(run);
String res = "";
if (Jenkins.get().getPlugin("workflow-job") != null) {
res = getEnvVarFromWorkflowRun(run);
}

if(StringUtils.isBlank(res)) {
Map<String, String> env = run.getEnvironment(listener);
if (env.containsKey(var)) {
Expand Down