ci: fix small bugs in theme-preview action (#2093)

* ci: fix small bugs in theme-preview action

* ci: format code
This commit is contained in:
Rick Staa 2022-10-02 17:41:28 +02:00 committed by GitHub
parent 84c9d354b3
commit 822efbf6cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 15 deletions

View File

@ -2,6 +2,8 @@
* @file Contains helper functions used in the scripts. * @file Contains helper functions used in the scripts.
*/ */
import { getInput } from "@actions/core";
// Script variables. // Script variables.
const OWNER = "anuraghazra"; const OWNER = "anuraghazra";
const REPO = "github-readme-stats"; const REPO = "github-readme-stats";
@ -32,7 +34,7 @@ export const getRepoInfo = (ctx) => {
* @returns {string} Github token. * @returns {string} Github token.
*/ */
export const getGithubToken = () => { export const getGithubToken = () => {
const token = core.getInput("github_token") || process.env.GITHUB_TOKEN; const token = getInput("github_token") || process.env.GITHUB_TOKEN;
if (!token) { if (!token) {
throw Error("Could not find github token"); throw Error("Could not find github token");
} }

View File

@ -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). \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 = [ const COLOR_PROPS = {
"title_color", title_color: 6,
"icon_color", icon_color: 6,
"text_color", text_color: 6,
"bg_color", 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) => const INVALID_REVIEW_COMMENT = (commentUrl) =>
`Some themes are invalid. See the [Automated Theme Preview](${commentUrl}) comment above for more information.`; `Some themes are invalid. See the [Automated Theme Preview](${commentUrl}) comment above for more information.`;
@ -271,11 +274,11 @@ const parseJSON = (json) => {
} }
} catch (error) { } catch (error) {
let parsedJson = json 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()); .filter((x) => typeof x !== "string" || !!x.trim());
if (parsedJson[0].replace(/\s+/g, "") === "},") { if (parsedJson[0].replace(/\s+/g, "") === "},") {
parsedJson[0] = "},"; parsedJson[0] = "},";
if (!/\s*}\s*,?\s*$/.test(parsedJson[0])) { if (!/\s*}\s*,?\s*$/.test(parsedJson[1])) {
parsedJson.push(parsedJson.shift()); parsedJson.push(parsedJson.shift());
} else { } else {
parsedJson.shift(); parsedJson.shift();
@ -299,7 +302,7 @@ const themeNameAlreadyExists = (name) => {
/** /**
* Main function. * Main function.
*/ */
const run = async () => { export const run = async (prNumber) => {
try { try {
const dryRun = process.env.DRY_RUN === "true" || false; const dryRun = process.env.DRY_RUN === "true" || false;
debug("Retrieve action information from context..."); debug("Retrieve action information from context...");
@ -310,7 +313,7 @@ const run = async () => {
`; `;
const ccc = new ColorContrastChecker(); const ccc = new ColorContrastChecker();
const octokit = github.getOctokit(getGithubToken()); const octokit = github.getOctokit(getGithubToken());
const pullRequestId = getPrNumber(); const pullRequestId = prNumber ? prNumber : getPrNumber();
const commenter = getCommenter(); const commenter = getCommenter();
const { owner, repo } = getRepoInfo(github.context); const { owner, repo } = getRepoInfo(github.context);
debug(`Owner: ${owner}`); debug(`Owner: ${owner}`);
@ -387,10 +390,19 @@ const run = async () => {
const missingKeys = REQUIRED_COLOR_PROPS.filter( const missingKeys = REQUIRED_COLOR_PROPS.filter(
(x) => !Object.keys(colors).includes(x), (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) { for (const missingKey of missingKeys) {
errors.push(`Theme color properties \`${missingKey}\` are missing`); errors.push(`Theme color properties \`${missingKey}\` are missing`);
} }
for (const extraKey of extraKeys) {
warnings.push(
`Theme color properties \`${extraKey}\` is not supported`,
);
}
invalidColors = true; invalidColors = true;
} else { } else {
for (const [colorKey, colorValue] of Object.entries(colors)) { for (const [colorKey, colorValue] of Object.entries(colors)) {
@ -399,6 +411,11 @@ const run = async () => {
`Theme color property \`${colorKey}\` should not start with '#'`, `Theme color property \`${colorKey}\` should not start with '#'`,
); );
invalidColors = true; 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)) { } else if (!isValidHexColor(colorValue)) {
errors.push( errors.push(
`Theme color property \`${colorKey}\` is not a valid hex color: <code>#${colorValue}</code>`, `Theme color property \`${colorKey}\` is not a valid hex color: <code>#${colorValue}</code>`,
@ -437,8 +454,10 @@ const run = async () => {
text_color: [textColor, bgColor], text_color: [textColor, bgColor],
}; };
Object.keys(colorPairs).forEach((item) => { Object.keys(colorPairs).forEach((item) => {
const color1 = colorPairs[item][0]; let color1 = colorPairs[item][0];
const color2 = colorPairs[item][1]; 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}`)) { if (!ccc.isLevelAA(`#${color1}`, `#${color2}`)) {
const permalink = getWebAimLink(color1, color2); const permalink = getWebAimLink(color1, color2);
warnings.push( warnings.push(
@ -543,4 +562,6 @@ const run = async () => {
} }
}; };
run(); if (typeof require !== "undefined" && require.main === module) {
run();
}