-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: scanning pr for review - github action
- Loading branch information
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |