From 5e00f2374f854f3013f4376c6c07ec498165ab3c Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 24 Dec 2009 02:16:06 +0000 Subject: [PATCH] Added a new CLI command to retrieve list of changes. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@24895 71c3de6d-444a-0410-be80-ed276b4c234a --- .../hudson/cli/AbstractBuildRangeCommand.java | 30 +++++++ .../java/hudson/cli/DeleteBuildsCommand.java | 21 ++--- .../java/hudson/cli/ListChangesCommand.java | 82 +++++++++++++++++++ 3 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 core/src/main/java/hudson/cli/AbstractBuildRangeCommand.java create mode 100644 core/src/main/java/hudson/cli/ListChangesCommand.java diff --git a/core/src/main/java/hudson/cli/AbstractBuildRangeCommand.java b/core/src/main/java/hudson/cli/AbstractBuildRangeCommand.java new file mode 100644 index 0000000000..5e500b2207 --- /dev/null +++ b/core/src/main/java/hudson/cli/AbstractBuildRangeCommand.java @@ -0,0 +1,30 @@ +package hudson.cli; + +import hudson.model.AbstractBuild; +import hudson.model.AbstractProject; +import hudson.model.Fingerprint.RangeSet; +import org.kohsuke.args4j.Argument; + +import java.io.IOException; +import java.util.List; + +/** + * {@link CLICommand} that acts on a series of {@link AbstractBuild}s. + * + * @author Kohsuke Kawaguchi + */ +public abstract class AbstractBuildRangeCommand extends CLICommand { + @Argument(metaVar="JOB",usage="Name of the job to build",required=true,index=0) + public AbstractProject 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> builds) throws IOException; +} diff --git a/core/src/main/java/hudson/cli/DeleteBuildsCommand.java b/core/src/main/java/hudson/cli/DeleteBuildsCommand.java index 090a100485..b52eb2d541 100644 --- a/core/src/main/java/hudson/cli/DeleteBuildsCommand.java +++ b/core/src/main/java/hudson/cli/DeleteBuildsCommand.java @@ -23,12 +23,10 @@ */ package hudson.cli; -import hudson.model.AbstractBuild; -import hudson.model.AbstractProject; -import hudson.model.Fingerprint.RangeSet; import hudson.Extension; -import org.kohsuke.args4j.Argument; +import hudson.model.AbstractBuild; +import java.io.IOException; import java.io.PrintStream; import java.util.List; @@ -38,18 +36,12 @@ import java.util.List; * @author Kohsuke Kawaguchi */ @Extension -public class DeleteBuildsCommand extends CLICommand { +public class DeleteBuildsCommand extends AbstractBuildRangeCommand { @Override public String getShortDescription() { return "Deletes build record(s)"; } - @Argument(metaVar="JOB",usage="Name of the job to build",required=true,index=0) - public AbstractProject 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; - @Override protected void printUsageSummary(PrintStream stderr) { stderr.println( @@ -57,10 +49,8 @@ public class DeleteBuildsCommand extends CLICommand { ); } - protected int run() throws Exception { - RangeSet rs = RangeSet.fromString(range,false); - List builds = job.getBuilds(rs); - + @Override + protected int act(List> builds) throws IOException { for (AbstractBuild build : builds) build.delete(); @@ -68,4 +58,5 @@ public class DeleteBuildsCommand extends CLICommand { return 0; } + } diff --git a/core/src/main/java/hudson/cli/ListChangesCommand.java b/core/src/main/java/hudson/cli/ListChangesCommand.java new file mode 100644 index 0000000000..e0b61ed205 --- /dev/null +++ b/core/src/main/java/hudson/cli/ListChangesCommand.java @@ -0,0 +1,82 @@ +package hudson.cli; + +import hudson.Extension; +import hudson.model.AbstractBuild; +import hudson.scm.ChangeLogSet; +import hudson.scm.ChangeLogSet.Entry; +import hudson.util.QuotedStringTokenizer; +import org.kohsuke.args4j.Option; +import org.kohsuke.stapler.export.Flavor; +import org.kohsuke.stapler.export.Model; +import org.kohsuke.stapler.export.ModelBuilder; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; + +/** + * Retrieves a change list for the specified builds. + * + * @author Kohsuke Kawaguchi + */ +@Extension +public class ListChangesCommand extends AbstractBuildRangeCommand { + @Override + public String getShortDescription() { + return "Dumps the changelog for the specified build(s)"; + } + +// @Override +// protected void printUsageSummary(PrintStream stderr) { +// TODO +// } + + enum Format { + XML, CSV, PLAIN + } + + @Option(name="-format",usage="Controls how the output from this command is printed.") + public Format format = Format.PLAIN; + + @Override + protected int act(List> builds) throws IOException { + switch (format) { + case XML: + PrintWriter w = new PrintWriter(stdout); + w.println(""); + for (AbstractBuild build : builds) { + w.println(""); + ChangeLogSet cs = build.getChangeSet(); + Model p = new ModelBuilder().get(cs.getClass()); + p.writeTo(cs,Flavor.XML.createDataWriter(cs,w)); + w.println(""); + } + w.println(""); + 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())); + } + } + 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); + } + } + break; + } + + return 0; + } + +} -- GitLab