Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gi-flow #430

Merged
merged 51 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
356e5e8
gi-flow
vradulescu-bd Nov 19, 2024
b1c1c0f
gi-flow
vradulescu-bd Nov 19, 2024
c5fd97a
gi-flow
vradulescu-bd Nov 19, 2024
72c13c1
gi-flow
vradulescu-bd Nov 19, 2024
8381aed
gi-flow
vradulescu-bd Nov 19, 2024
48c51af
Update workflow files [skip ci]
vradulescu-bd Nov 19, 2024
edd55ba
gi-flow
vradulescu-bd Nov 19, 2024
fb1007c
gi-flow
vradulescu-bd Nov 19, 2024
5c10e7b
gi-flow
vradulescu-bd Nov 19, 2024
d46b998
temporary skip check for aem-psi-check
vradulescu-bd Nov 19, 2024
c63bfd4
temporary mock snapshot testing
vradulescu-bd Nov 19, 2024
422d442
Acquire Lock - fix
vradulescu-bd Nov 19, 2024
6632f78
Verify Approvals - temp remove
vradulescu-bd Nov 19, 2024
52ab4e6
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
525409d
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
a9ded68
Lock for baseline update by PR #430
actions-user Nov 19, 2024
10de4de
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
e2e57d6
Merge remote-tracking branch 'origin/gi-flow' into gi-flow
vradulescu-bd Nov 19, 2024
4ec368c
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
d38aeb7
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
2fa8e60
Lock for baseline update by PR #430
actions-user Nov 19, 2024
1687660
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
e7c5a08
Lock for baseline update by PR #430
actions-user Nov 19, 2024
abe003e
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
80ec341
Merge remote-tracking branch 'origin/gi-flow' into gi-flow
vradulescu-bd Nov 19, 2024
88f0770
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
e4f8cf9
Lock for baseline update by PR #430
actions-user Nov 19, 2024
a6d16ea
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
6c4c163
Lock for baseline update by PR #430
actions-user Nov 19, 2024
031f500
Lock for baseline update by PR #430 [skip husky]
actions-user Nov 19, 2024
b38ba8c
Release lock for baseline update [skip husky]
actions-user Nov 19, 2024
ead8024
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
1326fb8
Lock for baseline update by PR #430
actions-user Nov 19, 2024
095e7cc
Lock for baseline update by PR #430 [skip husky]
actions-user Nov 19, 2024
89d5ddd
Release lock for baseline update [skip husky]
actions-user Nov 19, 2024
8359f4e
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
001d221
Acquire Lock - temp change to local branch
vradulescu-bd Nov 19, 2024
381d3bc
Merge branch 'main' into gi-flow
vradulescu-bd Nov 19, 2024
479b76a
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 19, 2024
d707899
Merge remote-tracking branch 'origin/gi-flow' into gi-flow
vradulescu-bd Nov 19, 2024
d1f4ad2
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 19, 2024
e1f6235
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 19, 2024
585278a
Merge branch 'main' into gi-flow
vradulescu-bd Nov 20, 2024
6ab9c98
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 20, 2024
248f352
Merge remote-tracking branch 'origin/gi-flow' into gi-flow
vradulescu-bd Nov 20, 2024
8fba7bf
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 20, 2024
f6056bf
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 20, 2024
7e443d2
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 20, 2024
42de58b
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 20, 2024
6c42d4a
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 20, 2024
29b5f3e
Acquire Lock - switched from main to aquire-lock branch
vradulescu-bd Nov 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 138 additions & 17 deletions .github/workflows/accept-baseline.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,157 @@
name: Accept Ghost Inspector Snapshots New Baseline on Merge
name: Accept Baselines Workflow

on:
workflow_run:
workflows: ["Accept New Baseline"]
pull_request:
types:
- completed
- labeled

permissions:
contents: write
statuses: write

jobs:
accept-baseline:
check-conditions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Download Baseline Acceptance Artifact
uses: actions/download-artifact@v3
- name: Validate Conditions
run: |
if [[ "${{ github.event.pull_request.merged }}" == "true" ]]; then
echo "PR is already merged. Exiting."
exit 1
fi

