Skip to content

Modify chunking code to make it more clearly resilient to being too big #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions src/postGithubComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,45 @@ else {
const trunctationSuffix = `\n:error: Truncated - see log for full output :error:`;

// GH caps the maximum body length, so paginate if necessary
const maxCommentLength = 65535;

const bodyChunks: string[] = [];
let chunk = initialHeader;
for (const output of outputs) {
if (chunk.length + output.length + closeDetails.length > 65535) {
if (chunk === initialHeader || chunk === continuationHeader) {
// output is too long and bumping it to the next comment won't help
console.log("Truncating output to fit in GH comment");
chunk += output.substring(0, 65535 - chunk.length - closeDetails.length - trunctationSuffix.length);
chunk += trunctationSuffix;
chunk += closeDetails;
bodyChunks.push(chunk);
chunk = continuationHeader;
continue; // Specifically, don't append output below
}

for (let i = 0; i < outputs.length;) {
const output = outputs[i];
if ((chunk.length + output.length + closeDetails.length) < maxCommentLength) {
// Output still fits within chunk; add and continue.
chunk += output;
i++;
continue;
}

// The output is too long to fit in the current chunk.

if (chunk === initialHeader || chunk === continuationHeader) {
// We only have a header, but the output still doesn't fit. Truncate and continue.
console.log("Truncating output to fit in GH comment");
chunk += output.slice(0, maxCommentLength - chunk.length - closeDetails.length - trunctationSuffix.length);
chunk += trunctationSuffix;
chunk += closeDetails;
bodyChunks.push(chunk);
chunk = continuationHeader;
i++;
continue;
}
chunk += output;

// Close the chunk and try the same output again.
chunk += closeDetails;
bodyChunks.push(chunk);
chunk = continuationHeader;
}

if (chunk !== initialHeader && chunk !== continuationHeader) {
chunk += closeDetails;
bodyChunks.push(chunk);
}
chunk += closeDetails;
bodyChunks.push(chunk);


for (const chunk of bodyChunks) {
console.log(`Chunk of size ${chunk.length}`);
Expand Down