Skip to content

Commit

Permalink
move logic to the flow execution analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdeitke committed Feb 15, 2025
1 parent 1298762 commit 88f7eb2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
class FlowExecutionAnalyzer {
private static final Logger LOGGER = Logger.getLogger(FlowExecutionAnalyzer.class.getName());
private static final String TRUNCATED_MESSAGE = "\n\nOutput truncated.";
private static final String TRUNCATED_MESSAGE_BUILD_LOG = "Build log truncated.\n\n";
private static final int MAX_MESSAGE_SIZE_TO_CHECKS_API = 65_535;

private final Run<?, ?> run;
Expand Down Expand Up @@ -217,7 +218,7 @@ private static String getLog(final FlowNode flowNode, final int maxMessageSize)
return new TruncatedString.Builder()
.setChunkOnNewlines()
.setTruncateStart()
.withTruncationText(TRUNCATED_MESSAGE)
.withTruncationText(TRUNCATED_MESSAGE_BUILD_LOG)
.addText(log)
.build()
.build(maxMessageSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,54 @@ public void shouldPublishStageDetailsWithoutLogsIfRequested() {
});
}

/**
* Test that log messages are properly truncated when they exceed the maximum size limit.
*/
@Test
public void shouldTruncateLogsWhenExceedingMaxSize() {
getProperties().setApplicable(true);
getProperties().setSkipped(false);
getProperties().setName("Test Status");
getProperties().setSuppressLogs(false);
WorkflowJob job = createPipeline();

// Create a pipeline that generates a large log output
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " stage('Large Log Stage') {\n"
+ " // Generate a large log by executing shell commands\n"
+ " sh '''\n"
+ " for i in {1..1000}; do\n"
+ " echo \"Line $i: $(date) - This is a very long log line that will be repeated many times to test truncation. Adding some extra system information here.\"\n"
+ " done\n"
+ " exit 1\n"
+ " '''\n"
+ " error('Pipeline failed with large logs')\n"
+ " }\n"
+ "}", true));

buildWithResult(job, Result.FAILURE);

List<ChecksDetails> checksDetails = getFactory().getPublishedChecks();

// Get the final check details which should contain the truncated logs
ChecksDetails details = checksDetails.get(checksDetails.size() - 1);
assertThat(details.getStatus()).isEqualTo(ChecksStatus.COMPLETED);
assertThat(details.getConclusion()).isEqualTo(ChecksConclusion.FAILURE);
assertThat(details.getOutput()).isPresent().get().satisfies(output -> {
assertThat(output.getSummary()).isPresent().get().satisfies(summary -> {
// Verify the log section exists and is truncated
assertThat(summary).contains("<details>");
assertThat(summary).contains("Build log");
assertThat(summary).contains("Build log truncated.");
// Verify the truncation message appears at the start of the log section to show that truncation occurred at start
assertThat(summary).matches(Pattern.compile(".*<summary>Build log</summary>\\s+\\n```\\s*\\nBuild log truncated.\\n\\n.*", Pattern.DOTALL));
// Verify the total size is within limits
assertThat(summary.length()).isLessThanOrEqualTo(65535);
});
});
}

/**
* Validates that a simple successful pipeline works.
*/
Expand Down

0 comments on commit 88f7eb2

Please sign in to comment.