From e8ef132d6df98ed982188e460ebb3b5d4ef3a9cd Mon Sep 17 00:00:00 2001 From: Peter Evans <18365890+peter-evans@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:23:50 +0100 Subject: [PATCH] fix: truncate body when exceeds max length (#1711) --- dist/index.js | 12 +++++++++++- src/main.ts | 14 +++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index e60f354..8cbfa40 100644 --- a/dist/index.js +++ b/dist/index.js @@ -45,6 +45,15 @@ const fs = __importStar(__nccwpck_require__(7147)); const util = __importStar(__nccwpck_require__(3837)); const utils = __importStar(__nccwpck_require__(918)); const util_1 = __nccwpck_require__(3837); +function truncateBody(body) { + // 65536 characters is the maximum allowed for issues. + const truncateWarning = '...*[Issue body truncated]*'; + if (body.length > 65536) { + core.warning(`Issue body is too long. Truncating to 65536 characters.`); + return body.substring(0, 65536 - truncateWarning.length) + truncateWarning; + } + return body; +} function run() { return __awaiter(this, void 0, void 0, function* () { try { @@ -64,9 +73,10 @@ function run() { // Check the file exists if (yield util.promisify(fs.exists)(inputs.contentFilepath)) { // Fetch the file content - const fileContent = yield fs.promises.readFile(inputs.contentFilepath, { + let fileContent = yield fs.promises.readFile(inputs.contentFilepath, { encoding: 'utf8' }); + fileContent = truncateBody(fileContent); const issueNumber = yield (() => __awaiter(this, void 0, void 0, function* () { if (inputs.issueNumber) { // Update an existing issue diff --git a/src/main.ts b/src/main.ts index 4c5bb7f..dbc5e48 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,16 @@ import * as util from 'util' import * as utils from './utils' import {inspect} from 'util' +function truncateBody(body: string) { + // 65536 characters is the maximum allowed for issues. + const truncateWarning = '...*[Issue body truncated]*' + if (body.length > 65536) { + core.warning(`Issue body is too long. Truncating to 65536 characters.`) + return body.substring(0, 65536 - truncateWarning.length) + truncateWarning + } + return body +} + async function run(): Promise { try { const inputs = { @@ -26,10 +36,12 @@ async function run(): Promise { // Check the file exists if (await util.promisify(fs.exists)(inputs.contentFilepath)) { // Fetch the file content - const fileContent = await fs.promises.readFile(inputs.contentFilepath, { + let fileContent = await fs.promises.readFile(inputs.contentFilepath, { encoding: 'utf8' }) + fileContent = truncateBody(fileContent) + const issueNumber = await (async (): Promise => { if (inputs.issueNumber) { // Update an existing issue