Skip to content
Permalink
Browse files

[JENKINS-28041] Code clean-up

  • Loading branch information
pjanouse committed May 15, 2015
1 parent c05b80f commit 3cc5162d68ee0f8922e1de184f75b34cdbac93bd
@@ -25,9 +25,6 @@

import hudson.Extension;
import hudson.model.AbstractItem;
import hudson.model.AbstractProject;
import hudson.model.View;
import hudson.model.ViewGroup;
import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;

@@ -59,31 +56,33 @@ protected int run() throws Exception {
hs.addAll(jobs);

for (String job_s: hs) {
AbstractItem job = (AbstractItem) Jenkins.getInstance().getItemByFullName(job_s);

if(job == null) {
stderr.format("No such job '%s'\n", job_s);
errorOccurred = true;
continue;
}
AbstractItem job = null;

try {
job.checkPermission(AbstractItem.READ);
job.checkPermission(AbstractItem.DELETE);
} catch (Exception e) {
stderr.println(e.getMessage());
errorOccurred = true;
continue;
}
job = (AbstractItem) Jenkins.getInstance().getItemByFullName(job_s);

if(job == null) {
stderr.format("No such job '%s'\n", job_s);
errorOccurred = true;
continue;
}

try {
job.checkPermission(AbstractItem.DELETE);
} catch (Exception e) {
stderr.println(e.getMessage());
errorOccurred = true;
continue;
}

try {
job.delete();
} catch (Exception e) {
stderr.format("Unexpected exception occurred during deletion of job '%s': %s\n",
job.getDisplayName(),
job == null ? "(null)" : job.getFullName(),
e.getMessage()
);
errorOccurred = true;
//noinspection UnnecessaryContinue
continue;
}
}
@@ -57,31 +57,33 @@ protected int run() throws Exception {
hs.addAll(nodes);

for (String node_s : hs) {
Node node = null;

Node node = Jenkins.getInstance().getNode(node_s);
try {
node = Jenkins.getInstance().getNode(node_s);

if(node == null) {
stderr.format("No such node '%s'\n", node_s);
errorOccurred = true;
continue;
}
if(node == null) {
stderr.format("No such node '%s'\n", node_s);
errorOccurred = true;
continue;
}

try {
node.checkPermission(Computer.DELETE);
} catch (Exception e) {
stderr.println(e.getMessage());
errorOccurred = true;
continue;
}
try {
node.checkPermission(Computer.DELETE);
} catch (Exception e) {
stderr.println(e.getMessage());
errorOccurred = true;
continue;
}

try {
Jenkins.getInstance().removeNode(node);
} catch (Exception e) {
stderr.format("Unexpected exception occurred during deletion of node '%s': %s\n",
node.getDisplayName(),
node == null ? "(null)" : node.toComputer().getName(),
e.getMessage()
);
errorOccurred = true;
//noinspection UnnecessaryContinue
continue;
}
}
@@ -24,15 +24,14 @@
package hudson.cli;

import hudson.Extension;
import hudson.cli.handlers.ViewOptionHandler;
import hudson.model.ViewGroup;
import hudson.model.View;

import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;

import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;

/**
* @author ogondza, pjanouse
@@ -59,69 +58,40 @@ protected int run() throws Exception {
HashSet<String> hs = new HashSet<String>();
hs.addAll(views);

ViewOptionHandler voh = new ViewOptionHandler(null, null, null);

for(String view_s : hs) {
View view = null;

ViewGroup group = Jenkins.getInstance();

final StringTokenizer tok = new StringTokenizer(view_s, "/");
while (tok.hasMoreTokens()) {
String viewName = tok.nextToken();

view = group.getView(viewName);
if (view == null) {
stderr.format("No view named %s inside view %s", viewName, group.getDisplayName());
errorOccurred = true;
break;
}

try {
try {
view.checkPermission(View.READ);
view = voh.getView(view_s);
view.checkPermission(View.DELETE);
} catch (Exception e) {
stderr.println(e.getMessage());
errorOccurred = true;
view = null;
break;
}

if (view instanceof ViewGroup) {
group = (ViewGroup) view;
} else if (tok.hasMoreTokens()) {
stderr.format("%s view can not contain views", view.getViewName());
view = null;
break;
continue;
}
}

if(view ==null)
continue;

try {
view.checkPermission(View.DELETE);
} catch (Exception e) {
stderr.println(e.getMessage());
errorOccurred = true;
continue;
}

try {
group = view.getOwner();
ViewGroup group = view.getOwner();
if (!group.canDelete(view)) {
stderr.format("%s does not allow to delete '%s' view\n",
group.getDisplayName(),
view.getViewName()
);
errorOccurred = true;
continue;
} else {
group.deleteView(view);
}

group.deleteView(view);
} catch (Exception e) {
stderr.format("Unexpected exception occurred during deletion of view '%s': %s\n",
view.getViewName(),
view == null ? "(null)" : view.getViewName(),
e.getMessage()
);
errorOccurred = true;
//noinspection UnnecessaryContinue
continue;
}
}
return errorOccurred ? -1 : 0;
@@ -73,7 +73,7 @@ public int parseArguments(Parameters params) throws CmdLineException {
return 1;
}

private View getView(String name) throws CmdLineException {
public View getView(String name) throws CmdLineException {

View view = null;
ViewGroup group = Jenkins.getInstance();
@@ -538,7 +538,6 @@ public synchronized void doSubmitDescription( StaplerRequest req, StaplerRespons
* since it predates {@code <l:confirmationLink>}. {@code /delete} goes to a Jelly page
* which should now be unused by core but is left in case plugins are still using it.
*/
// @CLIMethod(name="delete-job")
@RequirePOST
public void doDoDelete( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException, InterruptedException {
delete();
@@ -1292,7 +1292,6 @@ public void updateByXml(final InputStream source) throws IOException, ServletExc
/**
* Really deletes the slave.
*/
// @CLIMethod(name="delete-node")
@RequirePOST
public HttpResponse doDoDelete() throws IOException {
checkPermission(DELETE);
@@ -40,7 +40,6 @@

/**
* @author pjanouse
* @since TODO
*/
public class DeleteJobCommandTest {

@@ -24,28 +24,21 @@

/**
* @author pjanouse
* @since TODO
*/

package hudson.cli;

import hudson.model.Computer;
import hudson.slaves.DumbSlave;
import hudson.slaves.NodeProperty;
import jenkins.model.Jenkins;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.ArrayList;

import static hudson.cli.CLICommandInvoker.Matcher.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.notNullValue;

public class DeleteNodeCommandTest {

@@ -46,7 +46,6 @@

/**
* @author ??, pjanouse
* @since TODO
*/
public class DeleteViewCommandTest {

0 comments on commit 3cc5162

Please sign in to comment.
You can’t perform that action at this time.