Skip to content

Commit 9a365e3

Browse files
committed
Enhance file validation logic to support string and object formats, and improve subdirectory checks for app metadata
1 parent 4491329 commit 9a365e3

1 file changed

Lines changed: 35 additions & 47 deletions

File tree

scripts/validate-pull-request.js

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -189,28 +189,50 @@ async function validateMetadata(filePath, dir) {
189189
// Check each file exists in the repository at the specified commit
190190
if (metadata.owner && metadata.repo && metadata.commit) {
191191
for (const file of metadata.files) {
192-
if (typeof file !== 'string') {
193-
console.log(` - ❌ File entry must be a string: \`${file}\``);
192+
let filePath;
193+
let displayPath;
194+
195+
if (typeof file === 'string') {
196+
// String format: direct file path
197+
const cleanFilePath = file.startsWith('/') ? file.substring(1) : file;
198+
filePath = path.posix.join(metadata.path === '/' ? '' : metadata.path, cleanFilePath);
199+
displayPath = file;
200+
} else if (typeof file === 'object' && file !== null) {
201+
// Object format: must have 'source' and 'destination' properties
202+
if (!file.source || !file.destination) {
203+
console.log(` - ❌ File object must contain 'source' and 'destination' properties: \`${JSON.stringify(file)}\``);
204+
hasErrors = true;
205+
continue;
206+
}
207+
if (typeof file.source !== 'string' || typeof file.destination !== 'string') {
208+
console.log(` - ❌ File object 'source' and 'destination' must be strings: \`${JSON.stringify(file)}\``);
209+
hasErrors = true;
210+
continue;
211+
}
212+
// Use source path for verification, combined with metadata.path
213+
const cleanSourcePath = file.source.startsWith('/') ? file.source.substring(1) : file.source;
214+
filePath = path.posix.join(metadata.path === '/' ? '' : metadata.path, cleanSourcePath);
215+
displayPath = `${file.source}${file.destination}`;
216+
} else {
217+
console.log(` - ❌ File entry must be a string or object with 'source' and 'destination' properties: \`${file}\``);
194218
hasErrors = true;
195219
continue;
196220
}
197221

198222
try {
199-
// Construct path - remove leading slash if present
200-
const filePath = file.startsWith('/') ? file.substring(1) : file;
201223
const githubUrl = `https://api.github.com/repos/${metadata.owner}/${metadata.repo}/contents/${filePath}?ref=${metadata.commit}`;
202224
const response = await fetch(githubUrl);
203225

204226
if (response.status === 200) {
205-
console.log(` - ✅ File exists at commit: \`${file}\``);
227+
console.log(` - ✅ File exists at commit: \`${displayPath}\``);
206228
} else if (response.status === 404) {
207-
console.log(` - ❌ File not found at commit \`${metadata.commit}...\`: \`${file}\``);
229+
console.log(` - ❌ File not found at commit \`${metadata.commit}...\`: \`${displayPath}\``);
208230
hasErrors = true;
209231
} else {
210-
console.log(` - ⚠️ Could not verify file \`${file}\` (status: ${response.status})`);
232+
console.log(` - ⚠️ Could not verify file \`${displayPath}\` (status: ${response.status})`);
211233
}
212234
} catch (error) {
213-
console.log(` - ⚠️ Could not verify file \`${file}\`: ${error.message}`);
235+
console.log(` - ⚠️ Could not verify file \`${displayPath}\`: ${error.message}`);
214236
}
215237
}
216238
} else {
@@ -640,8 +662,8 @@ async function main() {
640662
await validateDirectoryFiles(dir, directMetadataFile, directLogoPath);
641663
} else {
642664
// This directory doesn't contain metadata.json directly
643-
// Check if it contains subdirectories with metadata.json (repositories/owner/repo/app structure)
644-
console.log(` - 🔍 Checking for repository subdirectories...`);
665+
// Check if it contains subdirectories with metadata.json
666+
console.log(` - 🔍 Checking for app subdirectories...`);
645667

646668
const subdirs = fs.readdirSync(dir, { withFileTypes: true })
647669
.filter(dirent => dirent.isDirectory())
@@ -654,51 +676,17 @@ async function main() {
654676
const subdirLogoPath = path.join(subdirPath, 'logo.png');
655677

656678
if (fs.existsSync(subdirMetadataFile)) {
657-
// Direct metadata.json in repo directory
658679
foundApps = true;
659-
console.log(` - 📁 Repository: \`${subdir}\``);
680+
console.log(` - 📁 App subdirectory: \`${subdir}\``);
660681
await validateDirectoryFiles(subdirPath, subdirMetadataFile, subdirLogoPath);
661-
} else {
662-
// Check for app subdirectories within this repo directory
663-
console.log(` - 📁 Repository: \`${subdir}\``);
664-
console.log(` - 🔍 Checking for app subdirectories...`);
665-
666-
try {
667-
const appSubdirs = fs.readdirSync(subdirPath, { withFileTypes: true })
668-
.filter(dirent => dirent.isDirectory())
669-
.map(dirent => dirent.name);
670-
671-
let foundAppsInRepo = false;
672-
for (const appSubdir of appSubdirs) {
673-
const appSubdirPath = path.join(subdirPath, appSubdir);
674-
const appMetadataFile = path.join(appSubdirPath, 'metadata.json');
675-
const appLogoPath = path.join(appSubdirPath, 'logo.png');
676-
677-
if (fs.existsSync(appMetadataFile)) {
678-
foundApps = true;
679-
foundAppsInRepo = true;
680-
console.log(` - 📁 App subdirectory: \`${appSubdir}\``);
681-
await validateDirectoryFiles(appSubdirPath, appMetadataFile, appLogoPath);
682-
}
683-
}
684-
685-
if (!foundAppsInRepo) {
686-
console.log(` - 📄 \`metadata.json\``);
687-
console.log(` - ❌ File not found in any app subdirectories`);
688-
console.log(` - 📄 \`logo.png\``);
689-
console.log(` - ❌ File not found in any app subdirectories`);
690-
}
691-
} catch (error) {
692-
console.log(` - ❌ Error reading repository directory: ${error.message}`);
693-
}
694682
}
695683
}
696684

697685
if (!foundApps) {
698686
console.log(` - 📄 \`metadata.json\``);
699-
console.log(` - ❌ File not found in any subdirectories`);
687+
console.log(` - ❌ File not found`);
700688
console.log(` - 📄 \`logo.png\``);
701-
console.log(` - ❌ File not found in any subdirectories`);
689+
console.log(` - ❌ File not found`);
702690
}
703691
}
704692

0 commit comments

Comments
 (0)