Skip to content
Permalink
Browse files

JENKINS-34876 Git Parameters not working for Pipeline projects and Je…

…nkinsfile from SCM
  • Loading branch information
klimas7 committed Aug 31, 2016
1 parent 3885d16 commit 5fc8a95ef5d23d704ca4e60ff668671be924ee20
@@ -375,9 +375,9 @@ private FilePathWrapper getTemporaryWorkspace() throws IOException {

private EnvVars getEnvironment(JobWrapper jobImplementationWrapper) throws IOException, InterruptedException {
EnvVars environment = jobImplementationWrapper.getEnvironment(Jenkins.getInstance().toComputer().getNode(), TaskListener.NULL);
if (jobImplementationWrapper.getSomeBuildWithWorkspace() != null) {
EnvVars buildEnvVars = jobImplementationWrapper.getSomeBuildWithWorkspace().getEnvironment(TaskListener.NULL);
environment.putAll(buildEnvVars);
EnvVars buildEnvironments = jobImplementationWrapper.getSomeBuildEnvironments();
if (buildEnvironments != null) {
environment.putAll(buildEnvironments);
}
EnvVars.resolve(environment);
return environment;
@@ -1,12 +1,19 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.jobs;

import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Job;
import hudson.model.TaskListener;
import hudson.scm.SCM;

public class AbstractProjectJobWrapper extends AbstractJobWrapper {
private static final Logger LOGGER = Logger.getLogger(AbstractProjectJobWrapper.class.getName());

public AbstractProjectJobWrapper(Job job) {
super(job);
}
@@ -22,7 +29,15 @@ public FilePath getSomeWorkspace() {
}

@Override
public AbstractBuild getSomeBuildWithWorkspace() {
return ((AbstractProject) getJob()).getSomeBuildWithWorkspace();
public EnvVars getSomeBuildEnvironments() {
try {
AbstractBuild someBuildWithWorkspace = ((AbstractProject) getJob()).getSomeBuildWithWorkspace();
if (someBuildWithWorkspace != null) {
return someBuildWithWorkspace.getEnvironment(TaskListener.NULL);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, Messages.AbstractProjectJobWrapper_GetEnvironmentsFromAbstractProject(), e);
}
return null;
}
}
@@ -4,7 +4,6 @@

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Job;
import hudson.model.Node;
import hudson.model.ParametersDefinitionProperty;
@@ -22,7 +21,7 @@

EnvVars getEnvironment(Node node, TaskListener taskListener) throws IOException, InterruptedException;

AbstractBuild getSomeBuildWithWorkspace();
EnvVars getSomeBuildEnvironments();

int getNextBuildNumber();

@@ -1,17 +1,18 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.jobs;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Job;
import hudson.model.TaskListener;
import hudson.model.TopLevelItem;
import hudson.scm.SCM;
import jenkins.model.Jenkins;
import net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition;

public class WorkflowJobWrapper extends AbstractJobWrapper {
private static final Logger LOGGER = Logger.getLogger(WorkflowJobWrapper.class.getName());
@@ -23,9 +24,7 @@ public WorkflowJobWrapper(Job job) {
@Override
public SCM getScm() {
try {
Class<?> workflowJobClazz = getJob().getClass();
Method getDefinitionMethod = workflowJobClazz.getDeclaredMethod("getDefinition");
Object definition = getDefinitionMethod.invoke(getJob());
Object definition = invokeGetMethodFromJob("getDefinition");

Class<?> cpsScmFlowDefinitionClazz = definition.getClass();
Method getScmMethod = cpsScmFlowDefinitionClazz.getMethod("getScm");
@@ -50,7 +49,27 @@ public FilePath getSomeWorkspace() throws IOException, InterruptedException {
}

@Override
public AbstractBuild getSomeBuildWithWorkspace() {
return null; //TODO Add implementation, perhaps is not necessary
public EnvVars getSomeBuildEnvironments() {
try {
Object lastBuild = invokeGetMethodFromJob("getLastBuild");
if (lastBuild != null) {
Class<?> workflowRunClazz = lastBuild.getClass();

Method getEnvironmentMethod = workflowRunClazz.getMethod("getEnvironment", TaskListener.class);
Object envVars = getEnvironmentMethod.invoke(lastBuild, TaskListener.NULL);
if (envVars instanceof EnvVars) {
return (EnvVars) envVars;
}
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, Messages.WorkflowJobWrapper_GetEnvironmentsFromWorkflowrun(), e);
}
return null;
}

private Object invokeGetMethodFromJob(String methodInvoke) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Class<?> workflowJobClazz = getJob().getClass();
Method getDefinitionMethod = workflowJobClazz.getDeclaredMethod(methodInvoke);
return getDefinitionMethod.invoke(getJob());
}
}
@@ -1,2 +1,4 @@
JobWrapperFactory.UnsupportedJobType=Unsupported job type. Class: {0}
WorkflowJobWrapper.GetWorkflowRepoScmFail=Get repo scm from Workflow job fail
WorkflowJobWrapper.GetWorkflowRepoScmFail=Get repo scm from Workflow job fail
WorkflowJobWrapper.GetEnvironmentsFromWorkflowrun=Get environments from WorkflowRun fail
AbstractProjectJobWrapper.GetEnvironmentsFromAbstractProject=Get environments from AbstractProject fail
@@ -1,2 +1,4 @@
JobWrapperFactory.UnsupportedJobType=Niewspierany typ zadania. Class: {0}
WorkflowJobWrapper.GetWorkflowRepoScmFail=B\u0142\u0105d przy obs\u0142udze repo scm w WorkflowJob
WorkflowJobWrapper.GetWorkflowRepoScmFail=B\u0142\u0105d przy obs\u0142udze repo scm w WorkflowJob
WorkflowJobWrapper.GetEnvironmentsFromWorkflowrun=Pobranie zmiennych z WorkflowRun zako\u0144czone niepowodzeniem
AbstractProjectJobWrapper.GetEnvironmentsFromAbstractProject=Pobranie zmiennych z AbstractProject zako\u0144czone niepowodzeniem

0 comments on commit 5fc8a95

Please sign in to comment.
You can’t perform that action at this time.