Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fwuensche committed Sep 27, 2024
1 parent d0fec6e commit 2676616
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
11 changes: 7 additions & 4 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# These owners will be the default owners for everything in
# the repo. Unless a later match takes precedence,
* @fwuensche
* @root

# Order is important; the last matching pattern takes the most
# precedence. When someone opens a pull request that only
# modifies JS files, only these and not the global
# owner(s) will be requested for a review.
*.js @rchoquet
*.js @javascript

# In this example, the user owns any files in the src directory
# at the root of the repository and any of its subdirectories.
/src/ @fwuensche @rchoquet
/src/ @source

# There's a plugin folder inside /src/ that we'll be using to test
# that the pattern matching is working correctly. See codeowners.test.ts
plugins/ @fwuensche
plugins/ @plugins

# This should override the plugins/ rule above and assign the user
/src/plugins/eslint.js @eslint-plugin
13 changes: 9 additions & 4 deletions src/codeowners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@ import Codeowners from './codeowners.js'
describe('getOwners', () => {
test('/src/ pattern matches files inside src folder', async () => {
const codeowners = new Codeowners()
expect(codeowners.getOwners('src/codeowners.test.ts')).toEqual(['@fwuensche', '@rchoquet'])
expect(codeowners.getOwners('src/codeowners.test.ts')).toEqual(['@source'])
})

test('plugins/ pattern should match any folder named plugins', async () => {
const codeowners = new Codeowners()
expect(codeowners.getOwners('src/plugins/eslint.js')).toEqual(['@fwuensche'])
expect(codeowners.getOwners('src/plugins/loc.js')).toEqual(['@plugins'])
})

test('defaults to the root owner', async () => {
const codeowners = new Codeowners()
expect(codeowners.getOwners('bin/commands/push.ts')).toEqual(['@fwuensche'])
expect(codeowners.getOwners('bin/commands/push.ts')).toEqual(['@root'])
})

test('*.js also matches files from subfolders', async () => {
const codeowners = new Codeowners()
expect(codeowners.getOwners('bin/commands/diff.js')).toEqual(['@rchoquet'])
expect(codeowners.getOwners('bin/commands/diff.js')).toEqual(['@javascript'])
})

test('non existing files return an empty list of owners', async () => {
const codeowners = new Codeowners()
expect(codeowners.getOwners('bin/non-existing-file')).toEqual([])
})

test('/src/plugins/eslint.ts pattern should override previous ones', async () => {
const codeowners = new Codeowners()
expect(codeowners.getOwners('src/plugins/eslint.js')).toEqual(['@eslint-plugin'])
})
})
6 changes: 5 additions & 1 deletion src/codeowners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,20 @@ class Codeowners {

/** Returns the files from the codebase mathing the given pattern */
#globPatterns(pattern: string) {
// Wilcard pattern
if (pattern.includes('*')) return glob.sync(pattern.replace('*', '**/*'), { nodir: true, ignore: IGNORES })

// Subdirectory pattern
if (pattern.endsWith('/')) {
if (pattern.startsWith('/')) {
return glob.sync(path.join(pattern.substring(1), '**', '*'), { nodir: true, ignore: IGNORES })
} else {
return glob.sync(path.join('**', pattern, '*'), { nodir: true, ignore: IGNORES })
}
}
return [pattern]

// Exact file pattern
return [pattern.replace(/^\//, '')]
}

getOwners(file: string) {
Expand Down

0 comments on commit 2676616

Please sign in to comment.