diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml index df219b4..66c61eb 100644 --- a/.github/workflows/pr-review.yml +++ b/.github/workflows/pr-review.yml @@ -54,7 +54,7 @@ jobs: review: needs: check if: needs.check.outputs.allowed == 'true' - uses: adobe/aio-reusable-workflows/.github/workflows/pr-review.yml@main + uses: adobe/aio-reusable-workflows/.github/workflows/pr-review.yml@fix/pr-review-pedantic-and-human-replies with: pr_number: ${{ needs.check.outputs.pr_number }} head_sha: ${{ needs.check.outputs.head_sha }} diff --git a/lib/utils.js b/lib/utils.js index bf4ad04..d7019bd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -117,8 +117,40 @@ function formatAjvErrors (errors) { return stringErrors } +/** + * Computes the TTL in seconds from an expiry date or a number of seconds. + * Returns undefined if no expiry is provided. + * + * @private + * @param {Date|number} expiry a Date object or seconds from now + * @returns {number|undefined} TTL in seconds + */ +function computeTTL (expiry) { + if (expiry === undefined || expiry === null) return undefined + if (expiry instanceof Date) { + return Math.floor((expiry.getTime() - Date.now()) / 1000) + } + return expiry +} + +/** + * Truncates a value to the max allowed byte size for state storage. + * + * @private + * @param {string} value the value to sanitize + * @param {number} maxBytes max allowed size in bytes + * @returns {string} the value, truncated if necessary + */ +function sanitizeValue (value, maxBytes) { + const encoded = Buffer.from(value) + if (encoded.length <= maxBytes) return value + return encoded.slice(0, maxBytes).toString() +} + module.exports = { withHiddenFields, isInternalToAdobeRuntime, - formatAjvErrors + formatAjvErrors, + computeTTL, + sanitizeValue }