Skip to content

Commit

Permalink
Adding file access to JobManagement. Still need test and access from …
Browse files Browse the repository at this point in the history
…JobParent
  • Loading branch information
Justin Ryan committed Jun 9, 2013
1 parent c4767e3 commit 7a9ad0a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.collect.Maps;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Map;

Expand Down Expand Up @@ -37,6 +39,16 @@ public void queueJob(String jobName) throws JobNameNotProvidedException {
validateJobNameArg(jobName);
}

@Override
public InputStream streamFileInWorkspace(String filePath) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public String readFileInWorkspace(String filePath) throws IOException {
throw new UnsupportedOperationException();
}

protected void validateUpdateArgs(String jobName, String config) {
validateJobNameArg(jobName);
validateConfigArg(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,15 @@ class FileJobManagement extends AbstractJobManagement {
new File(jobName + ext).write(config)
return true
}

@Override
public InputStream streamFileInWorkspace(String filePath) {
return new FileInputStream(new File(root, filePath));
}

@Override
public String readFileInWorkspace(String filePath) {
new File(root, filePath).text
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public interface JobManagement {
*/
void queueJob(String jobName) throws JobNameNotProvidedException;

InputStream streamFileInWorkspace(String filePath) throws IOException;
String readFileInWorkspace(String filePath) throws IOException;

/**
* Stream to write to, for stdout.
* @return PrintWriter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class StringJobManagement extends AbstractJobManagement {

Map<String,String> availableConfigs = [:]
Map<String,String> savedConfigs = [:]
Map<String,String> availableFiles = [:]

Map<String,String> params = [:]
List<String> jobScheduled = []
Expand Down Expand Up @@ -65,5 +66,18 @@ public class StringJobManagement extends AbstractJobManagement {
void queueJob(String jobName) throws JobNameNotProvidedException {
jobScheduled << jobName
}


@Override
public InputStream streamFileInWorkspace(String filePath) {
String body = availableFiles.get(filePath)
return new InputStreamReader(new StringReader(body));
}

@Override
public String readFileInWorkspace(String filePath) {
String body = availableFiles.get(filePath)
return body
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher,
env.putAll(build.getBuildVariables());

// We run the DSL, it'll need some way of grabbing a template config.xml and how to save it
JenkinsJobManagement jm = new JenkinsJobManagement(listener.getLogger(), env);
JenkinsJobManagement jm = new JenkinsJobManagement(listener.getLogger(), env, build);

Set<GeneratedJob> freshJobs;
String jobName = build.getProject().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.collect.Collections2;
import com.google.common.collect.Sets;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.XmlFile;
import hudson.model.*;
import javaposse.jobdsl.dsl.*;
Expand All @@ -29,17 +30,19 @@ public final class JenkinsJobManagement extends AbstractJobManagement {
Jenkins jenkins = Jenkins.getInstance();
EnvVars envVars;
Set<GeneratedJob> modifiedJobs;
AbstractBuild<?, ?> build;

JenkinsJobManagement() {
super();
envVars = new EnvVars();
modifiedJobs = Sets.newHashSet();
}

public JenkinsJobManagement(PrintStream outputLogger, EnvVars envVars) {
public JenkinsJobManagement(PrintStream outputLogger, EnvVars envVars, AbstractBuild<?, ?> build) {
super(outputLogger);
this.envVars = envVars;
modifiedJobs = Sets.newHashSet();
this.modifiedJobs = Sets.newHashSet();
this.build = build;
}

@Override
Expand Down Expand Up @@ -96,7 +99,6 @@ public void queueJob(String jobName) throws JobNameNotProvidedException {

AbstractProject<?,?> project = (AbstractProject<?,?>) jenkins.getItemByFullName(jobName);

Object build = ((Executor) Thread.currentThread()).getCurrentExecutable();
if(build != null && build instanceof Run) {
Run run = (Run) build;
LOGGER.log(Level.INFO, String.format("Scheduling build of %s from %s", jobName, run.getParent().getName()));
Expand All @@ -108,6 +110,30 @@ public void queueJob(String jobName) throws JobNameNotProvidedException {
}


@Override
public InputStream streamFileInWorkspace(String relLocation) throws IOException {
FilePath filePath = locateValidFileInWorkspace(relLocation);
return filePath.read();
}

@Override
public String readFileInWorkspace(String relLocation) throws IOException {
FilePath filePath = locateValidFileInWorkspace(relLocation);
return filePath.readToString();
}

private FilePath locateValidFileInWorkspace(String relLocation) throws IOException {
FilePath filePath = build.getWorkspace().child(relLocation);
try {
if (!filePath.exists()) {
throw new IllegalStateException("File does not exists");
}
} catch(InterruptedException ie) {
throw new RuntimeException(ie);
}
return filePath;
}

private String lookupJob(String jobName) throws IOException {
LOGGER.log(Level.FINE, String.format("Looking up Job %s", jobName));
String jobXml = "";
Expand Down

0 comments on commit 7a9ad0a

Please sign in to comment.