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 28, 2016
1 parent f047198 commit 3885d16c08943acd696a1c618d78687c4d63a0cb
12 pom.xml
@@ -99,6 +99,18 @@
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>1.15</version>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
@@ -9,7 +9,13 @@
<Match>
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
<Class name="net.uaznia.lukanus.hudson.plugins.gitparameter.GitParameterDefinition"/>
<Method name="getParentProject"/>
<Method name="getParentJob"/>
</Match>
<!-- Jenkins.getInstance() always returns a non-null. -->
<Match>
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
<Class name="net.uaznia.lukanus.hudson.plugins.gitparameter.jobs.WorkflowJobWrapper"/>
<Method name="getSomeWorkspace"/>
</Match>

<Match>
@@ -21,7 +21,7 @@
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.AbstractProject;
import hudson.model.Job;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
@@ -37,6 +37,8 @@
import jenkins.model.Jenkins;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uaznia.lukanus.hudson.plugins.gitparameter.jobs.JobWrapper;
import net.uaznia.lukanus.hudson.plugins.gitparameter.jobs.JobWrapperFactory;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
@@ -132,7 +134,7 @@ public ParameterValue getDefaultParameterValue() {
switch (getSelectedValue()) {
case TOP:
try {
ListBoxModel valueItems = getDescriptor().doFillValueItems(getParentProject(), getName());
ListBoxModel valueItems = getDescriptor().doFillValueItems(getParentJob(), getName());
if (valueItems.size() > 0) {
return new GitParameterValue(getName(), valueItems.get(0).value);
}
@@ -221,22 +223,22 @@ public Boolean getQuickFilterEnabled() {
return quickFilterEnabled;
}

public AbstractProject<?, ?> getParentProject() {
AbstractProject<?, ?> context = null;
List<AbstractProject> jobs = Jenkins.getInstance().getAllItems(AbstractProject.class);
public Job getParentJob() {
Job context = null;
List<Job> jobs = Jenkins.getInstance().getAllItems(Job.class);

for (AbstractProject<?, ?> project : jobs) {
if (!(project instanceof TopLevelItem)) continue;
for (Job job : jobs) {
if (!(job instanceof TopLevelItem)) continue;

ParametersDefinitionProperty property = project.getProperty(ParametersDefinitionProperty.class);
ParametersDefinitionProperty property = (ParametersDefinitionProperty) job.getProperty(ParametersDefinitionProperty.class);

if (property != null) {
List<ParameterDefinition> parameterDefinitions = property.getParameterDefinitions();

if (parameterDefinitions != null) {
for (ParameterDefinition pd : parameterDefinitions) {
if (pd instanceof GitParameterDefinition && ((GitParameterDefinition) pd).compareTo(this) == 0) {
context = project;
context = job;
break;
}
}
@@ -251,16 +253,16 @@ public int compareTo(GitParameterDefinition pd) {
return pd.uuid.equals(uuid) ? 0 : -1;
}

public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSCM git) {
public Map<String, String> generateContents(JobWrapper IJobWrapper, GitSCM git) {

Map<String, String> paramList = new LinkedHashMap<String, String>();
try {
EnvVars environment = getEnvironment(project);
EnvVars environment = getEnvironment(IJobWrapper);
for (RemoteConfig repository : git.getRepositories()) {
boolean isRepoScm = REPO_SCM_NAME.equals(repository.getName());
synchronized (GitParameterDefinition.class) {
FilePathWrapper workspace = getWorkspace(project, isRepoScm);
GitClient gitClient = getGitClient(project, workspace, git, environment);
FilePathWrapper workspace = getWorkspace(IJobWrapper, isRepoScm);
GitClient gitClient = getGitClient(IJobWrapper, workspace, git, environment);
for (URIish remoteURL : repository.getURIs()) {
initWorkspace(workspace, gitClient, remoteURL);

@@ -346,8 +348,8 @@ private void sortAndPutToParam(Set<String> setElement, Map<String, String> param
return sorted;
}

private FilePathWrapper getWorkspace(AbstractProject<?, ?> project, boolean isRepoScm) throws IOException, InterruptedException {
FilePathWrapper someWorkspace = new FilePathWrapper(project.getSomeWorkspace());
private FilePathWrapper getWorkspace(JobWrapper jobImplementationWrapper, boolean isRepoScm) throws IOException, InterruptedException {
FilePathWrapper someWorkspace = new FilePathWrapper(jobImplementationWrapper.getSomeWorkspace());
if (isRepoScm) {
FilePath repoDir = new FilePath(someWorkspace.getFilePath(), REPO_MANIFESTS_DIR);
if (repoDir.exists()) {
@@ -371,10 +373,10 @@ private FilePathWrapper getTemporaryWorkspace() throws IOException {
return filePathWrapper;
}

private EnvVars getEnvironment(AbstractProject<?, ?> project) throws IOException, InterruptedException {
EnvVars environment = project.getEnvironment(Jenkins.getInstance().toComputer().getNode(), TaskListener.NULL);
if (project.getSomeBuildWithWorkspace() != null) {
EnvVars buildEnvVars = project.getSomeBuildWithWorkspace().getEnvironment(TaskListener.NULL);
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.resolve(environment);
@@ -393,13 +395,13 @@ private boolean isEmptyWorkspace(FilePath workspaceDir) throws IOException, Inte
return workspaceDir.list().size() == 0;
}

private GitClient getGitClient(final AbstractProject<?, ?> project, FilePathWrapper workspace, GitSCM git, EnvVars environment) throws IOException, InterruptedException {
int nextBuildNumber = project.getNextBuildNumber();
private GitClient getGitClient(final JobWrapper jobImplementationWrapper, FilePathWrapper workspace, GitSCM git, EnvVars environment) throws IOException, InterruptedException {
int nextBuildNumber = jobImplementationWrapper.getNextBuildNumber();

GitClient gitClient = git.createClient(TaskListener.NULL, environment, new Run(project) {
GitClient gitClient = git.createClient(TaskListener.NULL, environment, new Run(jobImplementationWrapper.getJob()) {
}, workspace.getFilePath());

project.updateNextBuildNumber(nextBuildNumber);
jobImplementationWrapper.updateNextBuildNumber(nextBuildNumber);
return gitClient;
}

@@ -436,21 +438,24 @@ public String getDisplayName() {
return Messages.GitParameterDefinition_DisplayName();
}

public ListBoxModel doFillValueItems(@AncestorInPath AbstractProject<?, ?> project, @QueryParameter String param)
public ListBoxModel doFillValueItems(@AncestorInPath Job job, @QueryParameter String param)
throws IOException, InterruptedException {
ListBoxModel items = new ListBoxModel();
JobWrapper IJobWrapper = JobWrapperFactory.createJobWrapper(job);

scm = getProjectSCM(project);
if (scm == null) {
items.add(Messages.GitParameterDefinition_noRepositoryConfigured());
return items;
}
ParametersDefinitionProperty prop = project.getProperty(ParametersDefinitionProperty.class);
ParametersDefinitionProperty prop = IJobWrapper.getProperty(ParametersDefinitionProperty.class);
if (prop != null) {
ParameterDefinition def = prop.getParameterDefinition(param);
if (def instanceof GitParameterDefinition) {
GitParameterDefinition paramDef = (GitParameterDefinition) def;
Map<String, String> paramList = paramDef.generateContents(project, scm);

scm = getProjectSCM(IJobWrapper);
if (scm == null) {
items.add(Messages.GitParameterDefinition_noRepositoryConfigured());
return items;
}

Map<String, String> paramList = paramDef.generateContents(IJobWrapper, scm);

for (Map.Entry<String, String> entry : paramList.entrySet()) {
items.add(entry.getValue(), entry.getKey());
@@ -460,7 +465,7 @@ public ListBoxModel doFillValueItems(@AncestorInPath AbstractProject<?, ?> proje
return items;
}

public GitSCM getProjectSCM(AbstractProject<?, ?> project) {
public GitSCM getProjectSCM(JobWrapper project) {
SCM projectScm = null;
if (project != null) {
projectScm = project.getScm();
@@ -0,0 +1,43 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.jobs;


import java.io.IOException;

import hudson.EnvVars;
import hudson.model.Job;
import hudson.model.Node;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.TaskListener;

public abstract class AbstractJobWrapper implements JobWrapper {
private Job job;

public AbstractJobWrapper(Job job) {
this.job = job;
}

@Override
public Job getJob() {
return job;
}

@Override
public ParametersDefinitionProperty getProperty(Class<ParametersDefinitionProperty> propertyClass) {
return (ParametersDefinitionProperty) job.getProperty(propertyClass);
}

@Override
public EnvVars getEnvironment(Node node, TaskListener taskListener) throws IOException, InterruptedException {
return job.getEnvironment(node, taskListener);
}

@Override
public int getNextBuildNumber() {
return job.getNextBuildNumber();
}

@Override
public void updateNextBuildNumber(int nextBuildNumber) throws IOException {
job.updateNextBuildNumber(nextBuildNumber);
}
}
@@ -0,0 +1,28 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.jobs;

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

public class AbstractProjectJobWrapper extends AbstractJobWrapper {
public AbstractProjectJobWrapper(Job job) {
super(job);
}

@Override
public SCM getScm() {
return ((AbstractProject) getJob()).getScm();
}

@Override
public FilePath getSomeWorkspace() {
return ((AbstractProject) getJob()).getSomeWorkspace();
}

@Override
public AbstractBuild getSomeBuildWithWorkspace() {
return ((AbstractProject) getJob()).getSomeBuildWithWorkspace();
}
}
@@ -0,0 +1,30 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.jobs;

import java.io.IOException;

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

public interface JobWrapper {
Job getJob();

ParametersDefinitionProperty getProperty(Class<ParametersDefinitionProperty> parametersDefinitionPropertyClass);

SCM getScm();

FilePath getSomeWorkspace() throws IOException, InterruptedException;

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

AbstractBuild getSomeBuildWithWorkspace();

int getNextBuildNumber();

void updateNextBuildNumber(int nextBuildNumber) throws IOException;
}
@@ -0,0 +1,21 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.jobs;

import hudson.model.AbstractProject;
import hudson.model.Job;

public class JobWrapperFactory {
private static final String WORKFLOW_JOB_CLASS_NAME = "org.jenkinsci.plugins.workflow.job.WorkflowJob";

public static JobWrapper createJobWrapper(Job job) {
if (job instanceof AbstractProject) {
return new AbstractProjectJobWrapper(job);
} else if (isWorkflowJob(job)) {
return new WorkflowJobWrapper(job);
}
throw new UnsupportedJobType(Messages.JobWrapperFactory_UnsupportedJobType(job.getClass().getName()));
}

private static boolean isWorkflowJob(Job job) {
return job != null && WORKFLOW_JOB_CLASS_NAME.equals(job.getClass().getName());
}
}
@@ -0,0 +1,7 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.jobs;

public class UnsupportedJobType extends RuntimeException {
public UnsupportedJobType(String message) {
super(message);
}
}
@@ -0,0 +1,56 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.jobs;

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

import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Job;
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());

public WorkflowJobWrapper(Job job) {
super(job);
}

@Override
public SCM getScm() {
try {
Class<?> workflowJobClazz = getJob().getClass();
Method getDefinitionMethod = workflowJobClazz.getDeclaredMethod("getDefinition");
Object definition = getDefinitionMethod.invoke(getJob());

Class<?> cpsScmFlowDefinitionClazz = definition.getClass();
Method getScmMethod = cpsScmFlowDefinitionClazz.getMethod("getScm");

Object scm = getScmMethod.invoke(definition);
if (scm instanceof SCM) {
return (SCM) scm;
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, Messages.WorkflowJobWrapper_GetWorkflowRepoScmFail(), e);
}
return null;
}

@Override
public FilePath getSomeWorkspace() throws IOException, InterruptedException {
FilePath workspaceForWorkflow = Jenkins.getInstance().getWorkspaceFor((TopLevelItem) getJob()).withSuffix("@script");
if (workspaceForWorkflow.exists()) {
return workspaceForWorkflow;
}
return null;
}

@Override
public AbstractBuild getSomeBuildWithWorkspace() {
return null; //TODO Add implementation, perhaps is not necessary
}
}
@@ -0,0 +1,2 @@
JobWrapperFactory.UnsupportedJobType=Unsupported job type. Class: {0}
WorkflowJobWrapper.GetWorkflowRepoScmFail=Get repo scm from Workflow job fail
@@ -0,0 +1,2 @@
JobWrapperFactory.UnsupportedJobType=Niewspierany typ zadania. Class: {0}
WorkflowJobWrapper.GetWorkflowRepoScmFail=B\u0142\u0105d przy obs\u0142udze repo scm w WorkflowJob

0 comments on commit 3885d16

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