Skip to content

Commit

Permalink
JENKINS-29407: make helper variables visible to pipelines jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Apr 14, 2018
1 parent 2b542d2 commit 766aa42
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
Expand Up @@ -39,11 +39,12 @@
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

import hudson.model.AbstractBuild;
import hudson.model.AbstractItem;
import hudson.model.Job;
import hudson.model.ParameterValue;
import hudson.model.Project;
import hudson.model.Run;
import hudson.model.StringParameterValue;
import jenkins.model.Jenkins;

/**
* Base class for parameters with scripts.
Expand Down Expand Up @@ -131,7 +132,7 @@ protected AbstractScriptableParameter(String name, String description, String ra
final Object o = ancestor.getObject();
if (o instanceof AbstractItem) {
final AbstractItem parentItem = (AbstractItem) o;
projectName = parentItem.getName();
projectName = parentItem.getFullName();
}
}
}
Expand Down Expand Up @@ -166,19 +167,22 @@ private Map<Object, Object> getHelperParameters() {
final Map<Object, Object> helperParameters = new LinkedHashMap<Object, Object>();

// First, if the project name is set, we then find the project by its name, and inject into the map
Project<?, ?> project = null;
Object project = null;
if (StringUtils.isNotBlank(this.projectName)) {
// first we try to get the item given its name, which is more efficient
project = Utils.getProjectByName(this.projectName);
project = Jenkins.getInstance().getItemByFullName(this.projectName);
} else {
// otherwise, in case we don't have the item name, we iterate looking for a job that uses this UUID
project = Utils.findProjectByParameterUUID(this.getRandomName());
}
if (project != null) {
helperParameters.put(JENKINS_PROJECT_VARIABLE_NAME, project);
AbstractBuild<?, ?> build = project.getLastBuild();
if (build != null && build.getHasArtifacts()) {
helperParameters.put(JENKINS_BUILD_VARIABLE_NAME, build);
if (project instanceof Job) {
@SuppressWarnings("rawtypes")
Run build = ((Job) project).getLastBuild();
if (build != null) {
helperParameters.put(JENKINS_BUILD_VARIABLE_NAME, build);
}
}
}

Expand Down
19 changes: 0 additions & 19 deletions src/main/java/org/biouno/unochoice/util/Utils.java
Expand Up @@ -144,25 +144,6 @@ public static boolean isSelected(@Nullable Object obj) {
return System.getenv();
}

/**
* Get project in Jenkins given its name.
*
* @since 1.3
* @param projectName project name in Jenkins
* @return Project or {@code null} if none with this name
* @deprecated The choice is arbitrary if there are multiple matches; use {@link Item#getFullName} and {@link Jenkins#getItemByFullName(String, Class)} instead.
*/
@SuppressWarnings("rawtypes")
public static @CheckForNull Project<?, ?> getProjectByName(@Nonnull String projectName) {
Authentication auth = Jenkins.getAuthentication();
for (Project p : Items.allItems(ACL.SYSTEM, Jenkins.getInstance(), Project.class)) {
if (p.getName().equals(projectName) && p.getACL().hasPermission(auth, Item.READ)) {
return p;
}
}
return null;
}

/**
* Find the current project give its parameter UUID.
*
Expand Down

0 comments on commit 766aa42

Please sign in to comment.