|
16 | 16 | import io.jenkins.plugins.pipelinegraphview.utils.PipelineStepApi;
|
17 | 17 | import io.jenkins.plugins.pipelinegraphview.utils.PipelineStepList;
|
18 | 18 | import java.io.IOException;
|
19 |
| -import java.io.Writer; |
20 | 19 | import java.util.ArrayList;
|
21 | 20 | import java.util.HashMap;
|
22 | 21 | import java.util.List;
|
|
28 | 27 | import org.kohsuke.stapler.StaplerRequest2;
|
29 | 28 | import org.kohsuke.stapler.StaplerResponse2;
|
30 | 29 | import org.kohsuke.stapler.WebMethod;
|
31 |
| -import org.kohsuke.stapler.framework.io.CharSpool; |
32 |
| -import org.kohsuke.stapler.framework.io.LineEndNormalizingWriter; |
33 | 30 | import org.kohsuke.stapler.verb.GET;
|
34 | 31 | import org.slf4j.Logger;
|
35 | 32 | import org.slf4j.LoggerFactory;
|
@@ -120,45 +117,42 @@ protected JSONObject getAllSteps() throws IOException {
|
120 | 117 | }
|
121 | 118 |
|
122 | 119 | @WebMethod(name = "log")
|
123 |
| - public HttpResponse getConsoleText(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException { |
| 120 | + public void getConsoleText(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException { |
124 | 121 | String nodeId = req.getParameter("nodeId");
|
125 | 122 | if (nodeId == null) {
|
126 | 123 | logger.error("'consoleText' was not passed 'nodeId'.");
|
127 |
| - return HttpResponses.errorJSON("Error getting console text"); |
| 124 | + rsp.getWriter().write("Error getting console text\n"); |
| 125 | + return; |
128 | 126 | }
|
129 | 127 | logger.debug("getConsoleText was passed node id '{}'.", nodeId);
|
130 | 128 | // This will be a step, so return its log output.
|
131 | 129 | AnnotatedLargeText<? extends FlowNode> logText = getLogForNode(nodeId);
|
| 130 | + if (logText != null) { |
| 131 | + logText.writeLogTo(0L, rsp.getOutputStream()); |
| 132 | + return; |
| 133 | + } |
132 | 134 |
|
133 |
| - long count = 0; |
| 135 | + // Potentially a stage, so get the log text for the stage. |
| 136 | + boolean foundLogs = false; |
134 | 137 | PipelineStepList steps = stepApi.getSteps(nodeId);
|
135 |
| - try (CharSpool spool = new CharSpool()) { |
136 |
| - |
137 |
| - for (PipelineStep step : steps.getSteps()) { |
138 |
| - AnnotatedLargeText<? extends FlowNode> logForNode = getLogForNode(String.valueOf(step.getId())); |
139 |
| - if (logForNode != null) { |
140 |
| - count += logForNode.writeLogTo(0, spool); |
141 |
| - } |
142 |
| - } |
143 |
| - |
144 |
| - if (count > 0) { |
145 |
| - rsp.setContentType("text/plain;charset=UTF-8"); |
146 |
| - try (Writer writer = rsp.getWriter()) { |
147 |
| - spool.flush(); |
148 |
| - spool.writeTo(new LineEndNormalizingWriter(writer)); |
149 |
| - } |
| 138 | + for (PipelineStep step : steps.getSteps()) { |
| 139 | + logText = getLogForNode(step.getId()); |
| 140 | + if (logText != null) { |
| 141 | + foundLogs = true; |
| 142 | + logText.writeLogTo(0L, rsp.getOutputStream()); |
150 | 143 | }
|
151 | 144 | }
|
152 |
| - |
153 |
| - if (logText != null) { |
154 |
| - return HttpResponses.text(PipelineNodeUtil.convertLogToString(logText)); |
| 145 | + if (!foundLogs) { |
| 146 | + rsp.getWriter().write("No logs found\n"); |
155 | 147 | }
|
156 |
| - return HttpResponses.text("No logs found"); |
157 | 148 | }
|
158 | 149 |
|
159 | 150 | /*
|
160 | 151 | * The default behavior of this functions differs from 'getConsoleOutput' in that it will use LOG_THRESHOLD from the end of the string.
|
161 | 152 | * Note: if 'startByte' is negative and falls outside of the console text then we will start from byte 0.
|
| 153 | + * |
| 154 | + * FIXME: This is not performant and needs to be re-written to not buffer in memory. Avoiding JSON for log text. |
| 155 | + * |
162 | 156 | * Example:
|
163 | 157 | * {
|
164 | 158 | * "startByte": 0,
|
@@ -215,7 +209,7 @@ protected JSONObject getConsoleOutputJson(String nodeId, Long requestStartByte)
|
215 | 209 | startByte = requestStartByte;
|
216 | 210 | }
|
217 | 211 | logger.debug("Returning '{}' bytes from 'getConsoleOutput'.", textLength - startByte);
|
218 |
| - text = PipelineNodeUtil.convertLogToString(logText, startByte, true); |
| 212 | + text = PipelineNodeUtil.convertLogToString(logText, startByte); |
219 | 213 | endByte = textLength;
|
220 | 214 | }
|
221 | 215 | // If has an exception, return the exception text (inc. stacktrace).
|
|
0 commit comments