feat: truncate short description exceeding the byte limit (#143)

* feat: truncate short description exceeding the byte limit

* fix tests
This commit is contained in:
Peter Evans 2023-04-07 09:16:46 +09:00 committed by GitHub
parent 3ba533f0ff
commit 4b1a4bb484
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 80 additions and 47 deletions

View file

@ -1,4 +1,4 @@
import {completeRelativeUrls, truncateToBytes} from '../src/readme-helper' import {completeRelativeUrls} from '../src/readme-helper'
describe('complete relative urls tests', () => { describe('complete relative urls tests', () => {
const GITHUB_SERVER_URL = process.env['GITHUB_SERVER_URL'] const GITHUB_SERVER_URL = process.env['GITHUB_SERVER_URL']
@ -333,12 +333,3 @@ describe('complete relative urls tests', () => {
) )
}) })
}) })
describe('truncate to bytes tests', () => {
test('unicode aware truncation to a number of bytes', async () => {
expect(truncateToBytes('test string to be truncated', 10)).toEqual(
'test strin'
)
expect(truncateToBytes('😀😁😂🤣😃😄😅', 10)).toEqual('😀😁')
})
})

View file

@ -0,0 +1,10 @@
import {truncateToBytes} from '../src/utils'
describe('truncate to bytes tests', () => {
test('unicode aware truncation to a number of bytes', async () => {
expect(truncateToBytes('test string to be truncated', 10)).toEqual(
'test strin'
)
expect(truncateToBytes('😀😁😂🤣😃😄😅', 10)).toEqual('😀😁')
})
})

57
dist/index.js vendored
View file

