Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-37369] Expose the last computation for computed folders
  • Loading branch information
stephenc committed Feb 27, 2017
1 parent 359ae1f commit 8eac7c1
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 2 deletions.
Expand Up @@ -43,6 +43,7 @@
import hudson.model.Node;
import hudson.model.Queue;
import hudson.model.ResourceList;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.model.TopLevelItem;
import hudson.model.listeners.ItemListener;
Expand Down Expand Up @@ -594,6 +595,36 @@ public FolderComputation<I> getComputation() {
return computation;
}

@Restricted(NoExternalUse.class) // used by stapler only
public PseudoRun<I> getLastSuccessfulBuild() {
FolderComputation<I> computation = getComputation();
Result result = computation.getResult();
if (result != null && Result.UNSTABLE.isWorseOrEqualTo(result)) {
return new PseudoRun(computation);
}
return null;
}

@Restricted(NoExternalUse.class) // used by stapler only
public PseudoRun<I> getLastStableBuild() {
FolderComputation<I> computation = getComputation();
Result result = computation.getResult();
if (result != null && Result.SUCCESS.isWorseOrEqualTo(result)) {
return new PseudoRun(computation);
}
return null;
}

@Restricted(NoExternalUse.class) // used by stapler only
public PseudoRun<I> getLastFailedBuild() {
FolderComputation<I> computation = getComputation();
Result result = computation.getResult();
if (result != null && Result.UNSTABLE.isBetterThan(result)) {
return new PseudoRun(computation);
}
return null;
}

/**
* {@inheritDoc}
*/
Expand Down
Expand Up @@ -25,12 +25,14 @@
package com.cloudbees.hudson.plugins.folder.computed;

import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import com.jcraft.jzlib.GZIPInputStream;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.AbortException;
import hudson.BulkChange;
import hudson.Util;
import hudson.XmlFile;
import hudson.console.AnnotatedLargeText;
import hudson.console.PlainTextConsoleOutputStream;
import hudson.model.Action;
import hudson.model.Actionable;
import hudson.model.BallColor;
Expand All @@ -51,9 +53,12 @@
import hudson.util.AlternativeUiTextProvider.Message;
import hudson.util.StreamTaskListener;
import hudson.util.io.ReopenableRotatingFileOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -69,7 +74,10 @@
import net.jcip.annotations.GuardedBy;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.jelly.XMLOutput;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.framework.io.ByteBuffer;

/**
Expand Down Expand Up @@ -346,6 +354,48 @@ public void writeWholeLogTo(@Nonnull OutputStream out) throws IOException, Inter
} while (!logText.isComplete());
}

/**
* Sends out the raw console output.
*/
public void doConsoleText(StaplerRequest req, StaplerResponse rsp) throws IOException {
rsp.setContentType("text/plain;charset=UTF-8");
PlainTextConsoleOutputStream out = new PlainTextConsoleOutputStream(rsp.getCompressedOutputStream(req));
InputStream input = getLogInputStream();
try {
IOUtils.copy(input, out);
out.flush();
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(out);
}
}

/**
* Returns an input stream that reads from the log file.
* It will use a gzip-compressed log file (log.gz) if that exists.
*
* @return An input stream from the log file.
* If the log file does not exist, the error message will be returned to the output.
* @throws IOException if things go wrong
*/
@Nonnull
public InputStream getLogInputStream() throws IOException {
File logFile = getLogFile();

if (logFile.exists()) {
// Checking if a ".gz" file was return
FileInputStream fis = new FileInputStream(logFile);
if (logFile.getName().endsWith(".gz")) {
return new GZIPInputStream(fis);
} else {
return fis;
}
}

String message = "No such file: " + logFile;
return new ByteArrayInputStream(message.getBytes(Charsets.UTF_8));
}

