From ca98c6b480de04ea9fa767c5135bd38eeb16a8e2 Mon Sep 17 00:00:00 2001 From: Rick Staa Date: Fri, 3 Mar 2023 08:42:11 +0100 Subject: [PATCH 1/2] ci: fix theme preview action --- scripts/preview-theme.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/preview-theme.js b/scripts/preview-theme.js index baccaa61a3a78..2cfe0f25cad3a 100644 --- a/scripts/preview-theme.js +++ b/scripts/preview-theme.js @@ -46,8 +46,8 @@ const INVALID_REVIEW_COMMENT = (commentUrl) => // Retrieve octokit instance. const OCTOKIT = github.getOctokit(getGithubToken()); -const PULL_REQUEST_ID = getPrNumber(); const { OWNER, REPO } = getRepoInfo(github.context); +var PULL_REQUEST_ID; /** * Retrieve PR number from the event payload. @@ -318,6 +318,7 @@ export const run = async () => { `; const ccc = new ColorContrastChecker(); const commenter = getCommenter(); + PULL_REQUEST_ID = getPrNumber(); debug(`Owner: ${OWNER}`); debug(`Repo: ${REPO}`); debug(`Commenter: ${commenter}`); From ec2ab4437dc2d3da8fe04018f07999eb39fc7c49 Mon Sep 17 00:00:00 2001 From: Rick Staa Date: Fri, 3 Mar 2023 14:07:05 +0100 Subject: [PATCH 2/2] fix: fix some bugs in the 'theme-preveiw' action --- scripts/preview-theme.js | 41 +++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/scripts/preview-theme.js b/scripts/preview-theme.js index 2cfe0f25cad3a..c0bb9eb5522d8 100644 --- a/scripts/preview-theme.js +++ b/scripts/preview-theme.js @@ -43,12 +43,24 @@ 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.`; - -// Retrieve octokit instance. -const OCTOKIT = github.getOctokit(getGithubToken()); -const { OWNER, REPO } = getRepoInfo(github.context); +var OCTOKIT; +var OWNER; +var REPO; var PULL_REQUEST_ID; +/** + * Incorrect JSON format error. + * @extends Error + * @param {string} message Error message. + * @returns {Error} IncorrectJsonFormatError. + */ +class IncorrectJsonFormatError extends Error { + constructor(message) { + super(message); + this.name = "IncorrectJsonFormatError"; + } +} + /** * Retrieve PR number from the event payload. * @@ -274,7 +286,9 @@ const parseJSON = (json) => { if (typeof parsedJson === "object") { return parsedJson; } else { - throw new Error("PR diff is not a valid theme JSON object."); + throw new IncorrectJsonFormatError( + "PR diff is not a valid theme JSON object.", + ); } } catch (error) { let parsedJson = json @@ -289,7 +303,9 @@ const parseJSON = (json) => { } return Hjson.parse(parsedJson.join("")); } else { - throw error; + throw new IncorrectJsonFormatError( + `Theme JSON file could not be parsed: ${error.message}`, + ); } } }; @@ -317,6 +333,11 @@ export const run = async () => { \r${THEME_CONTRIB_GUIDELINESS} `; const ccc = new ColorContrastChecker(); + OCTOKIT = github.getOctokit(getGithubToken()); + PULL_REQUEST_ID = getPrNumber(); + const { owner, repo } = getRepoInfo(github.context); + OWNER = owner; + REPO = repo; const commenter = getCommenter(); PULL_REQUEST_ID = getPrNumber(); debug(`Owner: ${OWNER}`); @@ -326,8 +347,8 @@ export const run = async () => { // Retrieve the PR diff and preview-theme comment. debug("Retrieve PR diff..."); const res = await OCTOKIT.pulls.get({ - OWNER, - REPO, + owner: OWNER, + repo: REPO, pull_number: PULL_REQUEST_ID, mediaType: { format: "diff", @@ -569,7 +590,9 @@ export const run = async () => { OWNER, REPO, "REQUEST_CHANGES", - error.message, + "**Something went wrong in the theme preview action:** `" + + error.message + + "`", ); await addRemoveLabel( OCTOKIT,