Skip to content

Commit

Permalink
[FIXED JENKINS-14978] expand project name with TokenMacro
Browse files Browse the repository at this point in the history
  • Loading branch information
imod committed Sep 19, 2012
1 parent 9d3a174 commit 42a33ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -60,6 +60,11 @@
<artifactId>async-http-client</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Expand Up @@ -35,6 +35,8 @@
import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -86,12 +88,14 @@ public class MavenDeploymentDownloader extends Builder {
public MavenDeploymentDownloader(String projectName, String filePattern, String permaLink, String targetDir, boolean stripVersion,
String stripVersionPattern, boolean failIfNoArtifact, boolean cleanTargetDir) {
// check the permissions only if we can
StaplerRequest req = Stapler.getCurrentRequest();
if (req != null) {
// Prevents both invalid values and access to artifacts of projects which this user cannot see.
// If value is parameterized, it will be checked when build runs.
if (Hudson.getInstance().getItemByFullName(projectName, Job.class) == null) {
projectName = ""; // Ignore/clear bad value to avoid ugly 500 page
if(!projectName.startsWith("$")){ // if this is a parameter, we can't check the name here it will be expanded by the TokenMacro...
StaplerRequest req = Stapler.getCurrentRequest();
if (req != null) {
// Prevents both invalid values and access to artifacts of projects which this user cannot see.
// If value is parameterized, it will be checked when build runs.
if (Hudson.getInstance().getItemByFullName(projectName, Job.class) == null) {
projectName = ""; // Ignore/clear bad value to avoid ugly 500 page
}
}
}
this.projectName = projectName;
Expand Down Expand Up @@ -147,7 +151,21 @@ public boolean isFailIfNoArtifact() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {

final PrintStream console = listener.getLogger();
final Job<?, ?> job = Hudson.getInstance().getItemByFullName(projectName, Job.class);

String resolvedProjectName = null;
try {
resolvedProjectName = TokenMacro.expandAll( build, listener, projectName );
} catch (MacroEvaluationException e1) {
console.println(Messages.jobNameExandFailed()+": "+e1.getMessage());
return false;
}

if(StringUtils.isBlank(resolvedProjectName)){
console.println(Messages.noJobName());
return false;
}

final Job<?, ?> job = Hudson.getInstance().getItemByFullName(resolvedProjectName, Job.class);

FilePath targetDirFp = new FilePath(build.getWorkspace(), targetDir);
if (cleanTargetDir) {
Expand All @@ -162,10 +180,10 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

{
// do some hyper linked logging
final String jobUrl = Hudson.getInstance().getRootUrl() + "job/" + projectName;
final String jobUrl = Hudson.getInstance().getRootUrl() + "job/" + resolvedProjectName;
final String linkBuildNr = HyperlinkNote.encodeTo(jobUrl + "/" + resolvedJob.number, "#" + resolvedJob.number);
final String linkPerma = HyperlinkNote.encodeTo(jobUrl + "/" + link.getId(), link.getDisplayName());
final String linkJob = HyperlinkNote.encodeTo(jobUrl, projectName);
final String linkJob = HyperlinkNote.encodeTo(jobUrl, resolvedProjectName);
console.println(Messages.resolveArtifact(linkBuildNr, linkPerma, linkJob));
}
}
Expand Down
Expand Up @@ -10,4 +10,6 @@ resolveArtifact=resolve linked maven artifacts from build number {0} ({1}) on pr
failedUrlParsing=failed parsing the given url: {0} : {1}
downloadArtifact=download maven artifact: {0} to {1}
downloadArtifactFailed=failed downloading file {0} : {1}
jobNameExandFailed=not able to expand the token to a project name, can't download linked maven artifacts
noJobName=no job name was given to download linked maven artifacts

0 comments on commit 42a33ee

Please sign in to comment.