From 1c001a941dc7aedaacd87fc9c8fd3c271cff21d4 Mon Sep 17 00:00:00 2001 From: mstuartf Date: Tue, 7 Feb 2023 17:49:01 +0000 Subject: [PATCH] fix(PPDSC-2611): fix pipeline (#596) * fix(PPDSC-2611): fix pipeline * fix(PPDSC-2611): update pipeline logic * fix(PPDSC-2611): correct loop --- .circleci/config.yml | 26 ++++++++++------ .../__tests__/check-baseline-updates.test.js | 12 ++++---- scripts/check-baseline-updates.js | 30 ++++++++++++++----- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 80721fb45f..3062e9c1c8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -631,13 +631,17 @@ jobs: 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 + export CHECK_UPDATES=$(npx -y run-func scripts/check-baseline-updates.js run $PR_BRANCH_NAME) + echo "Result from check-baseline-updates.js: ${CHECK_UPDATES}" + if [ $CHECK_UPDATES = "UPDATES_REQUIRED" ]; then echo "Baseline updates required; continuing job." export PERCY_PARTIAL_BUILD=1 - else - echo "No baseline updates required; finishing job." && + elif [ $CHECK_UPDATES = "NO_UPDATES_REQUIRED" ]; then + echo "No baseline updates required; finishing job." circleci-agent step halt + else + echo "Error checking PR" + exit 1 fi - run_test_visual_comps_percy - slack/notify: @@ -655,14 +659,18 @@ jobs: 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 + export PERCY_TOKEN=$PERCY_DOCSITE_TOKEN + export CHECK_UPDATES=$(npx -y run-func scripts/check-baseline-updates.js run $PR_BRANCH_NAME) + echo "Result from check-baseline-updates.js: ${CHECK_UPDATES}" + if [ $CHECK_UPDATES = "UPDATES_REQUIRED" ]; then echo "Baseline updates required; continuing job." export PERCY_PARTIAL_BUILD=1 - else - echo "No baseline updates required; finishing job." && + elif [ $CHECK_UPDATES = "NO_UPDATES_REQUIRED" ]; then + echo "No baseline updates required; finishing job." circleci-agent step halt + else + echo "Error checking PR" + exit 1 fi - run_test_visual_docs_percy - slack/notify: diff --git a/scripts/__tests__/check-baseline-updates.test.js b/scripts/__tests__/check-baseline-updates.test.js index e9a5d563e2..ee8ea980c0 100644 --- a/scripts/__tests__/check-baseline-updates.test.js +++ b/scripts/__tests__/check-baseline-updates.test.js @@ -75,25 +75,27 @@ describe('check-baseline-updates', () => { './percy-storybook.config.json', JSON.stringify({include: ['^snapshot1$', '^snapshot2$']}), ); - expect(result).toBe(true); + expect(result).toBe('UPDATES_REQUIRED'); }); it('should return false if no diffs require updates', async () => { const result = await run('branch/approved-no-diffs'); - expect(result).toBe(false); + expect(result).toBe('NO_UPDATES_REQUIRED'); }); it('should return false if build not approved', async () => { const result = await run('branch/diffs-not-approved'); - expect(result).toBe(false); + expect(result).toBe('NO_UPDATES_REQUIRED'); }); it('should raise an exception if no token found', async () => { delete process.env.PERCY_TOKEN; - await expect(run('branch/approved-no-diffs')).rejects.toThrowError(); + const result = await run('approved-no-diffs'); + expect(result).toBe('ERROR'); }); it('should raise an exception if build not found', async () => { - await expect(run('invalid-branch')).rejects.toThrowError(); + const result = await run('invalid-branch'); + expect(result).toBe('ERROR'); }); }); diff --git a/scripts/check-baseline-updates.js b/scripts/check-baseline-updates.js index 9e721b09b5..8fcd7156b5 100644 --- a/scripts/check-baseline-updates.js +++ b/scripts/check-baseline-updates.js @@ -6,7 +6,14 @@ const PERCY_URL = 'https://percy.io'; const CONFIG_FILE = 'percy-storybook.config.json'; -const log = value => process.stdout.write(`${value}\n`); +// Use stderr to stop logs being returned to parent process +const log = value => process.stderr.write(`${value}\n`); + +const RESPONSES = { + ERROR: 'ERROR', + UPDATES_REQUIRED: 'UPDATES_REQUIRED', + NO_UPDATES_REQUIRED: 'NO_UPDATES_REQUIRED', +}; function apiCall(url, options) { return new Promise((resolve, reject) => { @@ -27,8 +34,7 @@ function apiCall(url, options) { function percyApiCall(path) { const token = process.env.PERCY_TOKEN; if (!token) { - log(`No Percy token found`); - throw Error(); + throw Error(`No Percy token found`); } return apiCall(`${PERCY_URL}${path}`, { headers: {Authorization: `Token ${token}`}, @@ -38,17 +44,16 @@ function percyApiCall(path) { 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++) { + for (let i = 0; i < builds.data.length; i++) { const build = builds.data[i]; if (build.attributes.branch === branchName) { return build; } } - log(`No Percy build found for branch ${branchName}`); - throw Error(); + throw Error(`No Percy build found for branch ${branchName}`); } -async function run(headRefName) { +async function checkUpdates(headRefName) { const branchName = headRefName.trim(); log(`Checking if baselines to be updated after ${branchName} was merged`); @@ -72,9 +77,20 @@ async function run(headRefName) { ) .map(({attributes: {name}}) => `^${name}$`); + log(`Updating ${CONFIG_FILE}`); fs.writeFileSync(`./${CONFIG_FILE}`, JSON.stringify({include})); return true; } +const run = async headRefName => + checkUpdates(headRefName) + .then(res => + res ? RESPONSES.UPDATES_REQUIRED : RESPONSES.NO_UPDATES_REQUIRED, + ) + .catch(err => { + log(err); + return RESPONSES.ERROR; + }); + module.exports = {run};