From 02e7c6b3e098d65538ed8cbfeb76539105f457df Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 30 Aug 2023 16:54:05 +0200 Subject: [PATCH 1/3] Enable async/await and for-of in tests and actions --- .eslintrc.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index ec55f16b2832..22b94d79e369 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -163,5 +163,13 @@ module.exports = { ], }, }, + { + files: ['tests/**/*.{js,jsx,ts,tsx}', '.github/**/*.{js,jsx,ts,tsx}'], + rules: { + '@lwc/lwc/no-async-await': 'off', + 'no-await-in-loop': 'off', + 'no-restricted-syntax': ['error', 'ForInStatement', 'LabeledStatement', 'WithStatement'], + }, + }, ], }; From 8ce749898fbd74186e347e7bbc26d2b6daa885ad Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 30 Aug 2023 17:02:45 +0200 Subject: [PATCH 2/3] Refactor createOrUpdateStagingDeploy to use async/await --- .../createOrUpdateStagingDeploy.js | 232 ++++++++---------- .../createOrUpdateStagingDeploy/index.js | 226 +++++++---------- tests/unit/createOrUpdateStagingDeployTest.js | 16 +- 3 files changed, 196 insertions(+), 278 deletions(-) diff --git a/.github/actions/javascript/createOrUpdateStagingDeploy/createOrUpdateStagingDeploy.js b/.github/actions/javascript/createOrUpdateStagingDeploy/createOrUpdateStagingDeploy.js index f2d42bf717eb..6465ff760473 100644 --- a/.github/actions/javascript/createOrUpdateStagingDeploy/createOrUpdateStagingDeploy.js +++ b/.github/actions/javascript/createOrUpdateStagingDeploy/createOrUpdateStagingDeploy.js @@ -5,167 +5,127 @@ const CONST = require('../../../libs/CONST'); const GithubUtils = require('../../../libs/GithubUtils'); const GitUtils = require('../../../libs/GitUtils'); -const run = function () { +async function run() { const newVersion = core.getInput('NPM_VERSION'); console.log('New version found from action input:', newVersion); - let shouldCreateNewStagingDeployCash = false; - let newTag = newVersion; - let newDeployBlockers = []; - let previousStagingDeployCashData = {}; - let currentStagingDeployCashData = null; - - // Start by fetching the list of recent StagingDeployCash issues, along with the list of open deploy blockers - return Promise.all([ - GithubUtils.octokit.issues.listForRepo({ + try { + // Start by fetching the list of recent StagingDeployCash issues, along with the list of open deploy blockers + const {data: recentDeployChecklists} = await GithubUtils.octokit.issues.listForRepo({ log: console, owner: CONST.GITHUB_OWNER, repo: CONST.APP_REPO, labels: CONST.LABELS.STAGING_DEPLOY, state: 'all', - }), - GithubUtils.octokit.issues.listForRepo({ - log: console, - owner: CONST.GITHUB_OWNER, - repo: CONST.APP_REPO, - labels: CONST.LABELS.DEPLOY_BLOCKER, - }), - ]) - .then(([stagingDeployResponse, deployBlockerResponse]) => { - if (!stagingDeployResponse || !stagingDeployResponse.data || _.isEmpty(stagingDeployResponse.data)) { - console.error('Failed fetching StagingDeployCash issues from Github!', stagingDeployResponse); - throw new Error('Failed fetching StagingDeployCash issues from Github'); - } - - if (!deployBlockerResponse || !deployBlockerResponse.data) { - console.log('Failed fetching DeployBlockerCash issues from Github, continuing...'); - } - - newDeployBlockers = _.map(deployBlockerResponse.data, ({html_url}) => ({ - url: html_url, - number: GithubUtils.getIssueOrPullRequestNumberFromURL(html_url), - isResolved: false, - })); - - // Look at the state of the most recent StagingDeployCash, - // if it is open then we'll update the existing one, otherwise, we'll create a new one. - shouldCreateNewStagingDeployCash = Boolean(stagingDeployResponse.data[0].state !== 'open'); - if (shouldCreateNewStagingDeployCash) { - console.log('Latest StagingDeployCash is closed, creating a new one.', stagingDeployResponse.data[0]); - } else { - console.log( - 'Latest StagingDeployCash is open, updating it instead of creating a new one.', - 'Current:', - stagingDeployResponse.data[0], - 'Previous:', - stagingDeployResponse.data[1], - ); - } - - // Parse the data from the previous StagingDeployCash - // (newest if there are none open, otherwise second-newest) - previousStagingDeployCashData = shouldCreateNewStagingDeployCash - ? GithubUtils.getStagingDeployCashData(stagingDeployResponse.data[0]) - : GithubUtils.getStagingDeployCashData(stagingDeployResponse.data[1]); - - console.log('Found tag of previous StagingDeployCash:', previousStagingDeployCashData.tag); - - // Find the list of PRs merged between the last StagingDeployCash and the new version - if (shouldCreateNewStagingDeployCash) { - return GitUtils.getPullRequestsMergedBetween(previousStagingDeployCashData.tag, newTag); - } - - currentStagingDeployCashData = GithubUtils.getStagingDeployCashData(stagingDeployResponse.data[0]); - console.log('Parsed the following data from the current StagingDeployCash:', currentStagingDeployCashData); - - // If we aren't sent a tag, then use the existing tag - newTag = newTag || currentStagingDeployCashData.tag; - - return GitUtils.getPullRequestsMergedBetween(previousStagingDeployCashData.tag, newTag); - }) - .then((mergedPRs) => { - console.log(`The following PRs have been merged between the previous StagingDeployCash (${previousStagingDeployCashData.tag}) and new version (${newVersion}):`, mergedPRs); - - if (shouldCreateNewStagingDeployCash) { - return GithubUtils.generateStagingDeployCashBody(newTag, _.map(mergedPRs, GithubUtils.getPullRequestURLFromNumber)); - } - - const didVersionChange = newVersion ? newVersion !== currentStagingDeployCashData.tag : false; - - // Generate the PR list, preserving the previous state of `isVerified` for existing PRs - const PRList = _.sortBy( - _.unique( - _.union( - currentStagingDeployCashData.PRList, - _.map(mergedPRs, (number) => ({ - number, - url: GithubUtils.getPullRequestURLFromNumber(number), + }); - // Since this is the second argument to _.union, - // it will appear later in the array than any duplicate. - // Since it is later in the array, it will be truncated by _.unique, - // and the original value of isVerified will be preserved. - isVerified: false, - })), - ), - false, - (item) => item.number, - ), - 'number', + // Look at the state of the most recent StagingDeployCash, + // if it is open then we'll update the existing one, otherwise, we'll create a new one. + const mostRecentChecklist = recentDeployChecklists[0]; + const shouldCreateNewDeployChecklist = mostRecentChecklist.state !== 'open'; + const previousChecklist = shouldCreateNewDeployChecklist ? mostRecentChecklist : recentDeployChecklists[1]; + if (shouldCreateNewDeployChecklist) { + console.log('Latest StagingDeployCash is closed, creating a new one.', mostRecentChecklist); + } else { + console.log('Latest StagingDeployCash is open, updating it instead of creating a new one.', 'Current:', mostRecentChecklist, 'Previous:', previousChecklist); + } + + // Parse the data from the previous and current checklists into the format used to generate the checklist + const previousChecklistData = GithubUtils.getStagingDeployCashData(previousChecklist); + const currentChecklistData = shouldCreateNewDeployChecklist ? {} : GithubUtils.getStagingDeployCashData(mostRecentChecklist); + + // Find the list of PRs merged between the current checklist and the previous checklist + // Note that any time we're creating a new checklist we MUST have `NPM_VERSION` passed in as an input + const newTag = newVersion || _.get(currentChecklistData, 'tag'); + const mergedPRs = await GitUtils.getPullRequestsMergedBetween(previousChecklistData.tag, newTag); + + // Next, we generate the checklist body + let checklistBody = ''; + if (shouldCreateNewDeployChecklist) { + checklistBody = await GithubUtils.generateStagingDeployCashBody(newTag, _.map(mergedPRs, GithubUtils.getPullRequestURLFromNumber)); + } else { + // Generate the updated PR list, preserving the previous state of `isVerified` for existing PRs + const PRList = _.reduce( + mergedPRs, + (memo, prNum) => { + const indexOfPRInCurrentChecklist = _.findIndex(currentChecklistData.PRList, (pr) => pr.number === prNum); + const isVerified = indexOfPRInCurrentChecklist >= 0 ? currentChecklistData.PRList[indexOfPRInCurrentChecklist].isVerified : false; + memo.push({ + number: prNum, + url: GithubUtils.getPullRequestURLFromNumber(prNum), + isVerified, + }); + return memo; + }, + [], ); // Generate the deploy blocker list, preserving the previous state of `isResolved` - const deployBlockers = _.sortBy( - _.unique(_.union(currentStagingDeployCashData.deployBlockers, newDeployBlockers), false, (item) => item.number), - 'number', + const {data: openDeployBlockers} = await GithubUtils.octokit.issues.listForRepo({ + log: console, + owner: CONST.GITHUB_OWNER, + repo: CONST.APP_REPO, + labels: CONST.LABELS.DEPLOY_BLOCKER, + }); + const deployBlockers = _.reduce( + openDeployBlockers, + (memo, deployBlocker) => { + const {html_url, number} = deployBlocker; + const indexInCurrentChecklist = _.findIndex(currentChecklistData.deployBlockers, (item) => item.number === number); + const isResolved = indexInCurrentChecklist >= 0 ? currentChecklistData.deployBlockers[indexInCurrentChecklist].isResolved : false; + memo.push({ + number, + url: html_url, + isResolved, + }); + return memo; + }, + [], ); - // Get the internalQA PR list, preserving the previous state of `isResolved` - const internalQAPRList = _.sortBy(currentStagingDeployCashData.internalQAPRList, 'number'); - - return GithubUtils.generateStagingDeployCashBody( + const didVersionChange = newVersion ? newVersion !== currentChecklistData.tag : false; + checklistBody = await GithubUtils.generateStagingDeployCashBody( newTag, _.pluck(PRList, 'url'), _.pluck(_.where(PRList, {isVerified: true}), 'url'), _.pluck(deployBlockers, 'url'), _.pluck(_.where(deployBlockers, {isResolved: true}), 'url'), - _.pluck(_.where(internalQAPRList, {isResolved: true}), 'url'), - didVersionChange ? false : currentStagingDeployCashData.isTimingDashboardChecked, - didVersionChange ? false : currentStagingDeployCashData.isFirebaseChecked, - didVersionChange ? false : currentStagingDeployCashData.isGHStatusChecked, + _.pluck(_.where(currentChecklistData.internalQAPRList, {isResolved: true}), 'url'), + didVersionChange ? false : currentChecklistData.isTimingDashboardChecked, + didVersionChange ? false : currentChecklistData.isFirebaseChecked, + didVersionChange ? false : currentChecklistData.isGHStatusChecked, ); - }) - .then((body) => { - const defaultPayload = { - owner: CONST.GITHUB_OWNER, - repo: CONST.APP_REPO, - body, - }; + } - if (shouldCreateNewStagingDeployCash) { - return GithubUtils.octokit.issues.create({ - ...defaultPayload, - title: `Deploy Checklist: New Expensify ${moment().format('YYYY-MM-DD')}`, - labels: [CONST.LABELS.STAGING_DEPLOY], - assignees: [CONST.APPLAUSE_BOT], - }); - } + // Finally, create or update the checklist + const defaultPayload = { + owner: CONST.GITHUB_OWNER, + repo: CONST.APP_REPO, + body: checklistBody, + }; - return GithubUtils.octokit.issues.update({ + if (shouldCreateNewDeployChecklist) { + const {data: newChecklist} = await GithubUtils.octokit.issues.create({ ...defaultPayload, - issue_number: currentStagingDeployCashData.number, + title: `Deploy Checklist: New Expensify ${moment().format('YYYY-MM-DD')}`, + labels: [CONST.LABELS.STAGING_DEPLOY], + assignees: [CONST.APPLAUSE_BOT], }); - }) - .then(({data}) => { - // eslint-disable-next-line max-len - console.log(`Successfully ${shouldCreateNewStagingDeployCash ? 'created new' : 'updated'} StagingDeployCash! 🎉 ${data.html_url}`); - return data; - }) - .catch((err) => { - console.error('An unknown error occurred!', err); - core.setFailed(err); + console.log(`Successfully created new StagingDeployCash! 🎉 ${newChecklist.html_url}`); + return newChecklist; + } + + const {data: updatedChecklist} = await GithubUtils.octokit.issues.update({ + ...defaultPayload, + issue_number: currentChecklistData.number, }); -}; + console.log(`Successfully updated StagingDeployCash! 🎉 ${updatedChecklist.html_url}`); + return updatedChecklist; + } catch (err) { + console.error('An unknown error occurred!', err); + core.setFailed(err); + } +} if (require.main === module) { run(); diff --git a/.github/actions/javascript/createOrUpdateStagingDeploy/index.js b/.github/actions/javascript/createOrUpdateStagingDeploy/index.js index 9a015ef71e6b..ddf7a128e417 100644 --- a/.github/actions/javascript/createOrUpdateStagingDeploy/index.js +++ b/.github/actions/javascript/createOrUpdateStagingDeploy/index.js @@ -15,167 +15,127 @@ const CONST = __nccwpck_require__(4097); const GithubUtils = __nccwpck_require__(7999); const GitUtils = __nccwpck_require__(669); -const run = function () { +async function run() { const newVersion = core.getInput('NPM_VERSION'); console.log('New version found from action input:', newVersion); - let shouldCreateNewStagingDeployCash = false; - let newTag = newVersion; - let newDeployBlockers = []; - let previousStagingDeployCashData = {}; - let currentStagingDeployCashData = null; - - // Start by fetching the list of recent StagingDeployCash issues, along with the list of open deploy blockers - return Promise.all([ - GithubUtils.octokit.issues.listForRepo({ + try { + // Start by fetching the list of recent StagingDeployCash issues, along with the list of open deploy blockers + const {data: recentDeployChecklists} = await GithubUtils.octokit.issues.listForRepo({ log: console, owner: CONST.GITHUB_OWNER, repo: CONST.APP_REPO, labels: CONST.LABELS.STAGING_DEPLOY, state: 'all', - }), - GithubUtils.octokit.issues.listForRepo({ - log: console, - owner: CONST.GITHUB_OWNER, - repo: CONST.APP_REPO, - labels: CONST.LABELS.DEPLOY_BLOCKER, - }), - ]) - .then(([stagingDeployResponse, deployBlockerResponse]) => { - if (!stagingDeployResponse || !stagingDeployResponse.data || _.isEmpty(stagingDeployResponse.data)) { - console.error('Failed fetching StagingDeployCash issues from Github!', stagingDeployResponse); - throw new Error('Failed fetching StagingDeployCash issues from Github'); - } - - if (!deployBlockerResponse || !deployBlockerResponse.data) { - console.log('Failed fetching DeployBlockerCash issues from Github, continuing...'); - } - - newDeployBlockers = _.map(deployBlockerResponse.data, ({html_url}) => ({ - url: html_url, - number: GithubUtils.getIssueOrPullRequestNumberFromURL(html_url), - isResolved: false, - })); - - // Look at the state of the most recent StagingDeployCash, - // if it is open then we'll update the existing one, otherwise, we'll create a new one. - shouldCreateNewStagingDeployCash = Boolean(stagingDeployResponse.data[0].state !== 'open'); - if (shouldCreateNewStagingDeployCash) { - console.log('Latest StagingDeployCash is closed, creating a new one.', stagingDeployResponse.data[0]); - } else { - console.log( - 'Latest StagingDeployCash is open, updating it instead of creating a new one.', - 'Current:', - stagingDeployResponse.data[0], - 'Previous:', - stagingDeployResponse.data[1], - ); - } - - // Parse the data from the previous StagingDeployCash - // (newest if there are none open, otherwise second-newest) - previousStagingDeployCashData = shouldCreateNewStagingDeployCash - ? GithubUtils.getStagingDeployCashData(stagingDeployResponse.data[0]) - : GithubUtils.getStagingDeployCashData(stagingDeployResponse.data[1]); - - console.log('Found tag of previous StagingDeployCash:', previousStagingDeployCashData.tag); - - // Find the list of PRs merged between the last StagingDeployCash and the new version - if (shouldCreateNewStagingDeployCash) { - return GitUtils.getPullRequestsMergedBetween(previousStagingDeployCashData.tag, newTag); - } - - currentStagingDeployCashData = GithubUtils.getStagingDeployCashData(stagingDeployResponse.data[0]); - console.log('Parsed the following data from the current StagingDeployCash:', currentStagingDeployCashData); + }); - // If we aren't sent a tag, then use the existing tag - newTag = newTag || currentStagingDeployCashData.tag; + // Look at the state of the most recent StagingDeployCash, + // if it is open then we'll update the existing one, otherwise, we'll create a new one. + const mostRecentChecklist = recentDeployChecklists[0]; + const shouldCreateNewDeployChecklist = mostRecentChecklist.state !== 'open'; + const previousChecklist = shouldCreateNewDeployChecklist ? mostRecentChecklist : recentDeployChecklists[1]; + if (shouldCreateNewDeployChecklist) { + console.log('Latest StagingDeployCash is closed, creating a new one.', mostRecentChecklist); + } else { + console.log('Latest StagingDeployCash is open, updating it instead of creating a new one.', 'Current:', mostRecentChecklist, 'Previous:', previousChecklist); + } - return GitUtils.getPullRequestsMergedBetween(previousStagingDeployCashData.tag, newTag); - }) - .then((mergedPRs) => { - console.log(`The following PRs have been merged between the previous StagingDeployCash (${previousStagingDeployCashData.tag}) and new version (${newVersion}):`, mergedPRs); + // Parse the data from the previous and current checklists into the format used to generate the checklist + const previousChecklistData = GithubUtils.getStagingDeployCashData(previousChecklist); + const currentChecklistData = shouldCreateNewDeployChecklist ? {} : GithubUtils.getStagingDeployCashData(mostRecentChecklist); - if (shouldCreateNewStagingDeployCash) { - return GithubUtils.generateStagingDeployCashBody(newTag, _.map(mergedPRs, GithubUtils.getPullRequestURLFromNumber)); - } + // Find the list of PRs merged between the current checklist and the previous checklist + // Note that any time we're creating a new checklist we MUST have `NPM_VERSION` passed in as an input + const newTag = newVersion || _.get(currentChecklistData, 'tag'); + const mergedPRs = await GitUtils.getPullRequestsMergedBetween(previousChecklistData.tag, newTag); - const didVersionChange = newVersion ? newVersion !== currentStagingDeployCashData.tag : false; - - // Generate the PR list, preserving the previous state of `isVerified` for existing PRs - const PRList = _.sortBy( - _.unique( - _.union( - currentStagingDeployCashData.PRList, - _.map(mergedPRs, (number) => ({ - number, - url: GithubUtils.getPullRequestURLFromNumber(number), - - // Since this is the second argument to _.union, - // it will appear later in the array than any duplicate. - // Since it is later in the array, it will be truncated by _.unique, - // and the original value of isVerified will be preserved. - isVerified: false, - })), - ), - false, - (item) => item.number, - ), - 'number', + // Next, we generate the checklist body + let checklistBody = ''; + if (shouldCreateNewDeployChecklist) { + checklistBody = await GithubUtils.generateStagingDeployCashBody(newTag, _.map(mergedPRs, GithubUtils.getPullRequestURLFromNumber)); + } else { + // Generate the updated PR list, preserving the previous state of `isVerified` for existing PRs + const PRList = _.reduce( + mergedPRs, + (memo, prNum) => { + const indexOfPRInCurrentChecklist = _.findIndex(currentChecklistData.PRList, (pr) => pr.number === prNum); + const isVerified = indexOfPRInCurrentChecklist >= 0 ? currentChecklistData.PRList[indexOfPRInCurrentChecklist].isVerified : false; + memo.push({ + number: prNum, + url: GithubUtils.getPullRequestURLFromNumber(prNum), + isVerified, + }); + return memo; + }, + [], ); // Generate the deploy blocker list, preserving the previous state of `isResolved` - const deployBlockers = _.sortBy( - _.unique(_.union(currentStagingDeployCashData.deployBlockers, newDeployBlockers), false, (item) => item.number), - 'number', + const {data: openDeployBlockers} = await GithubUtils.octokit.issues.listForRepo({ + log: console, + owner: CONST.GITHUB_OWNER, + repo: CONST.APP_REPO, + labels: CONST.LABELS.DEPLOY_BLOCKER, + }); + const deployBlockers = _.reduce( + openDeployBlockers, + (memo, deployBlocker) => { + const {html_url, number} = deployBlocker; + const indexInCurrentChecklist = _.findIndex(currentChecklistData.deployBlockers, (item) => item.number === number); + const isResolved = indexInCurrentChecklist >= 0 ? currentChecklistData.deployBlockers[indexInCurrentChecklist].isResolved : false; + memo.push({ + number, + url: html_url, + isResolved, + }); + return memo; + }, + [], ); - // Get the internalQA PR list, preserving the previous state of `isResolved` - const internalQAPRList = _.sortBy(currentStagingDeployCashData.internalQAPRList, 'number'); - - return GithubUtils.generateStagingDeployCashBody( + const didVersionChange = newVersion ? newVersion !== currentChecklistData.tag : false; + checklistBody = await GithubUtils.generateStagingDeployCashBody( newTag, _.pluck(PRList, 'url'), _.pluck(_.where(PRList, {isVerified: true}), 'url'), _.pluck(deployBlockers, 'url'), _.pluck(_.where(deployBlockers, {isResolved: true}), 'url'), - _.pluck(_.where(internalQAPRList, {isResolved: true}), 'url'), - didVersionChange ? false : currentStagingDeployCashData.isTimingDashboardChecked, - didVersionChange ? false : currentStagingDeployCashData.isFirebaseChecked, - didVersionChange ? false : currentStagingDeployCashData.isGHStatusChecked, + _.pluck(_.where(currentChecklistData.internalQAPRList, {isResolved: true}), 'url'), + didVersionChange ? false : currentChecklistData.isTimingDashboardChecked, + didVersionChange ? false : currentChecklistData.isFirebaseChecked, + didVersionChange ? false : currentChecklistData.isGHStatusChecked, ); - }) - .then((body) => { - const defaultPayload = { - owner: CONST.GITHUB_OWNER, - repo: CONST.APP_REPO, - body, - }; + } - if (shouldCreateNewStagingDeployCash) { - return GithubUtils.octokit.issues.create({ - ...defaultPayload, - title: `Deploy Checklist: New Expensify ${moment().format('YYYY-MM-DD')}`, - labels: [CONST.LABELS.STAGING_DEPLOY], - assignees: [CONST.APPLAUSE_BOT], - }); - } + // Finally, create or update the checklist + const defaultPayload = { + owner: CONST.GITHUB_OWNER, + repo: CONST.APP_REPO, + body: checklistBody, + }; - return GithubUtils.octokit.issues.update({ + if (shouldCreateNewDeployChecklist) { + const {data: newChecklist} = await GithubUtils.octokit.issues.create({ ...defaultPayload, - issue_number: currentStagingDeployCashData.number, + title: `Deploy Checklist: New Expensify ${moment().format('YYYY-MM-DD')}`, + labels: [CONST.LABELS.STAGING_DEPLOY], + assignees: [CONST.APPLAUSE_BOT], }); - }) - .then(({data}) => { - // eslint-disable-next-line max-len - console.log(`Successfully ${shouldCreateNewStagingDeployCash ? 'created new' : 'updated'} StagingDeployCash! 🎉 ${data.html_url}`); - return data; - }) - .catch((err) => { - console.error('An unknown error occurred!', err); - core.setFailed(err); + console.log(`Successfully created new StagingDeployCash! 🎉 ${newChecklist.html_url}`); + return newChecklist; + } + + const {data: updatedChecklist} = await GithubUtils.octokit.issues.update({ + ...defaultPayload, + issue_number: currentChecklistData.number, }); -}; + console.log(`Successfully updated StagingDeployCash! 🎉 ${updatedChecklist.html_url}`); + return updatedChecklist; + } catch (err) { + console.error('An unknown error occurred!', err); + core.setFailed(err); + } +} if (require.main === require.cache[eval('__filename')]) { run(); diff --git a/tests/unit/createOrUpdateStagingDeployTest.js b/tests/unit/createOrUpdateStagingDeployTest.js index 679653fb2822..9e1b9f23af70 100644 --- a/tests/unit/createOrUpdateStagingDeployTest.js +++ b/tests/unit/createOrUpdateStagingDeployTest.js @@ -94,8 +94,6 @@ const basePRList = [ 'https://github.com/Expensify/App/pull/8', 'https://github.com/Expensify/App/pull/9', 'https://github.com/Expensify/App/pull/10', - 'https://github.com/Expensify/App/issues/11', - 'https://github.com/Expensify/App/issues/12', ]; const baseIssueList = ['https://github.com/Expensify/App/issues/11', 'https://github.com/Expensify/App/issues/12']; @@ -138,7 +136,7 @@ describe('createOrUpdateStagingDeployCash', () => { `${lineBreakDouble}${ccApplauseLeads}`, }; - const baseNewPullRequests = ['6', '7', '8']; + const baseNewPullRequests = [6, 7, 8]; test('creates new issue when there is none open', () => { mockGetInput.mockImplementation((arg) => { @@ -220,13 +218,13 @@ describe('createOrUpdateStagingDeployCash', () => { labels: [LABELS.DEPLOY_BLOCKER_CASH], }, { - html_url: 'https://github.com/Expensify/App/issues/9', + html_url: 'https://github.com/Expensify/App/pull/9', number: 9, state: 'open', labels: [LABELS.DEPLOY_BLOCKER_CASH], }, { - html_url: 'https://github.com/Expensify/App/issues/10', + html_url: 'https://github.com/Expensify/App/pull/10', number: 10, state: 'closed', labels: [LABELS.DEPLOY_BLOCKER_CASH], @@ -245,7 +243,7 @@ describe('createOrUpdateStagingDeployCash', () => { }); // New pull requests to add to open StagingDeployCash - const newPullRequests = ['9', '10']; + const newPullRequests = [9, 10]; mockGetPullRequestsMergedBetween.mockImplementation((fromRef, toRef) => { if (fromRef === '1.0.1-0' && toRef === '1.0.2-2') { return [...baseNewPullRequests, ...newPullRequests]; @@ -300,8 +298,8 @@ describe('createOrUpdateStagingDeployCash', () => { `${lineBreak}${openCheckbox}${basePRList[5]}` + `${lineBreak}${openCheckbox}${basePRList[8]}` + `${lineBreak}${closedCheckbox}${basePRList[9]}` + - `${lineBreak}${openCheckbox}${basePRList[10]}` + - `${lineBreak}${openCheckbox}${basePRList[11]}${lineBreak}` + + `${lineBreak}${openCheckbox}${baseIssueList[0]}` + + `${lineBreak}${openCheckbox}${baseIssueList[1]}${lineBreak}` + `${lineBreakDouble}${deployerVerificationsHeader}` + // Note: these will be unchecked with a new app version, and that's intentional `${lineBreak}${openCheckbox}${timingDashboardVerification}` + @@ -320,7 +318,7 @@ describe('createOrUpdateStagingDeployCash', () => { return 'fake_token'; }); mockGetPullRequestsMergedBetween.mockImplementation((fromRef, toRef) => { - if (fromRef === '1.0.1-0' && toRef === '1.0.2-2') { + if (fromRef === '1.0.1-0' && toRef === '1.0.2-1') { return [...baseNewPullRequests]; } return []; From 0b633c36f8dd9e05fa8f642a615ec1efdb55db4b Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 30 Aug 2023 17:05:57 +0200 Subject: [PATCH 3/3] Refactor test to use async/await as well --- tests/unit/createOrUpdateStagingDeployTest.js | 147 +++++++++--------- 1 file changed, 72 insertions(+), 75 deletions(-) diff --git a/tests/unit/createOrUpdateStagingDeployTest.js b/tests/unit/createOrUpdateStagingDeployTest.js index 9e1b9f23af70..54c914f2500b 100644 --- a/tests/unit/createOrUpdateStagingDeployTest.js +++ b/tests/unit/createOrUpdateStagingDeployTest.js @@ -138,7 +138,7 @@ describe('createOrUpdateStagingDeployCash', () => { const baseNewPullRequests = [6, 7, 8]; - test('creates new issue when there is none open', () => { + test('creates new issue when there is none open', async () => { mockGetInput.mockImplementation((arg) => { if (arg === 'GITHUB_TOKEN') { return 'fake_token'; @@ -164,25 +164,24 @@ describe('createOrUpdateStagingDeployCash', () => { return {data: []}; }); - return run().then((result) => { - expect(result).toStrictEqual({ - owner: CONST.GITHUB_OWNER, - repo: CONST.APP_REPO, - title: `Deploy Checklist: New Expensify ${moment().format('YYYY-MM-DD')}`, - labels: [CONST.LABELS.STAGING_DEPLOY], - html_url: 'https://github.com/Expensify/App/issues/29', - assignees: [CONST.APPLAUSE_BOT], - body: - `${baseExpectedOutput()}` + - `${openCheckbox}${basePRList[5]}` + - `${lineBreak}${openCheckbox}${basePRList[6]}` + - `${lineBreak}${openCheckbox}${basePRList[7]}${lineBreak}` + - `${lineBreakDouble}${deployerVerificationsHeader}` + - `${lineBreak}${openCheckbox}${timingDashboardVerification}` + - `${lineBreak}${openCheckbox}${firebaseVerification}` + - `${lineBreak}${openCheckbox}${ghVerification}` + - `${lineBreakDouble}${ccApplauseLeads}`, - }); + const result = await run(); + expect(result).toStrictEqual({ + owner: CONST.GITHUB_OWNER, + repo: CONST.APP_REPO, + title: `Deploy Checklist: New Expensify ${moment().format('YYYY-MM-DD')}`, + labels: [CONST.LABELS.STAGING_DEPLOY], + html_url: 'https://github.com/Expensify/App/issues/29', + assignees: [CONST.APPLAUSE_BOT], + body: + `${baseExpectedOutput()}` + + `${openCheckbox}${basePRList[5]}` + + `${lineBreak}${openCheckbox}${basePRList[6]}` + + `${lineBreak}${openCheckbox}${basePRList[7]}${lineBreak}` + + `${lineBreakDouble}${deployerVerificationsHeader}` + + `${lineBreak}${openCheckbox}${timingDashboardVerification}` + + `${lineBreak}${openCheckbox}${firebaseVerification}` + + `${lineBreak}${openCheckbox}${ghVerification}` + + `${lineBreakDouble}${ccApplauseLeads}`, }); }); @@ -231,7 +230,7 @@ describe('createOrUpdateStagingDeployCash', () => { }, ]; - test('with NPM_VERSION input, pull requests, and deploy blockers', () => { + test('with NPM_VERSION input, pull requests, and deploy blockers', async () => { mockGetInput.mockImplementation((arg) => { if (arg === 'GITHUB_TOKEN') { return 'fake_token'; @@ -279,38 +278,37 @@ describe('createOrUpdateStagingDeployCash', () => { return {data: []}; }); - return run().then((result) => { - expect(result).toStrictEqual({ - owner: CONST.GITHUB_OWNER, - repo: CONST.APP_REPO, - issue_number: openStagingDeployCashBefore.number, - // eslint-disable-next-line max-len - html_url: `https://github.com/Expensify/App/issues/${openStagingDeployCashBefore.number}`, - // eslint-disable-next-line max-len - body: - `${baseExpectedOutput('1.0.2-2')}` + - `${openCheckbox}${basePRList[5]}` + - `${lineBreak}${closedCheckbox}${basePRList[6]}` + - `${lineBreak}${openCheckbox}${basePRList[7]}` + - `${lineBreak}${openCheckbox}${basePRList[8]}` + - `${lineBreak}${openCheckbox}${basePRList[9]}${lineBreak}` + - `${lineBreakDouble}${deployBlockerHeader}` + - `${lineBreak}${openCheckbox}${basePRList[5]}` + - `${lineBreak}${openCheckbox}${basePRList[8]}` + - `${lineBreak}${closedCheckbox}${basePRList[9]}` + - `${lineBreak}${openCheckbox}${baseIssueList[0]}` + - `${lineBreak}${openCheckbox}${baseIssueList[1]}${lineBreak}` + - `${lineBreakDouble}${deployerVerificationsHeader}` + - // Note: these will be unchecked with a new app version, and that's intentional - `${lineBreak}${openCheckbox}${timingDashboardVerification}` + - `${lineBreak}${openCheckbox}${firebaseVerification}` + - `${lineBreak}${openCheckbox}${ghVerification}` + - `${lineBreakDouble}${ccApplauseLeads}`, - }); + const result = await run(); + expect(result).toStrictEqual({ + owner: CONST.GITHUB_OWNER, + repo: CONST.APP_REPO, + issue_number: openStagingDeployCashBefore.number, + // eslint-disable-next-line max-len + html_url: `https://github.com/Expensify/App/issues/${openStagingDeployCashBefore.number}`, + // eslint-disable-next-line max-len + body: + `${baseExpectedOutput('1.0.2-2')}` + + `${openCheckbox}${basePRList[5]}` + + `${lineBreak}${closedCheckbox}${basePRList[6]}` + + `${lineBreak}${openCheckbox}${basePRList[7]}` + + `${lineBreak}${openCheckbox}${basePRList[8]}` + + `${lineBreak}${openCheckbox}${basePRList[9]}${lineBreak}` + + `${lineBreakDouble}${deployBlockerHeader}` + + `${lineBreak}${openCheckbox}${basePRList[5]}` + + `${lineBreak}${openCheckbox}${basePRList[8]}` + + `${lineBreak}${closedCheckbox}${basePRList[9]}` + + `${lineBreak}${openCheckbox}${baseIssueList[0]}` + + `${lineBreak}${openCheckbox}${baseIssueList[1]}${lineBreak}` + + `${lineBreakDouble}${deployerVerificationsHeader}` + + // Note: these will be unchecked with a new app version, and that's intentional + `${lineBreak}${openCheckbox}${timingDashboardVerification}` + + `${lineBreak}${openCheckbox}${firebaseVerification}` + + `${lineBreak}${openCheckbox}${ghVerification}` + + `${lineBreakDouble}${ccApplauseLeads}`, }); }); - test('without NPM_VERSION input, just a new deploy blocker', () => { + test('without NPM_VERSION input, just a new deploy blocker', async () => { mockGetInput.mockImplementation((arg) => { if (arg !== 'GITHUB_TOKEN') { return; @@ -351,31 +349,30 @@ describe('createOrUpdateStagingDeployCash', () => { return {data: []}; }); - return run().then((result) => { - expect(result).toStrictEqual({ - owner: CONST.GITHUB_OWNER, - repo: CONST.APP_REPO, - issue_number: openStagingDeployCashBefore.number, - // eslint-disable-next-line max-len - html_url: `https://github.com/Expensify/App/issues/${openStagingDeployCashBefore.number}`, - // eslint-disable-next-line max-len - body: - `${baseExpectedOutput('1.0.2-1')}` + - `${openCheckbox}${basePRList[5]}` + - `${lineBreak}${closedCheckbox}${basePRList[6]}` + - `${lineBreak}${openCheckbox}${basePRList[7]}${lineBreak}` + - `${lineBreakDouble}${deployBlockerHeader}` + - `${lineBreak}${openCheckbox}${basePRList[5]}` + - `${lineBreak}${openCheckbox}${basePRList[8]}` + - `${lineBreak}${closedCheckbox}${basePRList[9]}` + - `${lineBreak}${openCheckbox}${baseIssueList[0]}` + - `${lineBreak}${openCheckbox}${baseIssueList[1]}${lineBreak}` + - `${lineBreakDouble}${deployerVerificationsHeader}` + - `${lineBreak}${closedCheckbox}${timingDashboardVerification}` + - `${lineBreak}${closedCheckbox}${firebaseVerification}` + - `${lineBreak}${closedCheckbox}${ghVerification}` + - `${lineBreakDouble}${ccApplauseLeads}`, - }); + const result = await run(); + expect(result).toStrictEqual({ + owner: CONST.GITHUB_OWNER, + repo: CONST.APP_REPO, + issue_number: openStagingDeployCashBefore.number, + // eslint-disable-next-line max-len + html_url: `https://github.com/Expensify/App/issues/${openStagingDeployCashBefore.number}`, + // eslint-disable-next-line max-len + body: + `${baseExpectedOutput('1.0.2-1')}` + + `${openCheckbox}${basePRList[5]}` + + `${lineBreak}${closedCheckbox}${basePRList[6]}` + + `${lineBreak}${openCheckbox}${basePRList[7]}${lineBreak}` + + `${lineBreakDouble}${deployBlockerHeader}` + + `${lineBreak}${openCheckbox}${basePRList[5]}` + + `${lineBreak}${openCheckbox}${basePRList[8]}` + + `${lineBreak}${closedCheckbox}${basePRList[9]}` + + `${lineBreak}${openCheckbox}${baseIssueList[0]}` + + `${lineBreak}${openCheckbox}${baseIssueList[1]}${lineBreak}` + + `${lineBreakDouble}${deployerVerificationsHeader}` + + `${lineBreak}${closedCheckbox}${timingDashboardVerification}` + + `${lineBreak}${closedCheckbox}${firebaseVerification}` + + `${lineBreak}${closedCheckbox}${ghVerification}` + + `${lineBreakDouble}${ccApplauseLeads}`, }); }); });