Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-28041] Extended delete-job CLI command to accept multiple na…
…mes to delete
- Loading branch information
Showing
21 changed files
with
256 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2015 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 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; | ||
|
||
import java.util.List; | ||
import java.util.HashSet; | ||
|
||
/** | ||
* @author pjanouse | ||
* @since TODO | ||
*/ | ||
@Extension | ||
public class DeleteJobCommand extends CLICommand { | ||
|
||
@Argument(usage="Name of the job(s) to delete", required=true, multiValued=true) | ||
private List<String> jobs; | ||
|
||
@Override | ||
public String getShortDescription() { | ||
|
||
return Messages.DeleteJobCommand_ShortDescription(); | ||
} | ||
|
||
@Override | ||
protected int run() throws Exception { | ||
|
||
boolean errorOccurred = false; | ||
|
||
HashSet<String> hs = new HashSet<String>(); | ||
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; | ||
} | ||
|
||
try { | ||
job.checkPermission(AbstractItem.READ); | ||
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(), | ||
e.getMessage() | ||
); | ||
errorOccurred = true; | ||
continue; | ||
} | ||
} | ||
return errorOccurred ? -1 : 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DeleteNodeCommand.ShortDescription=Sletter en node | ||
DeleteJobCommand.ShortDescription=Sletter et job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DeleteNodeCommand.ShortDescription=Knoten l\u00f6schen. | ||
DeleteJobCommand.ShortDescription=Job l\u00f6schen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DeleteNodeCommand.ShortDescription=Cancella un nodo | ||
DeleteJobCommand.ShortDescription=Cancella un job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DeleteNodeCommand.ShortDescription=Deletes a node | ||
DeleteJobCommand.ShortDescription=Deletes a job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
test/src/test/java/hudson/cli/DeleteJobCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2015 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 hudson.model.Job; | ||
import jenkins.model.Jenkins; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import java.io.IOException; | ||
|
||
import static hudson.cli.CLICommandInvoker.Matcher.*; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
/** | ||
* @author pjanouse | ||
* @since TODO | ||
*/ | ||
public class DeleteJobCommandTest { | ||
|
||
private CLICommandInvoker command; | ||
|
||
@Rule public final JenkinsRule j = new JenkinsRule(); | ||
|
||
@Before public void setUp() { | ||
|
||
command = new CLICommandInvoker(j, new DeleteJobCommand()); | ||
} | ||
|
||
@Test public void deleteJobShouldFailWithoutJobDeletePermission() throws IOException { | ||
|
||
j.createFreeStyleProject("aProject"); | ||
|
||
final CLICommandInvoker.Result result = command | ||
.authorizedTo(Job.READ, Jenkins.READ) | ||
.invokeWithArgs("aProject"); | ||
|
||
assertThat(result, failedWith(-1)); | ||
assertThat(result, hasNoStandardOutput()); | ||
assertThat(result.stderr(), containsString("user is missing the Job/Delete permission")); | ||
} | ||
|
||
@Test public void deleteJobShouldFailWithoutJobReadPermission() throws IOException { | ||
|
||
j.createFreeStyleProject("aProject"); | ||
|
||
final CLICommandInvoker.Result result = command | ||
.authorizedTo(Job.DELETE, Jenkins.READ) | ||
.invokeWithArgs("aProject"); | ||
|
||
assertThat(result, failedWith(-1)); | ||
assertThat(result, hasNoStandardOutput()); | ||
assertThat(result.stderr(), containsString("No such job 'aProject'")); | ||
} | ||
|
||
@Test public void deleteJobShouldSucceed() throws Exception { | ||
|
||
j.createFreeStyleProject("aProject"); | ||
|
||
final CLICommandInvoker.Result result = command | ||
.authorizedTo(Job.READ, Job.DELETE, Jenkins.READ) | ||
.invokeWithArgs("aProject"); | ||
|
||
assertThat(result, succeededSilently()); | ||
assertThat(j.jenkins.getItem("aProject"), nullValue()); | ||
} | ||
|
||
@Test public void deleteJobShouldFailIfJobDoesNotExist() { | ||
|
||
final CLICommandInvoker.Result result = command | ||
.authorizedTo(Job.READ, Job.DELETE, Jenkins.READ) | ||
.invokeWithArgs("never_created"); | ||
|
||
assertThat(result, failedWith(-1)); | ||
assertThat(result, hasNoStandardOutput()); | ||
assertThat(result.stderr(), containsString("No such job 'never_created'")); | ||
} | ||
|
||
@Test public void deleteJobManyShouldSucceed() throws Exception { | ||
|
||
j.createFreeStyleProject("aProject1"); | ||
j.createFreeStyleProject("aProject2"); | ||
j.createFreeStyleProject("aProject3"); | ||
|
||
final CLICommandInvoker.Result result = command | ||
.authorizedTo(Job.READ, Job.DELETE, Jenkins.READ) | ||
.invokeWithArgs("aProject1", "aProject2", "aProject3"); | ||
|
||
assertThat(result, succeededSilently()); | ||
assertThat(j.jenkins.getItem("aProject1"), nullValue()); | ||
assertThat(j.jenkins.getItem("aProject2"), nullValue()); | ||
assertThat(j.jenkins.getItem("aProject3"), nullValue()); | ||
} | ||
|
||
@Test public void deleteJobManyShouldFailIfAJobDoesNotExist() throws Exception { | ||
|
||
j.createFreeStyleProject("aProject1"); | ||
j.createFreeStyleProject("aProject2"); | ||
|
||
final CLICommandInvoker.Result result = command | ||
.authorizedTo(Job.READ, Job.DELETE, Jenkins.READ) | ||
.invokeWithArgs("aProject1", "aProject2", "never_created"); | ||
|
||
assertThat(result, failedWith(-1)); | ||
assertThat(result, hasNoStandardOutput()); | ||
assertThat(result.stderr(), containsString("No such job 'never_created'")); | ||
|
||
assertThat(j.jenkins.getItem("aProject1"), nullValue()); | ||
assertThat(j.jenkins.getItem("aProject2"), nullValue()); | ||
assertThat(j.jenkins.getItem("never_created"), nullValue()); | ||
} | ||
|
||
@Test public void deleteJobManyShouldSucceedEvenAJobIsSpecifiedTwice() throws Exception { | ||
|
||
j.createFreeStyleProject("aProject1"); | ||
j.createFreeStyleProject("aProject2"); | ||
|
||
final CLICommandInvoker.Result result = command | ||
.authorizedTo(Job.READ, Job.DELETE, Jenkins.READ) | ||
.invokeWithArgs("aProject1", "aProject2", "aProject1"); | ||
|
||
assertThat(result, succeededSilently()); | ||
assertThat(j.jenkins.getItem("aProject1"), nullValue()); | ||
assertThat(j.jenkins.getItem("aProject2"), nullValue()); | ||
} | ||
} |