if [[ ! "${{ github.event.label.name }}" == "accept-baselines" ]]; then
echo "Label 'accept-baselines' not found. Exiting."
exit 1
fi
- name: Checkout Repository
uses: actions/checkout@v3
- name: Verify Required Checks
run: |
CHECKS=$(gh pr checks ${{ github.event.pull_request.number }} --json name,state --jq '.[] | select(.state != "SUCCESS" and .name != "snapshots-tests" and .name != "check-conditions" and .name != "auto-merge" and .name != "update-baselines" and .name != "acquire-lock" and .name != "aem-psi-check") | .name')
if [ -n "$CHECKS" ]; then
echo "Some required checks failed: $CHECKS"
exit 1
else
echo "All required checks passed."
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Verify Approvals
run: |
APPROVALS=$(gh pr view ${{ github.event.pull_request.number }} --json reviews --jq '[.reviews[] | select(.state=="APPROVED")] | length')
if [[ "$APPROVALS" -lt 2 ]]; then
echo "Not enough approvals. Exiting."
exit 1
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

acquire-lock:
needs: check-conditions
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
name: baseline-acceptance
- name: Validate Baseline Acceptance
ref: aquire-lock

- name: Check for Lock
id: check-lock
run: |
if [ ! -f accept_baseline_flag ]; then
echo "Baseline acceptance flag not found."
LOCK_FILE_URL="https://github.com/bitdefender/www-landing-pages/blob/aquire-lock/.baseline-lock"
STATUS_CODE=$(curl -o /dev/null -s -w "%{http_code}" $LOCK_FILE_URL)
if [ "$STATUS_CODE" == "200" ]; then
echo "Baseline update is already in progress. Exiting."
exit 1
fi
- name: Update Baseline in Ghost Inspector
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Acquire Lock
run: |
echo "Updating baselines for PR #${{ github.event.pull_request.number }}" > .baseline-lock

git config user.name "github-actions"
git config user.email "[email protected]"

git pull origin aquire-lock
git add .baseline-lock
git commit -m "Lock for baseline update by PR #${{ github.event.pull_request.number }}"
git push origin aquire-lock
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

update-baselines:
needs: acquire-lock
runs-on: ubuntu-latest
steps:
- name: Checkout `aquire-lock` Branch
uses: actions/checkout@v3
with:
ref: aquire-lock

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '20.4'
- run: npm install

- name: Create Lock File
run: |
echo "Updating baselines for PR #${{ github.event.pull_request.number }}" > .baseline-lock

git config user.name "github-actions"
git config user.email "[email protected]"

git pull origin aquire-lock

# Update the lock file with unique content
echo "$(date +%s): Updating baselines for PR #${{ github.event.pull_request.number }}" > .baseline-lock

git add .baseline-lock

# Commit only if there are changes
if git diff --cached --quiet; then
echo "No changes to commit. Skipping commit."
else
git commit -m "Lock for baseline update by PR #${{ github.event.pull_request.number }} [skip husky]"
git push origin aquire-lock
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Update Baselines in Ghost Inspector
run: npm run test:accept-baseline
env:
GI_KEY: ${{ secrets.GI_KEY }}
ACCEPT_NEW_BASELINE: 'true'
- name: Mark PR as Safe to Merge
run: echo "The baseline has been updated successfully. The PR is safe to merge."

auto-merge:
needs: update-baselines
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
ref: aquire-lock

- name: Release Lock
if: success()
run: |
git config user.name "github-actions"
git config user.email "[email protected]"

git pull origin aquire-lock --rebase

git rm .baseline-lock
git commit -m "Release lock for baseline update [skip husky]"
git push origin aquire-lock
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Auto-Merge PR
if: success()
run: gh pr merge ${{ github.event.pull_request.number }} --squash --admin
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

26 changes: 13 additions & 13 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
name: Lint, unit tests, and snapshots
name: Run Tests and Snapshots

