Skip to content

Commit

Permalink
ci: fix small bugs in theme-preview action (#2093)
Browse files Browse the repository at this point in the history
* ci: fix small bugs in theme-preview action

* ci: format code
  • Loading branch information
rickstaa authored Oct 2, 2022
1 parent 84c9d35 commit 822efbf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
4 changes: 3 additions & 1 deletion scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
}
Expand Down
49 changes: 35 additions & 14 deletions scripts/preview-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;

Expand Down Expand Up @@ -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();
Expand All @@ -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...");
Expand All @@ -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}`);
Expand Down Expand Up @@ -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)) {
Expand All @@ -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: <code>#${colorValue}</code>`,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -543,4 +562,6 @@ const run = async () => {
}
};

run();
if (typeof require !== "undefined" && require.main === module) {
run();
}

1 comment on commit 822efbf

@vercel
Copy link

@vercel vercel bot commented on 822efbf Oct 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.