Skip to content

Commit b4ef620

Browse files
rickstaadevantler
authored andcommitted
fix theme preview (anuraghazra#2564)
* ci: fix theme preview action * fix: fix some bugs in the 'theme-preveiw' action
1 parent 89ffe4e commit b4ef620

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

scripts/preview-theme.js

+32-9
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,24 @@ const ACCEPTED_COLOR_PROPS = Object.keys(COLOR_PROPS);
4343
const REQUIRED_COLOR_PROPS = ACCEPTED_COLOR_PROPS.slice(0, 4);
4444
const INVALID_REVIEW_COMMENT = (commentUrl) =>
4545
`Some themes are invalid. See the [Automated Theme Preview](${commentUrl}) comment above for more information.`;
46-
47-
// Retrieve octokit instance.
48-
const OCTOKIT = github.getOctokit(getGithubToken());
49-
const { OWNER, REPO } = getRepoInfo(github.context);
46+
var OCTOKIT;
47+
var OWNER;
48+
var REPO;
5049
var PULL_REQUEST_ID;
5150

51+
/**
52+
* Incorrect JSON format error.
53+
* @extends Error
54+
* @param {string} message Error message.
55+
* @returns {Error} IncorrectJsonFormatError.
56+
*/
57+
class IncorrectJsonFormatError extends Error {
58+
constructor(message) {
59+
super(message);
60+
this.name = "IncorrectJsonFormatError";
61+
}
62+
}
63+
5264
/**
5365
* Retrieve PR number from the event payload.
5466
*
@@ -274,7 +286,9 @@ const parseJSON = (json) => {
274286
if (typeof parsedJson === "object") {
275287
return parsedJson;
276288
} else {
277-
throw new Error("PR diff is not a valid theme JSON object.");
289+
throw new IncorrectJsonFormatError(
290+
"PR diff is not a valid theme JSON object.",
291+
);
278292
}
279293
} catch (error) {
280294
let parsedJson = json
@@ -289,7 +303,9 @@ const parseJSON = (json) => {
289303
}
290304
return Hjson.parse(parsedJson.join(""));
291305
} else {
292-
throw error;
306+
throw new IncorrectJsonFormatError(
307+
`Theme JSON file could not be parsed: ${error.message}`,
308+
);
293309
}
294310
}
295311
};
@@ -317,6 +333,11 @@ export const run = async () => {
317333
\r${THEME_CONTRIB_GUIDELINESS}
318334
`;
319335
const ccc = new ColorContrastChecker();
336+
OCTOKIT = github.getOctokit(getGithubToken());
337+
PULL_REQUEST_ID = getPrNumber();
338+
const { owner, repo } = getRepoInfo(github.context);
339+
OWNER = owner;
340+
REPO = repo;
320341
const commenter = getCommenter();
321342
PULL_REQUEST_ID = getPrNumber();
322343
debug(`Owner: ${OWNER}`);
@@ -326,8 +347,8 @@ export const run = async () => {
326347
// Retrieve the PR diff and preview-theme comment.
327348
debug("Retrieve PR diff...");
328349
const res = await OCTOKIT.pulls.get({
329-
OWNER,
330-
REPO,
350+
owner: OWNER,
351+
repo: REPO,
331352
pull_number: PULL_REQUEST_ID,
332353
mediaType: {
333354
format: "diff",
@@ -569,7 +590,9 @@ export const run = async () => {
569590
OWNER,
570591
REPO,
571592
"REQUEST_CHANGES",
572-
error.message,
593+
"**Something went wrong in the theme preview action:** `" +
594+
error.message +
595+
"`",
573596
);
574597
await addRemoveLabel(
575598
OCTOKIT,

0 commit comments

Comments
 (0)