Skip to content

Commit

Permalink
fix: truncate body when exceeds max length (#1711)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans authored Sep 24, 2024
1 parent 60e5747 commit e8ef132
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 11 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
try {
const inputs = {
Expand All @@ -26,10 +36,12 @@ async function run(): Promise<void> {
// 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<number> => {
if (inputs.issueNumber) {
// Update an existing issue
Expand Down

0 comments on commit e8ef132

Please sign in to comment.