Skip to content

Commit

Permalink
fix theme preview (anuraghazra#2564)
Browse files Browse the repository at this point in the history
* ci: fix theme preview action

* fix: fix some bugs in the 'theme-preveiw' action
  • Loading branch information
rickstaa authored and tomy0000000 committed Apr 4, 2023
1 parent 20e19c7 commit 385b1d8
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions scripts/preview-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -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}`,
);
}
}
};
Expand Down Expand Up @@ -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}`);
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 385b1d8

Please sign in to comment.