From 20aaac4a85402e931353aece3eff6083ebbe64c4 Mon Sep 17 00:00:00 2001 From: Sebastian Szewczyk Date: Tue, 26 Sep 2023 11:09:10 +0200 Subject: [PATCH] Detecting react component and fetching files in promise.race --- .../authorChecklist/authorChecklist.js | 5 +++-- .../javascript/authorChecklist/index.js | 18 ++++++++++-------- .../authorChecklist/newComponentCategory.js | 13 +++++++------ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/.github/actions/javascript/authorChecklist/authorChecklist.js b/.github/actions/javascript/authorChecklist/authorChecklist.js index 28e9bc038ca7..8a8a02e77f7d 100644 --- a/.github/actions/javascript/authorChecklist/authorChecklist.js +++ b/.github/actions/javascript/authorChecklist/authorChecklist.js @@ -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); diff --git a/.github/actions/javascript/authorChecklist/index.js b/.github/actions/javascript/authorChecklist/index.js index d24e4d484587..7f1b72f7afd0 100644 --- a/.github/actions/javascript/authorChecklist/index.js +++ b/.github/actions/javascript/authorChecklist/index.js @@ -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); @@ -248,7 +249,7 @@ function detectReactComponent(code) { return isReactComponent; }; -function fetchFile(filename) { +async function detectReactComponentInFile(filename) { const content = { owner: CONST.GITHUB_OWNER, repo: CONST.APP_REPO, @@ -256,16 +257,17 @@ function fetchFile(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, diff --git a/.github/actions/javascript/authorChecklist/newComponentCategory.js b/.github/actions/javascript/authorChecklist/newComponentCategory.js index 8095f0fda29b..8bad5f3d286e 100644 --- a/.github/actions/javascript/authorChecklist/newComponentCategory.js +++ b/.github/actions/javascript/authorChecklist/newComponentCategory.js @@ -48,7 +48,7 @@ function detectReactComponent(code) { return isReactComponent; }; -function fetchFile(filename) { +async function detectReactComponentInFile(filename) { const content = { owner: CONST.GITHUB_OWNER, repo: CONST.APP_REPO, @@ -56,16 +56,17 @@ function fetchFile(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,