Skip to content

Commit

Permalink
feat: scanning pr for review - github action
Browse files Browse the repository at this point in the history
  • Loading branch information
yednapg committed Jul 19, 2024
1 parent 7d6fc6d commit 82f81c9
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
92 changes: 92 additions & 0 deletions .github/workflows/check_artwork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const fs = require('fs');
const path = require('path');

function fileExists(filePath) {
try {
return fs.statSync(filePath).isFile();
} catch (err) {
return false;
}
}

function readMetadata(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const metadataRegex = /@snapshot:\s*([^\s]+)/;
const match = content.match(metadataRegex);
return match ? match[1] : null;
} catch (err) {
return null;
}
}

function countAndVerifySnapshots(artworkDir) {
const indexPath = path.join(artworkDir, 'index.js');
const snapshotsDirPath = path.join(artworkDir, 'snapshots');

let comments = [];

if (!fileExists(indexPath)) {
comments.push(`::error file=${indexPath}::index.js is missing`);
return comments.join('\n');
}
comments.push('✅ Step 1: There is index.js');

const snapshotNameInMetadata = readMetadata(indexPath);
if (!snapshotNameInMetadata) {
comments.push(`::error file=${indexPath}::Snapshot name not found in metadata`);
return comments.join('\n');
}
comments.push('✅ Step 2: Metadata found in index.js');

const expectedSnapshotBaseName = snapshotNameInMetadata.toLowerCase(); // Ensure case insensitive
comments.push(`✅ Step 3: Expected snapshot base name: ${expectedSnapshotBaseName}`);

try {
if (!fs.existsSync(snapshotsDirPath)) {
throw new Error(`Snapshots directory does not exist: ${snapshotsDirPath}`);
}

const files = fs.readdirSync(snapshotsDirPath);
const pngSnapshotFiles = files.filter(file => {
const ext = path.extname(file).toLowerCase();
return ext === '.png';
}).sort();
const snapshotsCount = pngSnapshotFiles.length;

if (snapshotsCount < 3) {
comments.push(`::error file=${snapshotsDirPath}::Expected at least 3 PNG snapshots, but only ${snapshotsCount} found`);
} else {
comments.push(`✅ Step 4: Found ${snapshotsCount} PNG snapshots`);
}

let mismatchedNames = false;
pngSnapshotFiles.forEach((file, index) => {
const expectedFileName = `${expectedSnapshotBaseName}${index + 1}.png`;
if (file.toLowerCase() === expectedFileName.toLowerCase()) {
comments.push(`✅ PNG Snapshot '${file}' matches the expected name '${expectedFileName}'`);
} else {
comments.push(`::error file=${snapshotsDirPath}/${file}::PNG Snapshot '${file}' should be named '${expectedFileName}'`);
mismatchedNames = true;
}
});

} catch (err) {
comments.push(`::error file=${snapshotsDirPath}::Error reading PNG snapshots directory: ${err.message}`);
}

return comments.join('\n');
}

if (require.main === module) {
if (process.argv.length !== 3) {
console.error('Usage: node check_artwork.js <artwork_directory>');
process.exit(1);
}

const artworkDir = process.argv[2];
const result = countAndVerifySnapshots(artworkDir);
console.log(result);
}

module.exports = countAndVerifySnapshots;
31 changes: 31 additions & 0 deletions .github/workflows/check_artwork.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Check Artwork

on:
pull_request:
paths:
- 'art/**'

jobs:
check-artwork:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Fetch main branch
run: git fetch origin main

- name: Get new directories
id: new_dirs
run: |
NEW_DIRS=$(git diff --name-only --diff-filter=A origin/main | grep '^art/' | xargs -I{} dirname {} | sort -u | tr '\n' ' ')
echo "new_dirs=$NEW_DIRS" >> $GITHUB_ENV
echo "New directories: $new_dirs"
- name: Run artwork checks
run: |
for dir in $NEW_DIRS; do
node .github/workflows/check_artwork.js "$dir"
done
env:
NEW_DIRS: ${{ env.new_dirs }}

0 comments on commit 82f81c9

Please sign in to comment.