Skip to content

Commit

Permalink
fix JENKINS-12228 : allow to filter artifacts returned by the option …
Browse files Browse the repository at this point in the history
…provider, based on a java-regex
  • Loading branch information
vbehar committed Jan 4, 2012
1 parent cd67a50 commit 68da7d4
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/main/java/org/jenkinsci/plugins/rundeck/OptionProvider.java
@@ -1,9 +1,9 @@
package org.jenkinsci.plugins.rundeck;

import hudson.model.TopLevelItem;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.Run;
import hudson.model.TopLevelItem;
import hudson.model.Run.Artifact;
import hudson.util.RunList;
import java.io.IOException;
Expand All @@ -28,23 +28,41 @@ public class OptionProvider {
/**
* Provider for artifacts of a specific build, with the name and absolute url of the artifact.<br>
* Mandatory parameter : "project"<br>
* Optional parameters : "build" (either a build number, or "lastStable", "lastSuccessful", "last")
* Optional parameters : "build" (either a build number, or "lastStable", "lastSuccessful", "last"), "artifactRegex"
* (java regex used to filter artifacts).
*/
public void doArtifact(StaplerRequest request, StaplerResponse response) throws IOException {
// mandatory parameters
AbstractProject<?, ?> project = findProject(request.getParameter("project"));
if (project == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "You must provide a valid 'project' parameter !");
return;
}

// optional parameters
String artifactRegex = request.getParameter("artifactRegex");
Pattern artifactPattern = null;
if (StringUtils.isNotBlank(artifactRegex)) {
try {
artifactPattern = Pattern.compile(artifactRegex);
} catch (PatternSyntaxException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Invalid java-regex syntax for the 'artifactRegex' parameter : " + e.getMessage());
return;
}
}

Run<?, ?> build = findBuild(request.getParameter("build"), project);
if (build == null) {
return;
}

List<Option> options = new ArrayList<OptionProvider.Option>();
for (Artifact artifact : build.getArtifacts()) {
options.add(new Option(artifact.getFileName(), buildArtifactUrl(build, artifact)));
if (artifactPattern == null
|| (artifactPattern != null && artifactPattern.matcher(artifact.getFileName()).matches())) {
options.add(new Option(artifact.getFileName(), buildArtifactUrl(build, artifact)));
}
}

writeJson(options, response);
Expand Down Expand Up @@ -77,7 +95,7 @@ public void doBuild(StaplerRequest request, StaplerResponse response) throws IOE
artifactPattern = Pattern.compile(artifactRegex);
} catch (PatternSyntaxException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Invalid java-regex syntax for the 'artifactPattern' parameter : " + e.getMessage());
"Invalid java-regex syntax for the 'artifactRegex' parameter : " + e.getMessage());
return;
}
}
Expand Down

0 comments on commit 68da7d4

Please sign in to comment.