feat: truncate short description exceeding the byte limit (#143)
Some checks failed
Publish Docker Image / publish (push) Has been cancelled

* 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
7 changed files with 80 additions and 47 deletions

View File

@@ -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
@@ -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',

View File

@@ -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 {
@@ -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(
@@ -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))
}
}

View File

@@ -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'
@@ -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,
@@ -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.`

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
}