Skip to content

Commit

Permalink
feat: update logic to handle glob patterns if present
Browse files Browse the repository at this point in the history
  • Loading branch information
devindford committed Aug 6, 2024
1 parent 95353bf commit 9527cb8
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions .github/workflows/codeowner_review_status.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,39 @@ jobs:
const [pattern, ...owners] = line.split(/\s+/);
return { pattern, owners: owners.map(o => o.trim()) };
});
console.log('CODEOWNERS rules:', codeownersRules);
// Function to check if a file matches a pattern
const matchesPattern = (file, pattern) => {
const regexPattern = pattern
.replace(/\*/g, '.*')
.replace(/\?/g, '.')
.replace(/\//g, '\\/');
// Handle directory patterns
if (pattern.endsWith('/')) {
return file.startsWith(pattern);
}
// Handle glob patterns
let regexPattern = pattern
.replace(/\./g, '\\.')
.replace(/\*/g, '[^/]*')
.replace(/\?/g, '[^/]')
.replace(/\//g, '\\/')
.replace(/\*\*/g, '.*');
// For double-star patterns, match any depth
if (pattern.includes('**')) {
return new RegExp(`^${regexPattern}`).test(file);
}
// For patterns without wildcards, check if the file is in the directory or is the file itself
if (!pattern.includes('*') && !pattern.includes('?')) {
return file === pattern || file.startsWith(pattern + '/');
}
// For single-star patterns, don't match across directory boundaries
return new RegExp(`^${regexPattern}$`).test(file);
};
// Get relevant code owners for the changed files
const relevantOwners = new Set();
const defaultOwner = codeownersRules.find(rule => rule.pattern === '*')?.owners[0];
console.log(defaultOwner);
files.forEach(file => {
let fileOwners = new Set();
codeownersRules.forEach(rule => {
Expand All @@ -79,7 +97,6 @@ jobs:
}
fileOwners.forEach(owner => relevantOwners.add(owner));
});
console.log('Relevant code owners:', relevantOwners);
if (relevantOwners.size === 0) {
console.log('No relevant code owners found for the changed files. Skipping check.');
Expand Down

0 comments on commit 9527cb8

Please sign in to comment.