Skip to content

Commit ecea5c1

Browse files
authored
Modify chunking code to make it more clearly resilient to being too big (#139)
1 parent f103b58 commit ecea5c1

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/postGithubComments.ts

+31-15
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,45 @@ else {
8989
const trunctationSuffix = `\n:error: Truncated - see log for full output :error:`;
9090

9191
// GH caps the maximum body length, so paginate if necessary
92+
const maxCommentLength = 65535;
93+
9294
const bodyChunks: string[] = [];
9395
let chunk = initialHeader;
94-
for (const output of outputs) {
95-
if (chunk.length + output.length + closeDetails.length > 65535) {
96-
if (chunk === initialHeader || chunk === continuationHeader) {
97-
// output is too long and bumping it to the next comment won't help
98-
console.log("Truncating output to fit in GH comment");
99-
chunk += output.substring(0, 65535 - chunk.length - closeDetails.length - trunctationSuffix.length);
100-
chunk += trunctationSuffix;
101-
chunk += closeDetails;
102-
bodyChunks.push(chunk);
103-
chunk = continuationHeader;
104-
continue; // Specifically, don't append output below
105-
}
10696

97+
for (let i = 0; i < outputs.length;) {
98+
const output = outputs[i];
99+
if ((chunk.length + output.length + closeDetails.length) < maxCommentLength) {
100+
// Output still fits within chunk; add and continue.
101+
chunk += output;
102+
i++;
103+
continue;
104+
}
105+
106+
// The output is too long to fit in the current chunk.
107+
108+
if (chunk === initialHeader || chunk === continuationHeader) {
109+
// We only have a header, but the output still doesn't fit. Truncate and continue.
110+
console.log("Truncating output to fit in GH comment");
111+
chunk += output.slice(0, maxCommentLength - chunk.length - closeDetails.length - trunctationSuffix.length);
112+
chunk += trunctationSuffix;
107113
chunk += closeDetails;
108114
bodyChunks.push(chunk);
109115
chunk = continuationHeader;
116+
i++;
117+
continue;
110118
}
111-
chunk += output;
119+
120+
// Close the chunk and try the same output again.
121+
chunk += closeDetails;
122+
bodyChunks.push(chunk);
123+
chunk = continuationHeader;
124+
}
125+
126+
if (chunk !== initialHeader && chunk !== continuationHeader) {
127+
chunk += closeDetails;
128+
bodyChunks.push(chunk);
112129
}
113-
chunk += closeDetails;
114-
bodyChunks.push(chunk);
130+
115131

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

0 commit comments

Comments
 (0)