Skip to content

Commit

Permalink
fix(PPDSC-2611): fix pipeline (#596)
Browse files Browse the repository at this point in the history
* fix(PPDSC-2611): fix pipeline

* fix(PPDSC-2611): update pipeline logic

* fix(PPDSC-2611): correct loop
  • Loading branch information
mstuartf authored Feb 7, 2023
1 parent 204bdd1 commit 1c001a9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
26 changes: 17 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
12 changes: 7 additions & 5 deletions scripts/__tests__/check-baseline-updates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
30 changes: 23 additions & 7 deletions scripts/check-baseline-updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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}`},
Expand All @@ -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`);

Expand All @@ -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};

0 comments on commit 1c001a9

Please sign in to comment.