Skip to content

Commit

Permalink
fixed JENKINS-12215
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob_robertson committed Dec 24, 2011
1 parent 44d79cb commit 947f532
Showing 1 changed file with 33 additions and 14 deletions.
Expand Up @@ -10,6 +10,7 @@
import hudson.tasks.Shell; import hudson.tasks.Shell;
import hudson.util.DescribableList; import hudson.util.DescribableList;


import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
Expand Down Expand Up @@ -88,42 +89,60 @@ private DescribableList<Builder,Descriptor<Builder>> getBuildersList(AbstractPro


public boolean setValues(AbstractProject<?, ?> item, Set<String> set) { public boolean setValues(AbstractProject<?, ?> item, Set<String> set) {
DescribableList<Builder,Descriptor<Builder>> buildersList = getBuildersList(item); DescribableList<Builder,Descriptor<Builder>> buildersList = getBuildersList(item);
Shell shell = null;
String command = set.iterator().next(); String command = set.iterator().next();


// if the command is empty or NOTHING, remove the shell builder from the job // if the command is empty or NOTHING, remove the shell builder from the job
if(command.equals(NOTHING) || command.equals("")) { if(command.equals(NOTHING) || command.equals("")) {
shell = (Shell)buildersList.get(Shell.class); Shell shell = (Shell) buildersList.get(Shell.class);
if(shell != null) { if(shell != null) {
try { try {
// the remove command will persist the project
buildersList.remove(shell.getDescriptor()); buildersList.remove(shell.getDescriptor());
} catch(java.io.IOException e) { } catch(java.io.IOException e) {
System.err.println("IOException Thrown removing shell value"); System.err.println("IOException Thrown removing shell value");
return false; return false;
} }
} }
} else { } else {
boolean replace = true; Shell newShell = new Shell(command);
// check to see if we need to replace the shell command. This prevents persisting // check to see if we need to replace the shell command. This prevents persisting
// an empty change, which is important for keeping audit trails clean. // an empty change, which is important for keeping audit trails clean.
Shell oldShell = buildersList.get(Shell.class); Shell currentShell = buildersList.get(Shell.class);
if (oldShell != null) { if (currentShell != null) {
String oldCommand = oldShell.getCommand(); String oldCommand = currentShell.getCommand();
if (command.equals(oldCommand)) { if (!command.equals(oldCommand)) {
replace = false; return replaceBuilder(buildersList, currentShell, newShell);
} }
} } else {
if (replace) {
shell = new Shell(command);
try { try {
buildersList.replace(shell); buildersList.add(newShell);
} catch(java.io.IOException e) { } catch(java.io.IOException e) {
System.err.println("IOException Thrown replacing shell value"); System.err.println("IOException Thrown add shell value");
return false; return false;
} }
} }
} }
return true; return true;
}
/**
* If we do other builders, publishers, etc - this should be the pattern to use.
* @throws IOException
*/
private boolean replaceBuilder(DescribableList<Builder,Descriptor<Builder>> builders, Builder oldBuilder, Builder newBuilder) {
List<Builder> newList = new ArrayList<Builder>(builders.toList());
for (int i = 0; i < newList.size(); i++) {
Builder b = newList.get(i);
if (b == oldBuilder) {
newList.set(i, newBuilder);
}
}
try {
builders.replaceBy(newList);
return true;
} catch(java.io.IOException e) {
System.err.println("IOException Thrown replacing builder list");
return false;
}
} }
} }
} }
Expand Down

0 comments on commit 947f532

Please sign in to comment.