Skip to content

Commit

Permalink
chore: attempt to download old artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
markw-deriv committed May 16, 2024
1 parent b838277 commit 30490e0
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/actions/analyze/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ inputs:
runs:
using: composite
steps:

- name: Get latest commit hash from main
id: latest_commit
run: |
git fetch origin main
LATEST_COMMIT=$(git rev-parse origin/main)
echo "LATEST_COMMIT=$LATEST_COMMIT" >> $GITHUB_ENV
- name: Check if previous artifact exists
id: artifact_check
run: |
ARTIFACT_EXISTS=$(curl -s -H "Authorization: Bearer ${{ github.token }}" "https://api.github.com/repos/${{ github.repository }}/actions/artifacts?name=reports-${{ env.LATEST_COMMIT }}" | jq '.total_count' | awk '{print $1}')
if [[ $ARTIFACT_EXISTS -gt 0 ]]; then
echo "ARTIFACT_EXISTS=true" >> $GITHUB_ENV
else
echo "ARTIFACT_EXISTS=false" >> $GITHUB_ENV
fi
- name: Get artifact URL
id: get_artifact_url
run: |
ARTIFACT_URL=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/actions/artifacts?name=reports-${{ env.LATEST_COMMIT }}" | \
jq -r '.artifacts[0].archive_download_url')
echo "ARTIFACT_URL=$ARTIFACT_URL" >> $GITHUB_ENV
- name: Download artifact
if: env.ARTIFACT_URL != 'null'
run: |
curl -L -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"${{ env.ARTIFACT_URL }}" \
-o artifact.zip
unzip artifact.zip -d old
cd old
unzip reports.zip
- name: Analyze all packages
env:
NODE_ENV: ${{ inputs.NODE_ENV }}
Expand Down
122 changes: 122 additions & 0 deletions .github/actions/analyze/compareReports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const fs = require('fs');
const path = require('path');

function roundUpToDecimals(num, decimals) {
const factor = Math.pow(10, decimals);
return Math.ceil(num * factor) / factor;
}

function readJsonFile(filePath) {
if (fs.existsSync(filePath)) {
const data = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(data);
}
return null;
}

function calculateDiff(oldSize, newSize) {
return newSize - oldSize;
}

function calculatePercentage(oldSize, newSize) {
if (oldSize === 0) {
return newSize === 0 ? 0 : 100;
}
return ((newSize - oldSize) / oldSize) * 100;
}

function formatSize(size, roundUp) {
if (size === null) {
return '-';
}

const formattedSize = roundUp ? roundUpToDecimals(size / 1024, 2) + 'kb' : (size / 1024).toFixed(2) + 'kb';
return formattedSize;
}

const packagesDir = './packages';
const oldPackagesDir = './old/packages';
const packages = [...new Set([...fs.readdirSync(packagesDir), ...fs.readdirSync(oldPackagesDir)])];

let tableRows = '';

for (const pkg of packages) {
const oldReport = readJsonFile(path.join(oldPackagesDir, pkg, 'report.json'));
const newReport = readJsonFile(path.join(packagesDir, pkg, 'report.json'));

const oldSize = oldReport ? oldReport[0].gzipSize : null;
const newSize = newReport ? newReport[0].gzipSize : null;
let diff = oldSize && newSize ? calculateDiff(oldSize, newSize) : null;

if (oldSize === null) {
diff = newSize;
}

if (newSize === null) {
diff = oldSize;
}

let diffText = '-';

if (diff !== 0) {
diffText = diff < 0 ? '-' : '+' + formatSize(diff, true);
} else {
diffText = '+0kb';
}

let percentage = oldSize && newSize ? calculatePercentage(oldSize, newSize) : null;

if (oldSize === null) {
percentage = 100;
}

if (newSize === null) {
percentage = -100;
}

let percentageText = '-';
let percentageEmoji;

if (percentage === 0) {
percentageEmoji = '';
} else if (percentage < 0) {
percentageEmoji = '🟢'; // green for decrease
} else if (percentage >= 0 && percentage <= 5) {
percentageEmoji = '🟡'; // yellow for small increase
} else {
percentageEmoji = '🔴'; // red for larger increase
}

if (percentage !== 0) {
percentageText = percentage.toFixed(2) + '%';
} else {
percentageText = '0%';
}

tableRows += `
<tr>
<td>${pkg}</td>
<td>${formatSize(oldSize)}</td>
<td>${formatSize(newSize)}</td>
<td>${diffText}</td>
<td>${percentageText} ${percentageEmoji}</td>
</tr>
`.trim();
}

console.log(
`
<table>
<thead>
<th>package</th>
<th>old</th>
<th>new</th>
<th>diff</th>
<th>percentage</th>
</thead>
<tbody>
${tableRows}
</tbody>
</table>
`.trim()
);

0 comments on commit 30490e0

Please sign in to comment.