on:
pull_request:
types: [opened, synchronize, reopened]
workflow_run:
workflows: ["Accept Baselines Workflow"]
types:
- completed

jobs:
setup-nodejs:
Expand All @@ -18,6 +22,7 @@ jobs:
lint:
needs: setup-nodejs
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' }}
steps:
- uses: actions/checkout@v3
- run: npm install
Expand All @@ -26,13 +31,14 @@ jobs:
unit-tests:
needs: setup-nodejs
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' }}
steps:
- uses: actions/checkout@v3
- run: npm install
- run: npm run test:unit

snapshots-tests:
needs: setup-nodejs
needs: [lint, unit-tests]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -41,15 +47,9 @@ jobs:
run: npm run test:snapshots
env:
GI_KEY: ${{ secrets.GI_KEY }}
- name: Inform Developer About Failing Snapshots
if: failure() # This runs if snapshot tests fail
run: echo "Snapshots failed. Check your code or accept new baselines in the GitHub Actions tab."
- name: Save Accept Baseline Option
if: failure()
run: echo "baseline_failure=true" > baseline_failure_flag
- name: Upload Baseline Acceptance Artifact
- name: Inform Developer About Snapshot Failures
if: failure()
uses: actions/upload-artifact@v3
with:
name: baseline-acceptance
path: baseline_failure_flag
run: echo "Snapshots failed. Add the 'accept-baselines' label to update the baselines or fix your code."
- name: Set PR Ready for Merge
if: success()
run: echo "PR is ready for merge."
27 changes: 0 additions & 27 deletions .github/workflows/start-accept-baselines.yml

This file was deleted.

8 changes: 8 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

if git log -1 --pretty=%B | grep -q "\[skip husky\]"; then
echo "Skipping Husky hooks for this commit."
exit 0
fi

npm run lint && npm run test:unit
71 changes: 37 additions & 34 deletions ghosti/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
logSuccess,
logError
} = require('./constants');
const { fetchWithRetry } = require('./utils');
const GhostInspector = require('ghost-inspector')(process.env.GI_KEY);

const hlxEnv = {
Expand Down Expand Up @@ -75,53 +76,53 @@ const EXCLUDED_SNAPSHOT_BLOCKS = [
}
}

try {
const blockSnapshotsToTest = fs.readdirSync(LOCAL_BLOCKS_PATH).filter(blockName => !EXCLUDED_SNAPSHOT_BLOCKS.includes(blockName));
// get snapshots tests
const snapshotSuiteTests = await GhostInspector.getSuiteTests(SNAPSHOTS_SUITE_ID);

const snapshotsPromises = blockSnapshotsToTest
.map((testName) => {
const testAlreadyExists = snapshotSuiteTests.find((originalTest) => originalTest.name === testName);

if (testAlreadyExists) {
return fetch(`https://api.ghostinspector.com/v1/tests/${testAlreadyExists._id}/execute/?apiKey=${process.env.GI_KEY}&startUrl=${featureBranchEnvironmentBaseUrl}/${PATH_TO_BLOCKS}/${testAlreadyExists.name}`).then((res) => res.json());
}

return GhostInspector.importTest(SNAPSHOTS_SUITE_ID, new SnapshotBlockTest({
name: testName,
startUrl: `${featureBranchEnvironmentBaseUrl}/${PATH_TO_BLOCKS}/${testName}`,
}).generate())
.then(({ _id }) => fetch(`https://api.ghostinspector.com/v1/tests/${_id}/execute/?apiKey=${process.env.GI_KEY}`).then((res) => res.json()));
});

const [
snapshotsResult,
] = await Promise.all([
Promise.all(snapshotsPromises),
]);

showSnapshotTestsFullLogs(snapshotsResult);
} catch (err) {
console.error(err);
process.exit(1);
}
// try {
// const blockSnapshotsToTest = fs.readdirSync(LOCAL_BLOCKS_PATH).filter(blockName => !EXCLUDED_SNAPSHOT_BLOCKS.includes(blockName));
// // get snapshots tests
// const snapshotSuiteTests = await GhostInspector.getSuiteTests(SNAPSHOTS_SUITE_ID);
//
// const snapshotsPromises = blockSnapshotsToTest
// .map((testName) => {
// const testAlreadyExists = snapshotSuiteTests.find((originalTest) => originalTest.name === testName);
//
// if (testAlreadyExists) {
// return fetch(`https://api.ghostinspector.com/v1/tests/${testAlreadyExists._id}/execute/?apiKey=${process.env.GI_KEY}&startUrl=${featureBranchEnvironmentBaseUrl}/${PATH_TO_BLOCKS}/${testAlreadyExists.name}`).then((res) => res.json());
// }
//
// return GhostInspector.importTest(SNAPSHOTS_SUITE_ID, new SnapshotBlockTest({
// name: testName,
// startUrl: `${featureBranchEnvironmentBaseUrl}/${PATH_TO_BLOCKS}/${testName}`,
// }).generate())
// .then(({ _id }) => fetch(`https://api.ghostinspector.com/v1/tests/${_id}/execute/?apiKey=${process.env.GI_KEY}`).then((res) => res.json()));
// });
//
// const [
// snapshotsResult,
// ] = await Promise.all([
// Promise.all(snapshotsPromises),
// ]);
//
// showSnapshotTestsFullLogs(snapshotsResult);
// } catch (err) {
// console.error(err);
// process.exit(1);
// }

