From 822efbf6cb991732ed3fe7460c6831e48a407a38 Mon Sep 17 00:00:00 2001 From: Rick Staa Date: Sun, 2 Oct 2022 17:41:28 +0200 Subject: [PATCH] ci: fix small bugs in theme-preview action (#2093) * ci: fix small bugs in theme-preview action * ci: format code --- scripts/helpers.js | 4 +++- scripts/preview-theme.js | 49 ++++++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/scripts/helpers.js b/scripts/helpers.js index fabf300f477be..3fc5fc7d00eee 100644 --- a/scripts/helpers.js +++ b/scripts/helpers.js @@ -2,6 +2,8 @@ * @file Contains helper functions used in the scripts. */ +import { getInput } from "@actions/core"; + // Script variables. const OWNER = "anuraghazra"; const REPO = "github-readme-stats"; @@ -32,7 +34,7 @@ export const getRepoInfo = (ctx) => { * @returns {string} Github token. */ export const getGithubToken = () => { - const token = core.getInput("github_token") || process.env.GITHUB_TOKEN; + const token = getInput("github_token") || process.env.GITHUB_TOKEN; if (!token) { throw Error("Could not find github token"); } diff --git a/scripts/preview-theme.js b/scripts/preview-theme.js index 94145fb253c71..6bb29e68006c0 100644 --- a/scripts/preview-theme.js +++ b/scripts/preview-theme.js @@ -33,12 +33,15 @@ const THEME_CONTRIB_GUIDELINESS = ` \r> Also, note that if this theme is exclusively for your personal use, then instead of adding it to our theme collection, you can use card [customization options](https://github.com/anuraghazra/github-readme-stats#customization). `; -const REQUIRED_COLOR_PROPS = [ - "title_color", - "icon_color", - "text_color", - "bg_color", -]; +const COLOR_PROPS = { + title_color: 6, + icon_color: 6, + text_color: 6, + bg_color: 8, + border_color: 6, +}; +const ACCEPTED_COLOR_PROPS = Object.keys(COLOR_PROPS); +const REQUIRED_COLOR_PROPS = ACCEPTED_COLOR_PROPS.slice(0, 4); const INVALID_REVIEW_COMMENT = (commentUrl) => `Some themes are invalid. See the [Automated Theme Preview](${commentUrl}) comment above for more information.`; @@ -271,11 +274,11 @@ const parseJSON = (json) => { } } catch (error) { let parsedJson = json - .split(/(?:\s*)(}\s*,?)(?:\s*)(?=\s*[a-z_"]+:+)/) + .split(/([\s\r\s]*}[\s\r\s]*,[\s\r\s]*)(?=[\w"-]+:)/) .filter((x) => typeof x !== "string" || !!x.trim()); if (parsedJson[0].replace(/\s+/g, "") === "},") { parsedJson[0] = "},"; - if (!/\s*}\s*,?\s*$/.test(parsedJson[0])) { + if (!/\s*}\s*,?\s*$/.test(parsedJson[1])) { parsedJson.push(parsedJson.shift()); } else { parsedJson.shift(); @@ -299,7 +302,7 @@ const themeNameAlreadyExists = (name) => { /** * Main function. */ -const run = async () => { +export const run = async (prNumber) => { try { const dryRun = process.env.DRY_RUN === "true" || false; debug("Retrieve action information from context..."); @@ -310,7 +313,7 @@ const run = async () => { `; const ccc = new ColorContrastChecker(); const octokit = github.getOctokit(getGithubToken()); - const pullRequestId = getPrNumber(); + const pullRequestId = prNumber ? prNumber : getPrNumber(); const commenter = getCommenter(); const { owner, repo } = getRepoInfo(github.context); debug(`Owner: ${owner}`); @@ -387,10 +390,19 @@ const run = async () => { const missingKeys = REQUIRED_COLOR_PROPS.filter( (x) => !Object.keys(colors).includes(x), ); - if (missingKeys.length > 0) { + const extraKeys = Object.keys(colors).filter( + (x) => !ACCEPTED_COLOR_PROPS.includes(x), + ); + if (missingKeys.length > 0 || extraKeys.length > 0) { for (const missingKey of missingKeys) { errors.push(`Theme color properties \`${missingKey}\` are missing`); } + + for (const extraKey of extraKeys) { + warnings.push( + `Theme color properties \`${extraKey}\` is not supported`, + ); + } invalidColors = true; } else { for (const [colorKey, colorValue] of Object.entries(colors)) { @@ -399,6 +411,11 @@ const run = async () => { `Theme color property \`${colorKey}\` should not start with '#'`, ); invalidColors = true; + } else if (colorValue.length > COLOR_PROPS[colorKey]) { + errors.push( + `Theme color property \`${colorKey}\` can not be longer than \`${COLOR_PROPS[colorKey]}\` characters`, + ); + invalidColors = true; } else if (!isValidHexColor(colorValue)) { errors.push( `Theme color property \`${colorKey}\` is not a valid hex color: #${colorValue}`, @@ -437,8 +454,10 @@ const run = async () => { text_color: [textColor, bgColor], }; Object.keys(colorPairs).forEach((item) => { - const color1 = colorPairs[item][0]; - const color2 = colorPairs[item][1]; + let color1 = colorPairs[item][0]; + let color2 = colorPairs[item][1]; + color1 = color1.length === 4 ? color1.slice(0, 3) : color1.slice(0, 6); + color2 = color2.length === 4 ? color2.slice(0, 3) : color2.slice(0, 6); if (!ccc.isLevelAA(`#${color1}`, `#${color2}`)) { const permalink = getWebAimLink(color1, color2); warnings.push( @@ -543,4 +562,6 @@ const run = async () => { } }; -run(); +if (typeof require !== "undefined" && require.main === module) { + run(); +}