@ -42,7 +42,6 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.updateRepositoryDescription = exports.getToken = void 0; exports.updateRepositoryDescription = exports.getToken = void 0;
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const fetch = __importStar(__nccwpck_require__(467)); const fetch = __importStar(__nccwpck_require__(467));
const DESCRIPTION_MAX_CHARS = 100;
function getToken(username, password) { function getToken(username, password) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const body = { const body = {
@ -69,7 +68,7 @@ function updateRepositoryDescription(token, repository, description, fullDescrip
full_description: fullDescription full_description: fullDescription
}; };
if (description) { if (description) {
body['description'] = description.slice(0, DESCRIPTION_MAX_CHARS); body['description'] = description;
} }
yield fetch(`https://hub.docker.com/v2/repositories/${repository}`, { yield fetch(`https://hub.docker.com/v2/repositories/${repository}`, {
method: 'patch', method: 'patch',
@ -238,12 +237,9 @@ const core = __importStar(__nccwpck_require__(2186));
const inputHelper = __importStar(__nccwpck_require__(5480)); const inputHelper = __importStar(__nccwpck_require__(5480));
const dockerhubHelper = __importStar(__nccwpck_require__(1812)); const dockerhubHelper = __importStar(__nccwpck_require__(1812));
const readmeHelper = __importStar(__nccwpck_require__(3367)); const readmeHelper = __importStar(__nccwpck_require__(3367));
const utils = __importStar(__nccwpck_require__(918));
const util_1 = __nccwpck_require__(3837); const util_1 = __nccwpck_require__(3837);
function getErrorMessage(error) { const SHORT_DESCRIPTION_MAX_BYTES = 100;
if (error instanceof Error)
return error.message;
return String(error);
}
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
@ -254,6 +250,11 @@ function run() {
core.info('Reading description source file'); core.info('Reading description source file');
const readmeContent = yield readmeHelper.getReadmeContent(inputs.readmeFilepath, inputs.enableUrlCompletion, inputs.imageExtensions); const readmeContent = yield readmeHelper.getReadmeContent(inputs.readmeFilepath, inputs.enableUrlCompletion, inputs.imageExtensions);
core.debug(readmeContent); core.debug(readmeContent);
// Truncate the short description if it is too long
const truncatedShortDescription = utils.truncateToBytes(inputs.shortDescription, SHORT_DESCRIPTION_MAX_BYTES);
if (truncatedShortDescription.length !== inputs.shortDescription.length) {
core.warning(`The short description exceeds DockerHub's limit and has been truncated to ${SHORT_DESCRIPTION_MAX_BYTES} bytes.`);
}
// Acquire a token for the Docker Hub API // Acquire a token for the Docker Hub API
core.info('Acquiring token'); core.info('Acquiring token');
const token = yield dockerhubHelper.getToken(inputs.username, inputs.password); const token = yield dockerhubHelper.getToken(inputs.username, inputs.password);
@ -264,7 +265,7 @@ function run() {
} }
catch (error) { catch (error) {
core.debug((0, util_1.inspect)(error)); core.debug((0, util_1.inspect)(error));
core.setFailed(getErrorMessage(error)); core.setFailed(utils.getErrorMessage(error));
} }
}); });
} }
@ -311,10 +312,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.completeRelativeUrls = exports.getReadmeContent = exports.truncateToBytes = exports.ENABLE_URL_COMPLETION_DEFAULT = exports.IMAGE_EXTENSIONS_DEFAULT = exports.README_FILEPATH_DEFAULT = void 0; exports.completeRelativeUrls = exports.getReadmeContent = exports.ENABLE_URL_COMPLETION_DEFAULT = exports.IMAGE_EXTENSIONS_DEFAULT = exports.README_FILEPATH_DEFAULT = void 0;
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const fs = __importStar(__nccwpck_require__(7147)); const fs = __importStar(__nccwpck_require__(7147));
const unicodeSubstring = __nccwpck_require__(6986); const utils = __importStar(__nccwpck_require__(918));
exports.README_FILEPATH_DEFAULT = './README.md'; exports.README_FILEPATH_DEFAULT = './README.md';
exports.IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp'; exports.IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp';
exports.ENABLE_URL_COMPLETION_DEFAULT = false; exports.ENABLE_URL_COMPLETION_DEFAULT = false;
@ -323,14 +324,6 @@ const REPOSITORY_URL = `${process.env['GITHUB_SERVER_URL']}/${process.env['GITHU
const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/`; const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/`;
const RAW_PREFIX = `${REPOSITORY_URL}/raw/${process.env['GITHUB_REF_NAME']}/`; const RAW_PREFIX = `${REPOSITORY_URL}/raw/${process.env['GITHUB_REF_NAME']}/`;
const MAX_BYTES = 25000; const MAX_BYTES = 25000;
function truncateToBytes(s, n) {
let len = n;
while (Buffer.byteLength(s) > n) {
s = unicodeSubstring(s, 0, len--);
}
return s;
}
exports.truncateToBytes = truncateToBytes;
function getReadmeContent(readmeFilepath, enableUrlCompletion, imageExtensions) { function getReadmeContent(readmeFilepath, enableUrlCompletion, imageExtensions) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
// Fetch the readme content // Fetch the readme content
@ -338,7 +331,7 @@ function getReadmeContent(readmeFilepath, enableUrlCompletion, imageExtensions)
encoding: 'utf8' encoding: 'utf8'
}); });
readmeContent = completeRelativeUrls(readmeContent, readmeFilepath, enableUrlCompletion, imageExtensions); readmeContent = completeRelativeUrls(readmeContent, readmeFilepath, enableUrlCompletion, imageExtensions);
const truncatedReadmeContent = truncateToBytes(readmeContent, MAX_BYTES); const truncatedReadmeContent = utils.truncateToBytes(readmeContent, MAX_BYTES);
if (truncatedReadmeContent.length !== readmeContent.length) { if (truncatedReadmeContent.length !== readmeContent.length) {
core.warning(`The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.`); core.warning(`The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.`);
} }
@ -446,6 +439,32 @@ function getRelativeUrlRules() {
} }
/***/ }),
/***/ 918:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.truncateToBytes = exports.getErrorMessage = void 0;
const unicodeSubstring = __nccwpck_require__(6986);
function getErrorMessage(error) {
if (error instanceof Error)
return error.message;
return String(error);
}
exports.getErrorMessage = getErrorMessage;
function truncateToBytes(s, n) {
let len = n;
while (Buffer.byteLength(s) > n) {
s = unicodeSubstring(s, 0, len--);
}
return s;
}
exports.truncateToBytes = truncateToBytes;
/***/ }), /***/ }),
/***/ 7351: /***/ 7351:

View file

@ -1,8 +1,6 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as fetch from 'node-fetch' import * as fetch from 'node-fetch'
const DESCRIPTION_MAX_CHARS = 100
export async function getToken( export async function getToken(
username: string, username: string,
password: string password: string
@ -36,7 +34,7 @@ export async function updateRepositoryDescription(
full_description: fullDescription full_description: fullDescription
} }
if (description) { if (description) {
body['description'] = description.slice(0, DESCRIPTION_MAX_CHARS) body['description'] = description
} }
await fetch(`https://hub.docker.com/v2/repositories/${repository}`, { await fetch(`https://hub.docker.com/v2/repositories/${repository}`, {
method: 'patch', method: 'patch',

