Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all CLI commands compatible with Pipeline where possible #2874

Merged
merged 40 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
5046c56
[FIX JENKINS-30785] Generalize some CLI stuff to AbstractItem
daniel-beck Jan 2, 2017
2e27be7
Offering default methods on ParameterizedJob.
jglick May 1, 2017
fbadce3
Javadoc typo.
jglick May 1, 2017
626b876
Cleaner use of default methods in ParameterizedJob.
jglick May 2, 2017
3139f23
Need to pick up https://github.com/infradna/bridge-method-injector/pu…
jglick May 2, 2017
d71db0f
Sketch of pulling disabled functionality into ParameterizedJob.
jglick May 3, 2017
9318eef
EnableJobCommandTest.groovy → EnableJobCommandTest.java, and replacin…
jglick May 4, 2017
b7e1d86
All CLI commands could be broken by a missing CLI.*.shortDescription …
jglick May 4, 2017
41630e2
Forgot to move CLI method short descriptions to new package.
jglick May 4, 2017
1fffbed
Needed a @CLIResolver for ParameterizedJob. Adding an OptionHandler w…
jglick May 4, 2017
cd95d2b
Trying to fix up access-modifier versions; started failing in CI toda…
jglick May 4, 2017
137b8ec
Introduced <p:makeDisabled/> by analogy with <p:config-disableBuild/>.
jglick May 4, 2017
36a5301
Using new type bounds.
jglick May 4, 2017
1c759d3
Merge branch 'ParameterizedJobMixIn-defaults' into AbstractProject.di…
jglick May 4, 2017
59fb6e6
access-modifier 1.11 released.
jglick May 4, 2017
aeb9952
MatrixProject and MavenModuleSet both expect to have access to makeDi…
jglick May 4, 2017
bb617a6
Trying to generalize some more.
jglick May 4, 2017
1517179
Minor simplification.
jglick May 4, 2017
265ae0a
[JENKINS-34716] Generalizing doPolling and schedulePolling.
jglick May 4, 2017
b6459e1
isBuildable
jglick May 5, 2017
30c6674
Obsolete comment.
jglick May 5, 2017
244e419
Updated comments.
jglick May 5, 2017
82ad44a
bridge-method-injector 1.17
jglick May 5, 2017
d24bb16
Unfortunately AbstractProject.schedulePolling cannot delegate to SCMT…
jglick May 5, 2017
7be31a1
Merge branch 'jenkins-24141' of https://github.com/abayer/jenkins int…
jglick May 5, 2017
ab290b6
Making delete-builds and list-changes commands work with Pipeline.
jglick May 5, 2017
d36a1c9
[FIXED JENKINS-41527] Made console CLI command compatible with Pipeline.
jglick May 5, 2017
2703411
Merge branch 'JENKINS-30785' of https://github.com/daniel-beck/jenkin…
jglick May 5, 2017
bb6dd69
Fixed set-build-description and set-build-display-name.
jglick May 5, 2017
8a75156
Merge branch 'master' into CLI-JENKINS-41527
jglick May 8, 2017
d5e83dd
@oleg-nenashev agreed it would be clearer to explicitly mark commands…
jglick May 8, 2017
220e152
Updated tests to match slight message changes.
jglick May 9, 2017
faaee4c
bridge-method-injector 1.17
jglick May 9, 2017
8f49071
Merge branch 'master' into ParameterizedJobMixIn-defaults
jglick May 9, 2017
0a00506
Merge branch 'ParameterizedJobMixIn-defaults' into AbstractProject.di…
jglick May 9, 2017
6a38c31
Merge branch 'master' into CLI-JENKINS-41527
jglick May 10, 2017
f0b79a0
Merge branch 'master' into AbstractProject.disabled-JENKINS-27299
jglick May 10, 2017
eed093e
Merge branch 'AbstractProject.disabled-JENKINS-27299' into CLI-JENKIN…
jglick May 10, 2017
59223d1
@olivergondza pointed out that RunRangeCommand is a better name than …
jglick May 12, 2017
ec4e66e
Merge branch 'master' into CLI-JENKINS-41527
oleg-nenashev May 14, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/java/hudson/cli/AbstractBuildRangeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
* {@link CLICommand} that acts on a series of {@link AbstractBuild}s.
*
* @author Kohsuke Kawaguchi
* @deprecated rather use {@link RunRangeCommand}
*/
@Deprecated
public abstract class AbstractBuildRangeCommand extends CLICommand {
@Argument(metaVar="JOB",usage="Name of the job to build",required=true,index=0)
public AbstractProject<?,?> job;
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/hudson/cli/BuildCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import hudson.Util;
import hudson.console.ModelHyperlinkNote;
import hudson.model.AbstractProject;
import hudson.model.Cause.UserIdCause;
import hudson.model.CauseAction;
import hudson.model.Job;
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/hudson/cli/ConsoleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import hudson.Extension;
import hudson.console.AnnotatedLargeText;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.PermalinkProjectAction.Permalink;
import hudson.model.Run;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

Expand All @@ -29,7 +29,7 @@ public String getShortDescription() {
}

@Argument(metaVar="JOB",usage="Name of the job",required=true)
public AbstractProject<?,?> job;
public Job<?,?> job;

@Argument(metaVar="BUILD",usage="Build number or permalink to point to the build. Defaults to the last build",required=false,index=1)
public String build="lastBuild";
Expand All @@ -43,7 +43,7 @@ public String getShortDescription() {
protected int run() throws Exception {
job.checkPermission(Item.BUILD);

AbstractBuild<?,?> run;
Run<?,?> run;

try {
int n = Integer.parseInt(build);
Expand All @@ -54,7 +54,7 @@ protected int run() throws Exception {
// maybe a permalink?
Permalink p = job.getPermalinks().get(build);
if (p!=null) {
run = (AbstractBuild)p.resolve(job);
run = p.resolve(job);
if (run==null)
throw new IllegalStateException("Permalink "+build+" produced no build");
} else {
Expand Down Expand Up @@ -94,7 +94,7 @@ protected int run() throws Exception {
/**
* Find the byte offset in the log input stream that marks "last N lines".
*/
private long seek(AbstractBuild<?, ?> run) throws IOException {
private long seek(Run<?, ?> run) throws IOException {
class RingBuffer {
long[] lastNlines = new long[n];
int ptr=0;
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/hudson/cli/DeleteBuildsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@
package hudson.cli;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Run;

import java.io.IOException;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.List;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;

/**
* Deletes builds records in a bulk.
*
* @author Kohsuke Kawaguchi
*/
@Restricted(DoNotUse.class) // command implementation only
@Extension
public class DeleteBuildsCommand extends AbstractBuildRangeCommand {
public class DeleteBuildsCommand extends RunRangeCommand {
@Override
public String getShortDescription() {
return Messages.DeleteBuildsCommand_ShortDescription();
Expand All @@ -52,12 +54,12 @@ protected void printUsageSummary(PrintStream stderr) {
}

@Override
protected int act(List<AbstractBuild<?, ?>> builds) throws IOException {
protected int act(List<Run<?, ?>> builds) throws IOException {
job.checkPermission(Run.DELETE);

final HashSet<Integer> hsBuilds = new HashSet<Integer>();

for (AbstractBuild build : builds) {
for (Run<?, ?> build : builds) {
if (!hsBuilds.contains(build.number)) {
build.delete();
hsBuilds.add(build.number);
Expand Down
56 changes: 35 additions & 21 deletions core/src/main/java/hudson/cli/ListChangesCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package hudson.cli;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
import hudson.util.QuotedStringTokenizer;
import jenkins.scm.RunWithSCM;
import org.kohsuke.args4j.Option;
import org.kohsuke.stapler.export.Flavor;
import org.kohsuke.stapler.export.Model;
Expand All @@ -13,14 +14,17 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;

/**
* Retrieves a change list for the specified builds.
*
* @author Kohsuke Kawaguchi
*/
@Restricted(DoNotUse.class) // command implementation only
@Extension
public class ListChangesCommand extends AbstractBuildRangeCommand {
public class ListChangesCommand extends RunRangeCommand {
@Override
public String getShortDescription() {
return Messages.ListChangesCommand_ShortDescription();
Expand All @@ -39,40 +43,50 @@ enum Format {
public Format format = Format.PLAIN;

@Override
protected int act(List<AbstractBuild<?, ?>> builds) throws IOException {
protected int act(List<Run<?, ?>> builds) throws IOException {
// Loading job for this CLI command requires Item.READ permission.
// No other permission check needed.
switch (format) {
case XML:
PrintWriter w = new PrintWriter(stdout);
w.println("<changes>");
for (AbstractBuild build : builds) {
w.println("<build number='"+build.getNumber()+"'>");
ChangeLogSet<?> cs = build.getChangeSet();
Model p = new ModelBuilder().get(cs.getClass());
p.writeTo(cs,Flavor.XML.createDataWriter(cs,w));
w.println("</build>");
for (Run<?, ?> build : builds) {
if (build instanceof RunWithSCM) {
w.println("<build number='" + build.getNumber() + "'>");
for (ChangeLogSet<?> cs : ((RunWithSCM<?, ?>) build).getChangeSets()) {
Model p = new ModelBuilder().get(cs.getClass());
p.writeTo(cs, Flavor.XML.createDataWriter(cs, w));
}
w.println("</build>");
}
}
w.println("</changes>");
w.flush();
break;
case CSV:
for (AbstractBuild build : builds) {
ChangeLogSet<?> cs = build.getChangeSet();
for (Entry e : cs) {
stdout.printf("%s,%s%n",
QuotedStringTokenizer.quote(e.getAuthor().getId()),
QuotedStringTokenizer.quote(e.getMsg()));
for (Run<?, ?> build : builds) {
if (build instanceof RunWithSCM) {
for (ChangeLogSet<?> cs : ((RunWithSCM<?, ?>) build).getChangeSets()) {
for (Entry e : cs) {
stdout.printf("%s,%s%n",
QuotedStringTokenizer.quote(e.getAuthor().getId()),
QuotedStringTokenizer.quote(e.getMsg()));
}
}
}
}
break;
case PLAIN:
for (AbstractBuild build : builds) {
ChangeLogSet<?> cs = build.getChangeSet();
for (Entry e : cs) {
stdout.printf("%s\t%s%n",e.getAuthor(),e.getMsg());
for (String p : e.getAffectedPaths())
stdout.println(" "+p);
for (Run<?, ?> build : builds) {
if (build instanceof RunWithSCM) {
for (ChangeLogSet<?> cs : ((RunWithSCM<?, ?>) build).getChangeSets()) {
for (Entry e : cs) {
stdout.printf("%s\t%s%n", e.getAuthor(), e.getMsg());
for (String p : e.getAffectedPaths()) {
stdout.println(" " + p);
}
}
}
}
}
break;
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/hudson/cli/ReloadJobCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import hudson.model.AbstractProject;
import hudson.model.Item;

import hudson.model.Items;
import hudson.model.TopLevelItem;
import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;

Expand Down Expand Up @@ -69,7 +71,6 @@ protected int run() throws Exception {
AbstractItem job = null;

try {
// TODO: JENKINS-30786
Item item = jenkins.getItemByFullName(job_s);
if (item instanceof AbstractItem) {
job = (AbstractItem) item;
Expand All @@ -78,11 +79,10 @@ protected int run() throws Exception {
}

if(job == null) {
// TODO: JENKINS-30785
AbstractProject project = AbstractProject.findNearest(job_s);
AbstractItem project = Items.findNearest(AbstractItem.class, job_s, jenkins);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just item? Or maybe even TopLevelItem (who would reload other items anyway?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reload is defined in AbstractItem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

throw new IllegalArgumentException(project == null ?
"No such job \u2018" + job_s + "\u2019 exists." :
String.format("No such job \u2018%s\u2019 exists. Perhaps you meant \u2018%s\u2019?",
"No such item \u2018" + job_s + "\u2019 exists." :
String.format("No such item \u2018%s\u2019 exists. Perhaps you meant \u2018%s\u2019?",
job_s, project.getFullName()));
}

Expand Down
29 changes: 29 additions & 0 deletions core/src/main/java/hudson/cli/RunRangeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hudson.cli;

import hudson.model.Fingerprint.RangeSet;
import hudson.model.Job;
import hudson.model.Run;
import org.kohsuke.args4j.Argument;

import java.io.IOException;
import java.util.List;

/**
* {@link CLICommand} that acts on a series of {@link Run}s.
* @since FIXME
*/
public abstract class RunRangeCommand extends CLICommand {
@Argument(metaVar="JOB",usage="Name of the job to build",required=true,index=0)
public Job<?,?> job;

@Argument(metaVar="RANGE",usage="Range of the build records to delete. 'N-M', 'N,M', or 'N'",required=true,index=1)
public String range;

protected int run() throws Exception {
RangeSet rs = RangeSet.fromString(range,false);

return act((List)job.getBuilds(rs));
}

protected abstract int act(List<Run<?,?>> builds) throws IOException;
}
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/cli/SetBuildDescriptionCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hudson.cli;

import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Job;
import hudson.model.Run;

import java.io.Serializable;
Expand All @@ -18,7 +18,7 @@ public String getShortDescription() {
}

@Argument(metaVar="JOB",usage="Name of the job to build",required=true,index=0)
public transient AbstractProject<?,?> job;
public transient Job<?,?> job;

@Argument(metaVar="BUILD#",usage="Number of the build",required=true,index=1)
public int number;
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/cli/SetBuildDisplayNameCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hudson.cli;

import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Job;
import hudson.model.Run;
import org.apache.commons.io.IOUtils;
import org.kohsuke.args4j.Argument;
Expand All @@ -18,7 +18,7 @@ public String getShortDescription() {
}

@Argument(metaVar="JOB", usage="Name of the job to build", required=true, index=0)
public transient AbstractProject<?, ?> job;
public transient Job<?, ?> job;

@Argument(metaVar="BUILD#", usage="Number of the build", required=true, index=1)
public int number;
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/model/AbstractItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -850,11 +850,11 @@ public String getSearchName() {
*/
@CLIResolver
public static AbstractItem resolveForCLI(
@Argument(required=true,metaVar="NAME",usage="Job name") String name) throws CmdLineException {
@Argument(required=true,metaVar="NAME",usage="Item name") String name) throws CmdLineException {
// TODO can this (and its pseudo-override in AbstractProject) share code with GenericItemOptionHandler, used for explicit CLICommand’s rather than CLIMethod’s?
AbstractItem item = Jenkins.getInstance().getItemByFullName(name, AbstractItem.class);
if (item==null) {
AbstractProject project = AbstractProject.findNearest(name); // TODO should be Items.findNearest
AbstractItem project = Items.findNearest(AbstractItem.class, name, Jenkins.getInstance());
throw new CmdLineException(null, project == null ? Messages.AbstractItem_NoSuchJobExistsWithoutSuggestion(name)
: Messages.AbstractItem_NoSuchJobExists(name, project.getFullName()));
}
Expand Down
16 changes: 8 additions & 8 deletions test/src/test/java/hudson/cli/ReloadJobCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public class ReloadJobCommandTest {

assertThat(result, failedWith(3));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("ERROR: No such job \u2018aProject\u2019 exists."));
assertThat(result.stderr(), containsString("ERROR: No such item ‘aProject’ exists."));

assertThat(project.scheduleBuild2(0).get().getLog(), containsString("echo 1"));
}
Expand Down Expand Up @@ -121,7 +121,7 @@ public class ReloadJobCommandTest {
.invokeWithArgs("never_created");
assertThat(result, failedWith(3));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("ERROR: No such job \u2018never_created\u2019 exists."));
assertThat(result.stderr(), containsString("ERROR: No such item ‘never_created’ exists."));
}

@Test public void reloadJobShouldFailIfJobDoesNotExistButNearExists() throws Exception {
Expand All @@ -133,7 +133,7 @@ public class ReloadJobCommandTest {
.invokeWithArgs("never_created1");
assertThat(result, failedWith(3));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("ERROR: No such job \u2018never_created1\u2019 exists. Perhaps you meant \u2018never_created\u2019?"));
assertThat(result.stderr(), containsString("ERROR: No such item ‘never_created1’ exists. Perhaps you meant ‘never_created’?"));
}

@Test public void reloadJobManyShouldSucceed() throws Exception {
Expand Down Expand Up @@ -182,7 +182,7 @@ public class ReloadJobCommandTest {

assertThat(result, failedWith(5));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("never_created: No such job \u2018never_created\u2019 exists."));
assertThat(result.stderr(), containsString("never_created: No such item ‘never_created’ exists."));
assertThat(result.stderr(), containsString("ERROR: " + CLICommand.CLI_LISTPARAM_SUMMARY_ERROR_TEXT));

assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
Expand All @@ -208,7 +208,7 @@ public class ReloadJobCommandTest {

assertThat(result, failedWith(5));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("never_created: No such job \u2018never_created\u2019 exists."));
assertThat(result.stderr(), containsString("never_created: No such item ‘never_created’ exists."));
assertThat(result.stderr(), containsString("ERROR: " + CLICommand.CLI_LISTPARAM_SUMMARY_ERROR_TEXT));

assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
Expand All @@ -234,7 +234,7 @@ public class ReloadJobCommandTest {

assertThat(result, failedWith(5));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("never_created: No such job \u2018never_created\u2019 exists."));
assertThat(result.stderr(), containsString("never_created: No such item ‘never_created’ exists."));
assertThat(result.stderr(), containsString("ERROR: " + CLICommand.CLI_LISTPARAM_SUMMARY_ERROR_TEXT));

assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
Expand All @@ -260,8 +260,8 @@ public class ReloadJobCommandTest {

assertThat(result, failedWith(5));
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("never_created1: No such job \u2018never_created1\u2019 exists."));
assertThat(result.stderr(), containsString("never_created2: No such job \u2018never_created2\u2019 exists."));
assertThat(result.stderr(), containsString("never_created1: No such item ‘never_created1’ exists."));
assertThat(result.stderr(), containsString("never_created2: No such item ‘never_created2’ exists."));
assertThat(result.stderr(), containsString("ERROR: " + CLICommand.CLI_LISTPARAM_SUMMARY_ERROR_TEXT));

assertThat(project1.scheduleBuild2(0).get().getLog(), containsString("echo 2"));
Expand Down
Loading