try {
const blockSnapshotsToTest = fs.readdirSync(LOCAL_BLOCKS_PATH).filter(blockName => !EXCLUDED_SNAPSHOT_BLOCKS.includes(blockName));

// get snapshots tests
const snapshotSuiteTests = await GhostInspector.getSuiteTests(SNAPSHOTS_SUITE_ID);

const batches = createBatches(blockSnapshotsToTest, 3);
const batches = createBatches(blockSnapshotsToTest, 5);

let allTestResults = [];

for (const batch of batches) {
const snapshotsPromises = batch.map((testName) => {
const testAlreadyExists = snapshotSuiteTests.find((originalTest) => originalTest.name === testName);
if (testAlreadyExists) {
return fetch(`https://api.ghostinspector.com/v1/tests/${testAlreadyExists._id}/execute/?apiKey=${process.env.GI_KEY}&startUrl=${featureBranchEnvironmentBaseUrl}/${PATH_TO_BLOCKS}/${testAlreadyExists.name}`, {
return fetchWithRetry(`https://api.ghostinspector.com/v1/tests/${testAlreadyExists._id}/execute/?apiKey=${process.env.GI_KEY}&startUrl=${featureBranchEnvironmentBaseUrl}/${PATH_TO_BLOCKS}/${testAlreadyExists.name}`, {
signal: AbortSignal.timeout(FETCH_TIMEOUT)
}).then((res) => res.json());
}
Expand All @@ -130,7 +131,7 @@ const EXCLUDED_SNAPSHOT_BLOCKS = [
name: testName,
startUrl: `${featureBranchEnvironmentBaseUrl}/${PATH_TO_BLOCKS}/${testName}`,
}).generate())
.then(({ _id }) => fetch(`https://api.ghostinspector.com/v1/tests/${_id}/execute/?apiKey=${process.env.GI_KEY}`).then((res) => res.json()));
.then(({ _id }) => fetchWithRetry(`https://api.ghostinspector.com/v1/tests/${_id}/execute/?apiKey=${process.env.GI_KEY}`).then((res) => res.json()));
});

// Await the completion of all promises in the current batch before proceeding to the next
Expand All @@ -144,4 +145,6 @@ const EXCLUDED_SNAPSHOT_BLOCKS = [
console.error(err);
process.exit(1);
}

// await new Promise((res) => setTimeout(res, 1000))
})();
15 changes: 15 additions & 0 deletions ghosti/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
async function fetchWithRetry(url, options = {}, retries = 3) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
return await fetch(url, options);
} catch (error) {
if (attempt === retries) throw error;
console.warn(`Retrying fetch... (${attempt}/${retries})`);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
}

module.exports = {
fetchWithRetry
}
Loading