Skip to content

Commit 4b1a4bb

Browse files
authored
feat: truncate short description exceeding the byte limit (#143)
* feat: truncate short description exceeding the byte limit * fix tests
1 parent 3ba533f commit 4b1a4bb

File tree

7 files changed

+80
-47
lines changed

7 files changed

+80
-47
lines changed

__test__/readme-helper.unit.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {completeRelativeUrls, truncateToBytes} from '../src/readme-helper'
1+
import {completeRelativeUrls} from '../src/readme-helper'
22

33
describe('complete relative urls tests', () => {
44
const GITHUB_SERVER_URL = process.env['GITHUB_SERVER_URL']
@@ -333,12 +333,3 @@ describe('complete relative urls tests', () => {
333333
)
334334
})
335335
})
336-
337-
describe('truncate to bytes tests', () => {
338-
test('unicode aware truncation to a number of bytes', async () => {
339-
expect(truncateToBytes('test string to be truncated', 10)).toEqual(
340-
'test strin'
341-
)
342-
expect(truncateToBytes('😀😁😂🤣😃😄😅', 10)).toEqual('😀😁')
343-
})
344-
})

__test__/utils.unit.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {truncateToBytes} from '../src/utils'
2+
3+
describe('truncate to bytes tests', () => {
4+
test('unicode aware truncation to a number of bytes', async () => {
5+
expect(truncateToBytes('test string to be truncated', 10)).toEqual(
6+
'test strin'
7+
)
8+
expect(truncateToBytes('😀😁😂🤣😃😄😅', 10)).toEqual('😀😁')
9+
})
10+
})