View file

@ -2,12 +2,10 @@ import * as core from '@actions/core'
import * as inputHelper from './input-helper' import * as inputHelper from './input-helper'
import * as dockerhubHelper from './dockerhub-helper' import * as dockerhubHelper from './dockerhub-helper'
import * as readmeHelper from './readme-helper' import * as readmeHelper from './readme-helper'
import * as utils from './utils'
import {inspect} from 'util' import {inspect} from 'util'
function getErrorMessage(error: unknown) { const SHORT_DESCRIPTION_MAX_BYTES = 100
if (error instanceof Error) return error.message
return String(error)
}
async function run(): Promise<void> { async function run(): Promise<void> {
try { try {
@ -25,6 +23,17 @@ async function run(): Promise<void> {
) )
core.debug(readmeContent) core.debug(readmeContent)
// Truncate the short description if it is too long
const truncatedShortDescription = utils.truncateToBytes(
inputs.shortDescription,
SHORT_DESCRIPTION_MAX_BYTES
)
if (truncatedShortDescription.length !== inputs.shortDescription.length) {
core.warning(
`The short description exceeds DockerHub's limit and has been truncated to ${SHORT_DESCRIPTION_MAX_BYTES} bytes.`
)
}
// Acquire a token for the Docker Hub API // Acquire a token for the Docker Hub API
core.info('Acquiring token') core.info('Acquiring token')
const token = await dockerhubHelper.getToken( const token = await dockerhubHelper.getToken(
@ -42,7 +51,7 @@ async function run(): Promise<void> {
core.info('Request successful') core.info('Request successful')
} catch (error) { } catch (error) {
core.debug(inspect(error)) core.debug(inspect(error))
core.setFailed(getErrorMessage(error)) core.setFailed(utils.getErrorMessage(error))
} }
} }

View file

@ -1,6 +1,6 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as fs from 'fs' import * as fs from 'fs'
import unicodeSubstring = require('unicode-substring') import * as utils from './utils'
export const README_FILEPATH_DEFAULT = './README.md' export const README_FILEPATH_DEFAULT = './README.md'
export const IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp' export const IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp'
@ -28,14 +28,6 @@ type Rule = {
absUrlPrefix: string absUrlPrefix: string
} }
export function truncateToBytes(s: string, n: number): string {
let len = n
while (Buffer.byteLength(s) > n) {
s = unicodeSubstring(s, 0, len--)
}
return s
}
export async function getReadmeContent( export async function getReadmeContent(
readmeFilepath: string, readmeFilepath: string,
enableUrlCompletion: boolean, enableUrlCompletion: boolean,
@ -53,7 +45,7 @@ export async function getReadmeContent(
imageExtensions imageExtensions
) )
const truncatedReadmeContent = truncateToBytes(readmeContent, MAX_BYTES) const truncatedReadmeContent = utils.truncateToBytes(readmeContent, MAX_BYTES)
if (truncatedReadmeContent.length !== readmeContent.length) { if (truncatedReadmeContent.length !== readmeContent.length) {
core.warning( core.warning(
`The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.` `The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.`

14
src/utils.ts Normal file
View file

@ -0,0 +1,14 @@
import unicodeSubstring = require('unicode-substring')
export function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message
return String(error)
}
export function truncateToBytes(s: string, n: number): string {
let len = n
while (Buffer.byteLength(s) > n) {
s = unicodeSubstring(s, 0, len--)
}
return s
}