Skip to content

Commit

Permalink
feat: truncate short description exceeding the byte limit (#143)
Browse files Browse the repository at this point in the history
* feat: truncate short description exceeding the byte limit

* fix tests
  • Loading branch information
peter-evans authored Apr 7, 2023
1 parent 3ba533f commit 4b1a4bb
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 47 deletions.
11 changes: 1 addition & 10 deletions __test__/readme-helper.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {completeRelativeUrls, truncateToBytes} from '../src/readme-helper'
import {completeRelativeUrls} from '../src/readme-helper'

describe('complete relative urls tests', () => {
const GITHUB_SERVER_URL = process.env['GITHUB_SERVER_URL']
Expand Down Expand Up @@ -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('😀😁')
})
})
10 changes: 10 additions & 0 deletions __test__/utils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 19 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.updateRepositoryDescription = exports.getToken = void 0;
const core = __importStar(__nccwpck_require__(2186));
const fetch = __importStar(__nccwpck_require__(467));
const DESCRIPTION_MAX_CHARS = 100;
function getToken(username, password) {
return __awaiter(this, void 0, void 0, function* () {
const body = {
Expand All @@ -69,7 +68,7 @@ function updateRepositoryDescription(token, repository, description, fullDescrip
full_description: fullDescription
};
if (description) {
body['description'] = description.slice(0, DESCRIPTION_MAX_CHARS);
body['description'] = description;
}
yield fetch(`https://hub.docker.com/v2/repositories/${repository}`, {
method: 'patch',
Expand Down Expand Up @@ -238,12 +237,9 @@ const core = __importStar(__nccwpck_require__(2186));
const inputHelper = __importStar(__nccwpck_require__(5480));
const dockerhubHelper = __importStar(__nccwpck_require__(1812));
const readmeHelper = __importStar(__nccwpck_require__(3367));
const utils = __importStar(__nccwpck_require__(918));
const util_1 = __nccwpck_require__(3837);
function getErrorMessage(error) {
if (error instanceof Error)
return error.message;
return String(error);
}
const SHORT_DESCRIPTION_MAX_BYTES = 100;
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
Expand All @@ -254,6 +250,11 @@ function run() {
core.info('Reading description source file');
const readmeContent = yield readmeHelper.getReadmeContent(inputs.readmeFilepath, inputs.enableUrlCompletion, inputs.imageExtensions);
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
core.info('Acquiring token');
const token = yield dockerhubHelper.getToken(inputs.username, inputs.password);
Expand All @@ -264,7 +265,7 @@ function run() {
}
catch (error) {
core.debug((0, util_1.inspect)(error));
core.setFailed(getErrorMessage(error));
core.setFailed(utils.getErrorMessage(error));
}
});
}
Expand Down Expand Up @@ -311,10 +312,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
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 fs = __importStar(__nccwpck_require__(7147));
const unicodeSubstring = __nccwpck_require__(6986);
const utils = __importStar(__nccwpck_require__(918));
exports.README_FILEPATH_DEFAULT = './README.md';
exports.IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp';
exports.ENABLE_URL_COMPLETION_DEFAULT = false;
Expand All @@ -323,22 +324,14 @@ const REPOSITORY_URL = `${process.env['GITHUB_SERVER_URL']}/${process.env['GITHU
const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/`;
const RAW_PREFIX = `${REPOSITORY_URL}/raw/${process.env['GITHUB_REF_NAME']}/`;
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) {
return __awaiter(this, void 0, void 0, function* () {
// Fetch the readme content
let readmeContent = yield fs.promises.readFile(readmeFilepath, {
encoding: 'utf8'
});
readmeContent = completeRelativeUrls(readmeContent, readmeFilepath, enableUrlCompletion, imageExtensions);
const truncatedReadmeContent = truncateToBytes(readmeContent, MAX_BYTES);
const truncatedReadmeContent = utils.truncateToBytes(readmeContent, MAX_BYTES);
if (truncatedReadmeContent.length !== readmeContent.length) {
core.warning(`The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.`);
}
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions src/dockerhub-helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as core from '@actions/core'
import * as fetch from 'node-fetch'

const DESCRIPTION_MAX_CHARS = 100

export async function getToken(
username: string,
password: string
Expand Down Expand Up @@ -36,7 +34,7 @@ export async function updateRepositoryDescription(
full_description: fullDescription
}
if (description) {
body['description'] = description.slice(0, DESCRIPTION_MAX_CHARS)
body['description'] = description
}
await fetch(`https://hub.docker.com/v2/repositories/${repository}`, {
method: 'patch',
Expand Down
19 changes: 14 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import * as core from '@actions/core'
import * as inputHelper from './input-helper'
import * as dockerhubHelper from './dockerhub-helper'
import * as readmeHelper from './readme-helper'
import * as utils from './utils'
import {inspect} from 'util'

function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message
return String(error)
}
const SHORT_DESCRIPTION_MAX_BYTES = 100

async function run(): Promise<void> {
try {
Expand All @@ -25,6 +23,17 @@ async function run(): Promise<void> {
)
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
core.info('Acquiring token')
const token = await dockerhubHelper.getToken(
Expand All @@ -42,7 +51,7 @@ async function run(): Promise<void> {
core.info('Request successful')
} catch (error) {
core.debug(inspect(error))
core.setFailed(getErrorMessage(error))
core.setFailed(utils.getErrorMessage(error))
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/readme-helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import * as fs from 'fs'
import unicodeSubstring = require('unicode-substring')
import * as utils from './utils'

export const README_FILEPATH_DEFAULT = './README.md'
export const IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp'
Expand Down Expand Up @@ -28,14 +28,6 @@ type Rule = {
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(
readmeFilepath: string,
enableUrlCompletion: boolean,
Expand All @@ -53,7 +45,7 @@ export async function getReadmeContent(
imageExtensions
)

const truncatedReadmeContent = truncateToBytes(readmeContent, MAX_BYTES)
const truncatedReadmeContent = utils.truncateToBytes(readmeContent, MAX_BYTES)
if (truncatedReadmeContent.length !== readmeContent.length) {
core.warning(
`The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.`
Expand Down
14 changes: 14 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4b1a4bb

Please sign in to comment.