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

feat: scanning pr for review - github action #723

Merged
merged 10 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
93 changes: 93 additions & 0 deletions .github/workflows/check_artwork.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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 metadataBaseName = path.basename(snapshotNameInMetadata, path.extname(snapshotNameInMetadata));
const expectedSnapshotBaseName = metadataBaseName.toLowerCase();
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;
56 changes: 56 additions & 0 deletions .github/workflows/check_artwork.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Check Artwork

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

jobs:
check-artwork:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
repository-projects: write
issues: write

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 | grep -E '^art/[^/]+$')
echo "NEW_DIRS=$NEW_DIRS" >> $GITHUB_ENV
echo "New directories: $NEW_DIRS"

- name: Create logs directory
run: mkdir -p .github/workflows/logs

- name: Run artwork checks and save results
run: |
echo "" > .github/workflows/logs/artwork_check_results.txt
for dir in $NEW_DIRS; do
echo "Checking directory: $dir" >> .github/workflows/logs/artwork_check_results.txt
node .github/workflows/check_artwork.cjs "$dir" >> .github/workflows/logs/artwork_check_results.txt
done
echo "RESULTS_FILE=.github/workflows/logs/artwork_check_results.txt" >> $GITHUB_ENV
cat .github/workflows/logs/artwork_check_results.txt

- name: Comment on pull request
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const results = fs.readFileSync(process.env.RESULTS_FILE, 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: results
});