dist/index.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
4242
exports.updateRepositoryDescription = exports.getToken = void 0;
4343
const core = __importStar(__nccwpck_require__(2186));
4444
const fetch = __importStar(__nccwpck_require__(467));
45-
const DESCRIPTION_MAX_CHARS = 100;
4645
function getToken(username, password) {
4746
return __awaiter(this, void 0, void 0, function* () {
4847
const body = {
@@ -69,7 +68,7 @@ function updateRepositoryDescription(token, repository, description, fullDescrip
6968
full_description: fullDescription
7069
};
7170
if (description) {
72-
body['description'] = description.slice(0, DESCRIPTION_MAX_CHARS);
71+
body['description'] = description;
7372
}
7473
yield fetch(`https://hub.docker.com/v2/repositories/${repository}`, {
7574
method: 'patch',
@@ -238,12 +237,9 @@ const core = __importStar(__nccwpck_require__(2186));
238237
const inputHelper = __importStar(__nccwpck_require__(5480));
239238
const dockerhubHelper = __importStar(__nccwpck_require__(1812));
240239
const readmeHelper = __importStar(__nccwpck_require__(3367));
240+
const utils = __importStar(__nccwpck_require__(918));
241241
const util_1 = __nccwpck_require__(3837);
242-
function getErrorMessage(error) {
243-
if (error instanceof Error)
244-
return error.message;
245-
return String(error);
246-
}
242+
const SHORT_DESCRIPTION_MAX_BYTES = 100;
247243
function run() {
248244
return __awaiter(this, void 0, void 0, function* () {
249245
try {
@@ -254,6 +250,11 @@ function run() {
254250
core.info('Reading description source file');
255251
const readmeContent = yield readmeHelper.getReadmeContent(inputs.readmeFilepath, inputs.enableUrlCompletion, inputs.imageExtensions);
256252
core.debug(readmeContent);
253+
// Truncate the short description if it is too long
254+
const truncatedShortDescription = utils.truncateToBytes(inputs.shortDescription, SHORT_DESCRIPTION_MAX_BYTES);
255+
if (truncatedShortDescription.length !== inputs.shortDescription.length) {
256+
core.warning(`The short description exceeds DockerHub's limit and has been truncated to ${SHORT_DESCRIPTION_MAX_BYTES} bytes.`);
257+
}
257258
// Acquire a token for the Docker Hub API
258259
core.info('Acquiring token');
259260
const token = yield dockerhubHelper.getToken(inputs.username, inputs.password);
@@ -264,7 +265,7 @@ function run() {
264265
}
265266
catch (error) {
266267
core.debug((0, util_1.inspect)(error));
267-
core.setFailed(getErrorMessage(error));
268+
core.setFailed(utils.getErrorMessage(error));
268269
}
269270
});
270271
}
@@ -311,10 +312,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
311312
});
312313
};
313314
Object.defineProperty(exports, "__esModule", ({ value: true }));
314-
exports.completeRelativeUrls = exports.getReadmeContent = exports.truncateToBytes = exports.ENABLE_URL_COMPLETION_DEFAULT = exports.IMAGE_EXTENSIONS_DEFAULT = exports.README_FILEPATH_DEFAULT = void 0;
315+
exports.completeRelativeUrls = exports.getReadmeContent = exports.ENABLE_URL_COMPLETION_DEFAULT = exports.IMAGE_EXTENSIONS_DEFAULT = exports.README_FILEPATH_DEFAULT = void 0;
315316
const core = __importStar(__nccwpck_require__(2186));
316317
const fs = __importStar(__nccwpck_require__(7147));
317-
const unicodeSubstring = __nccwpck_require__(6986);
318+
const utils = __importStar(__nccwpck_require__(918));
318319
exports.README_FILEPATH_DEFAULT = './README.md';
319320
exports.IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp';
320321
exports.ENABLE_URL_COMPLETION_DEFAULT = false;
@@ -323,22 +324,14 @@ const REPOSITORY_URL = `${process.env['GITHUB_SERVER_URL']}/${process.env['GITHU
323324
const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/`;
324325
const RAW_PREFIX = `${REPOSITORY_URL}/raw/${process.env['GITHUB_REF_NAME']}/`;
325326
const MAX_BYTES = 25000;
326-
function truncateToBytes(s, n) {
327-
let len = n;
328-
while (Buffer.byteLength(s) > n) {
329-
s = unicodeSubstring(s, 0, len--);
330-
}
331-
return s;
332-
}
333-
exports.truncateToBytes = truncateToBytes;
334327
function getReadmeContent(readmeFilepath, enableUrlCompletion, imageExtensions) {
335328
return __awaiter(this, void 0, void 0, function* () {
336329
// Fetch the readme content
337330
let readmeContent = yield fs.promises.readFile(readmeFilepath, {
338331
encoding: 'utf8'
339332
});
340333
readmeContent = completeRelativeUrls(readmeContent, readmeFilepath, enableUrlCompletion, imageExtensions);
341-
const truncatedReadmeContent = truncateToBytes(readmeContent, MAX_BYTES);
334+
const truncatedReadmeContent = utils.truncateToBytes(readmeContent, MAX_BYTES);
342335
if (truncatedReadmeContent.length !== readmeContent.length) {
343336
core.warning(`The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.`);
344337
}
@@ -446,6 +439,32 @@ function getRelativeUrlRules() {
446439
}
447440

448441

442+
/***/ }),
443+
444+
/***/ 918:
445+
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
446+
447+
"use strict";
448+
449+
Object.defineProperty(exports, "__esModule", ({ value: true }));
450+
exports.truncateToBytes = exports.getErrorMessage = void 0;
451+
const unicodeSubstring = __nccwpck_require__(6986);
452+
function getErrorMessage(error) {
453+
if (error instanceof Error)
454+
return error.message;
455+
return String(error);
456+
}
457+
exports.getErrorMessage = getErrorMessage;
458+
function truncateToBytes(s, n) {
459+
let len = n;
460+
while (Buffer.byteLength(s) > n) {
461+
s = unicodeSubstring(s, 0, len--);
462+
}
463+
return s;
464+
}
465+
exports.truncateToBytes = truncateToBytes;
466+
467+
449468
/***/ }),
450469

451470
/***/ 7351:

src/dockerhub-helper.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as core from '@actions/core'
22
import * as fetch from 'node-fetch'
33

4-
const DESCRIPTION_MAX_CHARS = 100
5-
64
export async function getToken(
75
username: string,
86
password: string
@@ -36,7 +34,7 @@ export async function updateRepositoryDescription(
3634
full_description: fullDescription
3735
}
3836
if (description) {
39-
body['description'] = description.slice(0, DESCRIPTION_MAX_CHARS)
37+
body['description'] = description
4038
}
4139
await fetch(`https://hub.docker.com/v2/repositories/${repository}`, {
4240
method: 'patch',

src/main.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import * as core from '@actions/core'
22
import * as inputHelper from './input-helper'
33
import * as dockerhubHelper from './dockerhub-helper'
44
import * as readmeHelper from './readme-helper'
5+
import * as utils from './utils'
56
import {inspect} from 'util'
67

7-
function getErrorMessage(error: unknown) {
8-
if (error instanceof Error) return error.message
9-
return String(error)
10-
}
8+
const SHORT_DESCRIPTION_MAX_BYTES = 100
119

1210
async function run(): Promise<void> {
1311
try {
@@ -25,6 +23,17 @@ async function run(): Promise<void> {
2523
)
2624
core.debug(readmeContent)
2725

26+
// Truncate the short description if it is too long
27+
const truncatedShortDescription = utils.truncateToBytes(
28+
inputs.shortDescription,
29+
SHORT_DESCRIPTION_MAX_BYTES
30+
)
31+
if (truncatedShortDescription.length !== inputs.shortDescription.length) {
32+
core.warning(
33+
`The short description exceeds DockerHub's limit and has been truncated to ${SHORT_DESCRIPTION_MAX_BYTES} bytes.`
34+
)
35+
}
36+
2837
// Acquire a token for the Docker Hub API
2938
core.info('Acquiring token')
3039
const token = await dockerhubHelper.getToken(
@@ -42,7 +51,7 @@ async function run(): Promise<void> {
4251
core.info('Request successful')
4352
} catch (error) {
4453
core.debug(inspect(error))
45-
core.setFailed(getErrorMessage(error))
54+
core.setFailed(utils.getErrorMessage(error))
4655
}
4756
}
4857

src/readme-helper.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as core from '@actions/core'
22
import * as fs from 'fs'
3-
import unicodeSubstring = require('unicode-substring')
3+
import * as utils from './utils'
44

55
export const README_FILEPATH_DEFAULT = './README.md'
66
export const IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp'
@@ -28,14 +28,6 @@ type Rule = {
2828
absUrlPrefix: string
2929
}
3030

31-
export function truncateToBytes(s: string, n: number): string {
32-
let len = n
33-
while (Buffer.byteLength(s) > n) {
34-
s = unicodeSubstring(s, 0, len--)
35-
}
36-
return s
37-
}
38-
3931
export async function getReadmeContent(
4032
readmeFilepath: string,
4133
enableUrlCompletion: boolean,
@@ -53,7 +45,7 @@ export async function getReadmeContent(
5345
imageExtensions
5446
)
5547

56-
const truncatedReadmeContent = truncateToBytes(readmeContent, MAX_BYTES)
48+
const truncatedReadmeContent = utils.truncateToBytes(readmeContent, MAX_BYTES)
5749
if (truncatedReadmeContent.length !== readmeContent.length) {
5850
core.warning(
5951
`The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.`

src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unicodeSubstring = require('unicode-substring')
2+
3+
export function getErrorMessage(error: unknown) {
4+
if (error instanceof Error) return error.message
5+
return String(error)
6+
}
7+
8+
export function truncateToBytes(s: string, n: number): string {
9+
let len = n
10+
while (Buffer.byteLength(s) > n) {
11+
s = unicodeSubstring(s, 0, len--)
12+
}
13+
return s
14+
}

0 commit comments

Comments
 (0)