diff --git a/core/src/main/java/hudson/model/Cause.java b/core/src/main/java/hudson/model/Cause.java index 44d2f3c6e0..d296f27dff 100644 --- a/core/src/main/java/hudson/model/Cause.java +++ b/core/src/main/java/hudson/model/Cause.java @@ -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. diff --git a/core/src/main/java/hudson/model/CauseAction.java b/core/src/main/java/hudson/model/CauseAction.java index 0baed2c71c..af2b9f6bc2 100644 --- a/core/src/main/java/hudson/model/CauseAction.java +++ b/core/src/main/java/hudson/model/CauseAction.java @@ -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 otherActions) { CauseAction existing = item.getAction(CauseAction.class); if (existing!=null) { diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index e05570d58a..9978e343a7 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -266,6 +266,13 @@ public abstract class Run ,RunT extends Run { } /** - * 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(getPollingLogFile(), Charset.defaultCharset(), true, this); + } + + /** + * Used from polling.jelly 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 { 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 { } 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(); diff --git a/core/src/main/resources/hudson/triggers/SCMTrigger/BuildAction/index.jelly b/core/src/main/resources/hudson/triggers/SCMTrigger/BuildAction/index.jelly new file mode 100644 index 0000000000..c74a90d49a --- /dev/null +++ b/core/src/main/resources/hudson/triggers/SCMTrigger/BuildAction/index.jelly @@ -0,0 +1,47 @@ + + + + + + + +

${%Polling Log}

+ + + ${%View as plain text} + + + +

${%blurb}

+ +
+        
+        ${it.writePollingLogTo(0,output)}
+      
+
+
+
diff --git a/core/src/main/resources/hudson/triggers/SCMTrigger/BuildAction/index.properties b/core/src/main/resources/hudson/triggers/SCMTrigger/BuildAction/index.properties new file mode 100644 index 0000000000..1df279b00e --- /dev/null +++ b/core/src/main/resources/hudson/triggers/SCMTrigger/BuildAction/index.properties @@ -0,0 +1,2 @@ +blurb=\ + This page captures the polling log that triggered this build. \ No newline at end of file diff --git a/core/src/main/resources/hudson/triggers/SCMTrigger/SCMAction/index.properties b/core/src/main/resources/hudson/triggers/SCMTrigger/SCMAction/index.properties index e7cce3d584..783d886ff5 100644 --- a/core/src/main/resources/hudson/triggers/SCMTrigger/SCMAction/index.properties +++ b/core/src/main/resources/hudson/triggers/SCMTrigger/SCMAction/index.properties @@ -20,4 +20,4 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -title=Last {0} \ No newline at end of file +title={0} \ No newline at end of file diff --git a/core/src/main/resources/hudson/triggers/SCMTrigger/SCMTriggerCause/description.jelly b/core/src/main/resources/hudson/triggers/SCMTrigger/SCMTriggerCause/description.jelly new file mode 100644 index 0000000000..12f659cb7b --- /dev/null +++ b/core/src/main/resources/hudson/triggers/SCMTrigger/SCMTriggerCause/description.jelly @@ -0,0 +1,26 @@ + + + ${it.shortDescription} +