Skip to content

Commit

Permalink
Detecting react component and fetching files in promise.race
Browse files Browse the repository at this point in the history
  • Loading branch information
sebryu committed Sep 26, 2023
1 parent 90708ea commit 20aaac4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
5 changes: 3 additions & 2 deletions .github/actions/javascript/authorChecklist/authorChecklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ async function getChecklistCategoriesForPullRequest() {
per_page: 100,
});

_.each(CHECKLIST_CATEGORIES, ({ detectFunction, items }) => {
if (!detectFunction(changedFiles)) {
_.each(CHECKLIST_CATEGORIES, async ({ detectFunction, items }) => {
const categoryDetected = await detectFunction(changedFiles);
if (!categoryDetected) {
return;
}
categories.push(items);
Expand Down
18 changes: 10 additions & 8 deletions .github/actions/javascript/authorChecklist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ async function getChecklistCategoriesForPullRequest() {
per_page: 100,
});

_.each(CHECKLIST_CATEGORIES, ({ detectFunction, items }) => {
if (!detectFunction(changedFiles)) {
_.each(CHECKLIST_CATEGORIES, async ({ detectFunction, items }) => {
const categoryDetected = await detectFunction(changedFiles);
if (!categoryDetected) {
return;
}
categories.push(items);
Expand Down Expand Up @@ -248,24 +249,25 @@ function detectReactComponent(code) {
return isReactComponent;
};

function fetchFile(filename) {
async function detectReactComponentInFile(filename) {
const content = {
owner: CONST.GITHUB_OWNER,
repo: CONST.APP_REPO,
path: filename,
ref: github.context.payload.pull_request.head.ref,
};
try {
return GithubUtils.octokit.repos.getContent(content);
const { data } = await GithubUtils.octokit.repos.getContent(content);
return detectReactComponent(data);
} catch (error) {
console.error(`An unknown error occurred with the GitHub API: ${error}, while fetching ${content}`);
}
}

function detectFunction(changedFiles) {
;
async function detectFunction(changedFiles) {
const filteredFiles = _.filter((changedFiles), ({ filename }) => filename.endsWith('.js') || filename.endsWith('.jsx') || filename.endsWith('.ts') || filename.endsWith('.tsx'));
return _.some(filteredFiles, ({ filename }) => detectReactComponent(fetchFile(filename)));
}
return Promise.race(_.map(filteredFiles, ({ filename }) => detectReactComponentInFile(filename)));
};

module.exports = {
detectFunction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,25 @@ function detectReactComponent(code) {
return isReactComponent;
};

function fetchFile(filename) {
async function detectReactComponentInFile(filename) {
const content = {
owner: CONST.GITHUB_OWNER,
repo: CONST.APP_REPO,
path: filename,
ref: github.context.payload.pull_request.head.ref,
};
try {
return GithubUtils.octokit.repos.getContent(content);
const { data } = await GithubUtils.octokit.repos.getContent(content);
return detectReactComponent(data);
} catch (error) {
console.error(`An unknown error occurred with the GitHub API: ${error}, while fetching ${content}`);
}
}

function detectFunction(changedFiles) {
;
async function detectFunction(changedFiles) {
const filteredFiles = _.filter((changedFiles), ({ filename }) => filename.endsWith('.js') || filename.endsWith('.jsx') || filename.endsWith('.ts') || filename.endsWith('.tsx'));
return _.some(filteredFiles, ({ filename }) => detectReactComponent(fetchFile(filename)));
}
return Promise.race(_.map(filteredFiles, ({ filename }) => detectReactComponentInFile(filename)));
};

module.exports = {
detectFunction,
Expand Down

0 comments on commit 20aaac4

Please sign in to comment.