@CheckForNull
public Result getResult() {
return result;
Expand Down
@@ -0,0 +1,151 @@
package com.cloudbees.hudson.plugins.folder.computed;

import hudson.Functions;
import hudson.Util;
import hudson.model.Actionable;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TopLevelItem;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.StaplerFallback;
import org.kohsuke.stapler.StaplerRequest;

/**
* A fake {@link Run} used to render last build information via Stapler and Jelly
*/
@Restricted(NoExternalUse.class) // used by stapler / jelly only
public class PseudoRun<I extends TopLevelItem> extends Actionable implements StaplerFallback {
private final FolderComputation<I> computation;

PseudoRun(FolderComputation<I> computation) {
this.computation = computation;
}

public String getDisplayName() {
return "log";
}

public RunUrl decompose(StaplerRequest req) {
List<Ancestor> ancestors = req.getAncestors();

// find the first and last Run instances
Ancestor f = null, l = null;
for (Ancestor anc : ancestors) {
if (anc.getObject() instanceof PseudoRun) {
if (f == null) f = anc;
l = anc;
}
}
if (l == null) return null; // there was no Run object

String base = l.getUrl();

return new RunUrl(base);

}

/**
* Gets the string that says how long since this build has started.
*
* @return string like "3 minutes" "1 day" etc.
*/
@Nonnull
public String getTimestampString() {
long duration = new GregorianCalendar().getTimeInMillis() - computation.getTimestamp().getTimeInMillis();
return Util.getPastTimeString(duration);
}

/**
* Returns the timestamp formatted in xs:dateTime.
*
* @return the timestamp formatted in xs:dateTime.
*/
@Nonnull
public String getTimestampString2() {
return Util.XS_DATETIME_FORMATTER.format(computation.getTimestamp());
}

@CheckForNull
public Result getResult() {
return computation.getResult();
}

@Nonnull
public Calendar getTimestamp() {
return computation.getTimestamp();
}

@Nonnull
public String getDurationString() {
return computation.getDurationString();
}

@Override
public String getSearchUrl() {
return computation.getSearchUrl();
}

@Nonnull
public File getLogFile() {
return computation.getLogFile();
}

@Override
public Object getStaplerFallback() {
return computation;
}

public HttpResponse doIndex(StaplerRequest request) {
return HttpResponses.redirectTo(Jenkins.getActiveInstance().getRootUrlFromRequest() + computation.getUrl() );
}

public HttpResponse doConsole(StaplerRequest request) {
return HttpResponses.redirectTo(Jenkins.getActiveInstance().getRootUrlFromRequest() + computation.getUrl() + "console");
}

public HttpResponse doConsoleText(StaplerRequest request) {
return HttpResponses.redirectTo(Jenkins.getActiveInstance().getRootUrlFromRequest() + computation.getUrl() + "consoleText");
}

@Restricted(NoExternalUse.class)
public static final class RunUrl {
private final String base;


public RunUrl(String base) {
this.base = base;
}

public String getBaseUrl() {
return base;
}

/**
* Returns the same page in the next build.
*/
public String getNextBuildUrl() {
return null;
}

/**
* Returns the same page in the previous build.
*/
public String getPreviousBuildUrl() {
return null;
}

}

}
Expand Up @@ -31,7 +31,9 @@ THE SOFTWARE.
<j:if test="${h.hasPermission(it, it.BUILD) and it.buildable}">
<l:task icon="images/24x24/clock.png" href="${rootURL}/${it.url}build?delay=0" post="true" title="${%Run Now}"/>
</j:if>
<l:task icon="images/24x24/terminal.png" href="${rootURL}/${computation.url}console" title="${%Log}"/>
<l:task icon="images/24x24/terminal.png" href="${rootURL}/${computation.url}console" title="${%Log}">
<l:task href="${rootURL}/${computation.url}consoleText" icon="icon-document icon-md" title="${%View as plain text}"/>
</l:task>
<j:if test="${it.hasEvents}">
<l:task icon="images/24x24/monitor.png" href="${rootURL}/${computation.url}events" title="${%Events}"/>
</j:if>
Expand Down
@@ -1,4 +1,5 @@
ComputedFolder.already_computing=Already computing
FolderComputation.DisplayName=Folder Computation
ThrottleComputationQueueTaskDispatcher.MaxConcurrentIndexing=At maximum indexing capacity
PeriodicFolderTrigger.DisplayName=Periodically if not otherwise run
PeriodicFolderTrigger.DisplayName=Periodically if not otherwise run
PseudoRun.DisplayName=log
@@ -0,0 +1,37 @@
<!--
The MIT License
Copyright (c) 2017 CloudBees, 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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
<l:header/>
<l:side-panel>
<l:tasks>
<j:set var="buildUrl" value="${it.decompose(request)}"/>
<p:console-link/>
<st:include page="actions.jelly"/>
<t:actions actions="${it.transientActions}"/>
</l:tasks>
</l:side-panel>
</j:jelly>

0 comments on commit 8eac7c1

Please sign in to comment.