Skip to content

Commit

Permalink
[JENKINS-22969] Extract enable-job and disable-job
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed May 11, 2014
1 parent 8ea7841 commit 3d9c174
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 5 deletions.
52 changes: 52 additions & 0 deletions core/src/main/java/hudson/cli/DisableJobCommand.java
@@ -0,0 +1,52 @@
/*
* The MIT License
*
* Copyright (c) 2014 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.cli;

import org.kohsuke.args4j.Argument;

import hudson.Extension;
import hudson.model.AbstractProject;

/**
* @author ogondza
* @since TODO
*/
@Extension
public class DisableJobCommand extends CLICommand {

@Argument(required=true)
private AbstractProject<?, ?> project;

@Override
public String getShortDescription() {
return Messages.DisableJobCommand_ShortDescription();
}

@Override
protected int run() throws Exception{
project.checkPermission(AbstractProject.CONFIGURE);
project.makeDisabled(true);
return 0;
}
}
52 changes: 52 additions & 0 deletions core/src/main/java/hudson/cli/EnableJobCommand.java
@@ -0,0 +1,52 @@
/*
* The MIT License
*
* Copyright (c) 2014 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.cli;

import org.kohsuke.args4j.Argument;

import hudson.Extension;
import hudson.model.AbstractProject;

/**
* @author ogondza
* @since TODO
*/
@Extension
public class EnableJobCommand extends CLICommand {

@Argument(required=true)
private AbstractProject<?, ?> project;

@Override
public String getShortDescription() {
return Messages.EnableJobCommand_ShortDescription();
}

@Override
protected int run() throws Exception {
project.checkPermission(AbstractProject.CONFIGURE);
project.makeDisabled(false);
return 0;
}
}
2 changes: 0 additions & 2 deletions core/src/main/java/hudson/model/AbstractProject.java
Expand Up @@ -1903,15 +1903,13 @@ public HttpResponse doDoWipeOutWorkspace() throws IOException, ServletException,
}
}

@CLIMethod(name="disable-job")
@RequirePOST
public HttpResponse doDisable() throws IOException, ServletException {
checkPermission(CONFIGURE);
makeDisabled(true);
return new HttpRedirect(".");
}

@CLIMethod(name="enable-job")
@RequirePOST
public HttpResponse doEnable() throws IOException, ServletException {
checkPermission(CONFIGURE);
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/resources/hudson/cli/Messages.properties
Expand Up @@ -20,6 +20,10 @@ DeleteBuildsCommand.ShortDescription=\
Deletes build record(s).
DeleteViewCommand.ShortDescription=\
Deletes view.
DisableJobCommand.ShortDescription=\
Disables a job.
EnableJobCommand.ShortDescription=\
Enables a job.
GroovyCommand.ShortDescription=\
Executes the specified Groovy script.
GroovyshCommand.ShortDescription=\
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/resources/hudson/model/Messages.properties
Expand Up @@ -92,8 +92,6 @@ BallColor.Unstable=Unstable
CLI.clear-queue.shortDescription=Clears the build queue.
CLI.delete-job.shortDescription=Deletes a job.
CLI.reload-job.shortDescription=Reloads this job from disk.
CLI.disable-job.shortDescription=Disables a job.
CLI.enable-job.shortDescription=Enables a job.
CLI.delete-node.shortDescription=Deletes a node.
CLI.disconnect-node.shortDescription=Disconnects from a node.
CLI.connect-node.shortDescription=Reconnect to a node.
Expand Down
16 changes: 16 additions & 0 deletions test/src/main/java/hudson/cli/CLICommandInvoker.java
Expand Up @@ -250,5 +250,21 @@ public static Matcher failedWith(final long expectedCode) {
}
};
}

public static Matcher failedWith(final String message) {
return new Matcher("Exited with non-zero return code writing " + message + " to stderr") {
@Override protected boolean matchesSafely(Result result) {
return result.result != 0 && result.stderr().contains(message);
}
};
}

public static Matcher failed() {
return new Matcher("Exited with non-zero return code") {
@Override protected boolean matchesSafely(Result result) {
return result.result != 0;
}
};
}
}
}
40 changes: 39 additions & 1 deletion test/src/test/java/hudson/model/ProjectTest.java
Expand Up @@ -23,6 +23,9 @@
*/
package hudson.model;

import static hudson.cli.CLICommandInvoker.Matcher.*;

import hudson.cli.CLICommandInvoker;
import hudson.model.queue.QueueTaskFuture;
import hudson.security.AccessDeniedException2;
import org.acegisecurity.context.SecurityContextHolder;
Expand Down Expand Up @@ -642,7 +645,42 @@ public void testDoEnable() throws Exception{
}
assertFalse("Project should be enabled.", project.isDisabled());
}


@Test
public void disableUsingCli() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("a_project");

CLICommandInvoker invoker = new CLICommandInvoker(j, "disable-job");
CLICommandInvoker.Result res = invoker.invokeWithArgs("a_project");
assertThat(res, succeededSilently());

assertTrue(project.isDisabled());

res = invoker.invokeWithArgs("no_such_project");
assertThat(res, failed());

res = invoker.authorizedTo(Jenkins.READ, Job.READ).invokeWithArgs("a_project");
assertThat(res, failedWith("user is missing the Job/Configure permission"));
}

@Test
public void enableUsingCli() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("a_project");
project.disable();

CLICommandInvoker invoker = new CLICommandInvoker(j, "enable-job");
CLICommandInvoker.Result res = invoker.invokeWithArgs("a_project");
assertThat(res, succeededSilently());

assertFalse(project.isDisabled());

res = invoker.invokeWithArgs("no_such_project");
assertThat(res, failed());

res = invoker.authorizedTo(Jenkins.READ, Job.READ).invokeWithArgs("a_project");
assertThat(res, failedWith("user is missing the Job/Configure permission"));
}

/**
* Job is un-restricted (no nabel), this is submitted to queue, which spawns an on demand slave
* @throws Exception
Expand Down

0 comments on commit 3d9c174

Please sign in to comment.