From 438b5f605e9fc2d31b23d0208328db68c1044525 Mon Sep 17 00:00:00 2001 From: mikef Date: Mon, 20 Feb 2023 16:41:40 +0000 Subject: [PATCH] fix(PPDSC-2611): Revert "chore(PPDSC-2611): fewer tests for baseline updates (#588)" This reverts commit fd4d458849c729974aec1ea8a087fad642543ede. --- .circleci/config.yml | 40 ++++---- jest.config.js | 6 +- percy-storybook.config.json | 1 - percy.config.js | 2 - .../__tests__/check-baseline-updates.test.js | 99 ------------------- scripts/check-baseline-updates.js | 50 ++++------ scripts/jest.config.js | 5 - 7 files changed, 37 insertions(+), 166 deletions(-) delete mode 100644 percy-storybook.config.json delete mode 100644 scripts/__tests__/check-baseline-updates.test.js delete mode 100644 scripts/jest.config.js diff --git a/.circleci/config.yml b/.circleci/config.yml index 0dc2fd2239..4d9f611a26 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -803,17 +803,15 @@ jobs: - run: name: 'Check if updates required' command: | - export PR_BRANCH_NAME=$(gh pr list --search $CIRCLE_SHA1 --state merged --json headRefName --jq '.[].headRefName') - echo "Found branch name: ${PR_BRANCH_NAME}" - export PERCY_TOKEN=$PERCY_COMPS_TOKEN - export UPDATES_REQUIRED=$(npx -y run-func scripts/check-baseline-updates.js run $PR_BRANCH_NAME) - if [ $UPDATES_REQUIRED = "true" ]; then - echo "Baseline updates required; continuing job." - export PERCY_PARTIAL_BUILD=1 - else - echo "No baseline updates required; finishing job." && - circleci-agent step halt - fi + { + export PR_BRANCH_NAME=$(gh pr list --search $CIRCLE_SHA1 --state merged --json headRefName --jq '.[].headRefName') && + echo "Found branch name: ${PR_BRANCH_NAME}" && + node scripts/check-baseline-updates.js $PR_BRANCH_NAME "comps" && + echo "No baseline updates required; finishing job." && + circleci-agent step halt + } || { + echo "Baseline updates required; continuing job." + } - run_test_visual_comps_percy - slack/notify: <<: *slack_notify @@ -828,17 +826,15 @@ jobs: - run: name: 'Check if updates required' command: | - export PR_BRANCH_NAME=$(gh pr list --search $CIRCLE_SHA1 --state merged --json headRefName --jq '.[].headRefName') - echo "Found branch name: ${PR_BRANCH_NAME}" - export PERCY_TOKEN=$PERCY_COMPS_TOKEN - export UPDATES_REQUIRED=$(npx -y run-func scripts/check-baseline-updates.js run $PR_BRANCH_NAME) - if [ $UPDATES_REQUIRED = "true" ]; then - echo "Baseline updates required; continuing job." - export PERCY_PARTIAL_BUILD=1 - else - echo "No baseline updates required; finishing job." && - circleci-agent step halt - fi + { + export PR_BRANCH_NAME=$(gh pr list --search $CIRCLE_SHA1 --state merged --json headRefName --jq '.[].headRefName') && + echo "Found branch name: ${PR_BRANCH_NAME}" && + node scripts/check-baseline-updates.js $PR_BRANCH_NAME "docsite" && + echo "No baseline updates required; finishing job." && + circleci-agent step halt + } || { + echo "Baseline updates required; continuing job." + } - run_test_visual_docs_percy - slack/notify: <<: *slack_notify diff --git a/jest.config.js b/jest.config.js index 1a4b3c2d49..ad394f8e69 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,9 +1,5 @@ module.exports = { - projects: [ - '/src/jest.config.js', - '/site/jest.config.js', - '/scripts/jest.config.js', - ], + projects: ['/src/jest.config.js', '/site/jest.config.js'], coveragePathIgnorePatterns: ['/node_modules/', '/test'], coverageReporters: ['lcov', 'text-summary'], collectCoverage: true, diff --git a/percy-storybook.config.json b/percy-storybook.config.json deleted file mode 100644 index 0967ef424b..0000000000 --- a/percy-storybook.config.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/percy.config.js b/percy.config.js index 11e2d3d771..a273842d07 100644 --- a/percy.config.js +++ b/percy.config.js @@ -1,5 +1,4 @@ const SKIP_GITHUB_CHECK = process.env.SKIP_PERCY_CHECK === 'true'; -const storybookConfig = require('./percy-storybook.config.json'); module.exports = { version: 2, @@ -7,7 +6,6 @@ module.exports = { widths: [375, 1280], }, storybook: { - ...storybookConfig, ...(SKIP_GITHUB_CHECK ? {include: 'skip-percy-tests'} : {}), }, }; diff --git a/scripts/__tests__/check-baseline-updates.test.js b/scripts/__tests__/check-baseline-updates.test.js deleted file mode 100644 index e9a5d563e2..0000000000 --- a/scripts/__tests__/check-baseline-updates.test.js +++ /dev/null @@ -1,99 +0,0 @@ -const https = require('https'); -const fs = require('fs'); -const Stream = require('stream'); - -const {run} = require('../check-baseline-updates'); - -const BUILDS = { - data: [ - { - attributes: { - branch: 'branch/approved-with-diffs', - 'review-state': 'approved', - 'total-comparisons-diff': '3', - }, - relationships: { - snapshots: { - links: { - related: 'branch/approved-with-diffs-related', - }, - }, - }, - }, - { - attributes: { - branch: 'branch/approved-no-diffs', - 'review-state': 'approved', - 'total-comparisons-diff': 0, - }, - }, - { - attributes: { - branch: 'branch/diffs-not-approved', - 'review-state': 'pending', - 'total-comparisons-diff': 0, - }, - }, - ], -}; - -const SNAPSHOTS = { - data: [ - { - attributes: { - 'review-state-reason': 'user_approved', - name: 'snapshot1', - }, - }, - { - attributes: { - 'review-state-reason': 'user_approved', - name: 'snapshot2', - }, - }, - ], -}; - -const mockHttp = (url, options, callback) => { - const streamStream = new Stream(); - callback(streamStream); - const data = url.includes('/api/v1/builds') ? BUILDS : SNAPSHOTS; - streamStream.emit('data', JSON.stringify(data)); - streamStream.emit('end'); -}; - -describe('check-baseline-updates', () => { - beforeEach(() => { - process.env.PERCY_TOKEN = '__PERCY_TOKEN__'; - https.get = jest.fn().mockImplementation(mockHttp); - }); - - it('should update config file and return true if updates required', async () => { - fs.writeFileSync = jest.fn(); - const result = await run('branch/approved-with-diffs'); - expect(fs.writeFileSync).toHaveBeenCalledWith( - './percy-storybook.config.json', - JSON.stringify({include: ['^snapshot1$', '^snapshot2$']}), - ); - expect(result).toBe(true); - }); - - it('should return false if no diffs require updates', async () => { - const result = await run('branch/approved-no-diffs'); - expect(result).toBe(false); - }); - - it('should return false if build not approved', async () => { - const result = await run('branch/diffs-not-approved'); - expect(result).toBe(false); - }); - - it('should raise an exception if no token found', async () => { - delete process.env.PERCY_TOKEN; - await expect(run('branch/approved-no-diffs')).rejects.toThrowError(); - }); - - it('should raise an exception if build not found', async () => { - await expect(run('invalid-branch')).rejects.toThrowError(); - }); -}); diff --git a/scripts/check-baseline-updates.js b/scripts/check-baseline-updates.js index 9e721b09b5..2c76bbd082 100644 --- a/scripts/check-baseline-updates.js +++ b/scripts/check-baseline-updates.js @@ -1,10 +1,7 @@ #!/usr/bin/env node const https = require('https'); -const fs = require('fs'); -const PERCY_URL = 'https://percy.io'; - -const CONFIG_FILE = 'percy-storybook.config.json'; +const PERCY_URL = 'https://percy.io/api/v1'; const log = value => process.stdout.write(`${value}\n`); @@ -24,20 +21,16 @@ function apiCall(url, options) { }); } -function percyApiCall(path) { - const token = process.env.PERCY_TOKEN; +async function getPercyBuildForBranch(branchName, project) { + log(`Looking for Percy ${project} build for branch ${branchName}`); + const token = process.env[`PERCY_${project.toUpperCase()}_TOKEN`]; if (!token) { - log(`No Percy token found`); + log(`No Percy token found for project ${project}`); throw Error(); } - return apiCall(`${PERCY_URL}${path}`, { + const builds = await apiCall(`${PERCY_URL}/builds`, { headers: {Authorization: `Token ${token}`}, }); -} - -async function getPercyBuildForBranch(branchName) { - log(`Looking for Percy build for branch ${branchName}`); - const builds = await percyApiCall('/api/v1/builds'); for (let i = 0; i <= builds.data.length; i++) { const build = builds.data[i]; if (build.attributes.branch === branchName) { @@ -48,33 +41,26 @@ async function getPercyBuildForBranch(branchName) { throw Error(); } -async function run(headRefName) { +async function checkIfBaselineUpdatesRequired(headRefName, project) { const branchName = headRefName.trim(); - log(`Checking if baselines to be updated after ${branchName} was merged`); + log( + `Checking if baselines for ${project} need to be updated after ${branchName} was merged`, + ); - const build = await getPercyBuildForBranch(branchName); + const build = await getPercyBuildForBranch(branchName, project); const reviewState = build.attributes['review-state']; const nbDiffs = build.attributes['total-comparisons-diff']; log(`Build is in state ${reviewState} with ${nbDiffs} diffs`); if (reviewState !== 'approved' || !nbDiffs) { log(`No diffs requiring updates`); - return false; + throw Error(); } - - const snapshots = await percyApiCall( - build.relationships.snapshots.links.related, - ); - - const include = snapshots.data - .filter( - ({attributes}) => attributes['review-state-reason'] === 'user_approved', - ) - .map(({attributes: {name}}) => `^${name}$`); - - fs.writeFileSync(`./${CONFIG_FILE}`, JSON.stringify({include})); - - return true; } -module.exports = {run}; +// this script fails if updates are required +const headRefName = process.argv[2]; +const project = process.argv[3]; +checkIfBaselineUpdatesRequired(headRefName, project) + .then(() => process.exit(1)) + .catch(() => process.exit(0)); diff --git a/scripts/jest.config.js b/scripts/jest.config.js deleted file mode 100644 index f5f0f43713..0000000000 --- a/scripts/jest.config.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = { - name: 'scripts', - displayName: 'NewsKit Scripts', - testRegex: '(.|-)test\\.js$', -};