Capture polling log that triggered a build.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@34844 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2010-09-15 18:24:15 +00:00
1 parent 594c5ef124
commit 295689128c
9 files changed
+226 -5

No files matched your search

+10 -1
View File
@@ -60,7 +60,16 @@ public abstract class Cause {
* By default, this method is used to render HTML as well.
*/
@Exported(visibility=3)
abstract public String getShortDescription();
public abstract String getShortDescription();
/**
* Called when the cause is registered to {@link AbstractBuild}.
*
* @param build
* never null
* @since 1.376
*/
public void onAddedTo(AbstractBuild build) {}
/**
* Report a line to the listener about this cause.
@@ -37,7 +37,7 @@ import java.util.List;
import java.util.Map;
@ExportedBean
public class CauseAction implements FoldableAction {
public class CauseAction implements FoldableAction, RunAction {
/**
* @deprecated since 2009-02-28
*/
@@ -95,6 +95,26 @@ public class CauseAction implements FoldableAction {
return causes.get(0).getShortDescription();
}
public void onLoad() {
// noop
}
public void onBuildComplete() {
// noop
}
/**
* When hooked up to build, notify {@link Cause}s.
*/
public void onAttached(Run owner) {
if (owner instanceof AbstractBuild) {// this should be always true but being defensive here
AbstractBuild b = (AbstractBuild) owner;
for (Cause c : causes) {
c.onAddedTo(b);
}
}
}
public void foldIntoExisting(hudson.model.Queue.Item item, Task owner, List<Action> otherActions) {
CauseAction existing = item.getAction(CauseAction.class);
if (existing!=null) {
+7
View File
@@ -266,6 +266,13 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
((RunAction) a).onLoad();
}
@Override
public void addAction(Action a) {
super.addAction(a);
if (a instanceof RunAction)
((RunAction) a).onAttached(this);
}
/*package*/ static long parseTimestampFromBuildDir(File buildDir) throws IOException {
try {
return ID_FORMATTER.get().parse(buildDir.getName()).getTime();
@@ -34,6 +34,12 @@ public interface RunAction extends Action {
*/
void onLoad();
/**
* Called when the action is aded to the {@link Run} object.
* @since 1.376
*/
void onAttached(Run r);
/**
* Called after the build is finished.
*/
@@ -27,6 +27,7 @@ import antlr.ANTLRException;
import hudson.Util;
import hudson.Extension;
import hudson.console.AnnotatedLargeText;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Cause;
@@ -35,9 +36,11 @@ import hudson.model.Item;
import hudson.model.Project;
import hudson.model.SCMedItem;
import hudson.model.AdministrativeMonitor;
import hudson.util.FlushProofOutputStream;
import hudson.util.StreamTaskListener;
import hudson.util.TimeUnit2;
import hudson.util.SequentialExecutionQueue;
import org.apache.commons.io.FileUtils;
import org.apache.commons.jelly.XMLOutput;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.DataBoundConstructor;
@@ -58,6 +61,9 @@ import java.util.logging.Logger;
import java.text.DateFormat;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerResponse;
import static java.util.logging.Level.*;
/**
* {@link Trigger} that checks for SCM updates periodically.
@@ -256,7 +262,63 @@ public class SCMTrigger extends Trigger<SCMedItem> {
}
/**
* Action object for {@link Project}. Used to display the polling log.
* Associated with {@link AbstractBuild} to show the polling log
* that triggered that build.
*
* @since 1.376
*/
public static class BuildAction implements Action {
public final AbstractBuild build;
public BuildAction(AbstractBuild build) {
this.build = build;
}
/**
* Polling log that triggered the build.
*/
public File getPollingLogFile() {
return new File(build.getRootDir(),"polling.log");
}
public String getIconFileName() {
return "clipboard.gif";
}
public String getDisplayName() {
return "Polling Log";
}
public String getUrlName() {
return "pollingLog";
}
/**
* Sends out the raw polling log output.
*/
public void doPollingLog(StaplerRequest req, StaplerResponse rsp) throws IOException {
rsp.setContentType("text/plain;charset=UTF-8");
// Prevent jelly from flushing stream so Content-Length header can be added afterwards
FlushProofOutputStream out = new FlushProofOutputStream(rsp.getCompressedOutputStream(req));
getPollingLogText().writeLogTo(0, out);
out.close();
}
public AnnotatedLargeText getPollingLogText() {
return new AnnotatedLargeText<BuildAction>(getPollingLogFile(), Charset.defaultCharset(), true, this);
}
/**
* Used from <tt>polling.jelly</tt> to write annotated polling log to the given output.
*/
public void writePollingLogTo(long offset, XMLOutput out) throws IOException {
// TODO: resurrect compressed log file support
getPollingLogText().writeHtmlTo(offset, out.asWriter());
}
}
/**
* Action object for {@link Project}. Used to display the last polling log.
*/
public final class SCMAction implements Action {
public AbstractProject<?,?> getOwner() {
@@ -384,7 +446,14 @@ public class SCMTrigger extends Trigger<SCMedItem> {
if(runPolling()) {
AbstractProject p = job.asProject();
String name = " #"+p.getNextBuildNumber();
if(p.scheduleBuild(p.getQuietPeriod(), new SCMTriggerCause(), additionalActions)) {
SCMTriggerCause cause;
try {
cause = new SCMTriggerCause(getLogFile());
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to parse the polling log",e);
cause = new SCMTriggerCause();
}
if(p.scheduleBuild(p.getQuietPeriod(), cause, additionalActions)) {
LOGGER.info("SCM changes detected in "+ job.getName()+". Triggering "+name);
} else {
LOGGER.info("SCM changes detected in "+ job.getName()+". Job is already in the queue");
@@ -412,6 +481,41 @@ public class SCMTrigger extends Trigger<SCMedItem> {
}
public static class SCMTriggerCause extends Cause {
/**
* Only used while ths cause is in the queue.
* Once attached to the build, we'll move this into a file to reduce the memory footprint.
*/
private String pollingLog;
public SCMTriggerCause(File logFile) throws IOException {
// TODO: charset of this log file?
this(FileUtils.readFileToString(logFile));
}
public SCMTriggerCause(String pollingLog) {
this.pollingLog = pollingLog;
}
/**
* @deprecated
* Use {@link #SCMTriggerCause(String)}.
*/
public SCMTriggerCause() {
this("");
}
@Override
public void onAddedTo(AbstractBuild build) {
try {
BuildAction a = new BuildAction(build);
FileUtils.writeStringToFile(a.getPollingLogFile(),pollingLog);
build.addAction(a);
} catch (IOException e) {
LOGGER.log(WARNING,"Failed to persist the polling log",e);
}
pollingLog = null;
}
@Override
public String getShortDescription() {
return Messages.SCMTrigger_SCMTriggerCause_ShortDescription();