Skip to content

Commit

Permalink
feat: Add options 'one_of' and 'none_of' for filters and validators (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
andekande authored Jun 21, 2024
1 parent 970ef68 commit 62481ab
Show file tree
Hide file tree
Showing 21 changed files with 791 additions and 46 deletions.
3 changes: 2 additions & 1 deletion __fixtures__/unit/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ module.exports = {
updated_at: (options.updatedAt) ? options.updatedAt : new Date().toISOString(),
assignees: (options.assignees) ? options.assignees : [],
pull_request: {}
}
},
comment: options.issueComment
},
log: {
child: (s) => {
Expand Down
143 changes: 139 additions & 4 deletions __tests__/unit/filters/author.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ test('should fail with unexpected author', async () => {
expect(filter.status).toBe('fail')
})

test('should fail with unexpected author one_of author', async () => {
const author = new Author()
const settings = {
do: 'author',
one_of: [otherAuthorName]
}
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('fail')
})

test('should pass with expected author', async () => {
const author = new Author()
const settings = {
Expand All @@ -29,6 +39,16 @@ test('should pass with expected author', async () => {
expect(filter.status).toBe('pass')
})

test('should pass with one_of author', async () => {
const author = new Author()
const settings = {
do: 'author',
one_of: [authorName]
}
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('pass')
})

test('should fail with excluded author', async () => {
const author = new Author()
const settings = {
Expand All @@ -41,13 +61,21 @@ test('should fail with excluded author', async () => {
expect(filter.status).toBe('fail')
})

test('should pass with excluded author', async () => {
test('should fail with none_of author', async () => {
const author = new Author()
const settings = {
do: 'author',
must_exclude: {
regex: otherAuthorName
}
none_of: [authorName]
}
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('fail')
})

test('should pass with none_of author', async () => {
const author = new Author()
const settings = {
do: 'author',
none_of: [otherAuthorName]
}
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('pass')
Expand All @@ -67,6 +95,20 @@ test('should pass with expected author from correct team', async () => {
expect(filter.status).toBe('pass')
})

test('should pass with one_of author from correct team', async () => {
const author = new Author()
const settings = {
do: 'author',
must_include: {
regex: authorName
},
one_of: ['@org/team-slug']
}
Teams.extractTeamMembers = jest.fn().mockReturnValue([authorName])
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('pass')
})

test('should fail with expected author from incorrect team', async () => {
const author = new Author()
const settings = {
Expand All @@ -81,6 +123,20 @@ test('should fail with expected author from incorrect team', async () => {
expect(filter.status).toBe('fail')
})

test('should fail with one_of author from incorrect team', async () => {
const author = new Author()
const settings = {
do: 'author',
must_include: {
regex: authorName
},
one_of: ['@org/team-slug']
}
Teams.extractTeamMembers = jest.fn().mockReturnValue([])
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('fail')
})

test('should fail with unexpected author from correct team', async () => {
const author = new Author()
const settings = {
Expand All @@ -95,6 +151,20 @@ test('should fail with unexpected author from correct team', async () => {
expect(filter.status).toBe('fail')
})

test('should fail with one_of author from correct team', async () => {
const author = new Author()
const settings = {
do: 'author',
must_include: {
regex: otherAuthorName
},
one_of: ['@org/team-slug']
}
Teams.extractTeamMembers = jest.fn().mockReturnValue([authorName])
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('fail')
})

test('should pass when the author is a member of the team', async () => {
const author = new Author()
const settings = {
Expand All @@ -106,6 +176,17 @@ test('should pass when the author is a member of the team', async () => {
expect(filter.status).toBe('pass')
})

test('should pass when the author is one_of the members of the team', async () => {
const author = new Author()
const settings = {
do: 'author',
one_of: ['@org/team-slug']
}
Teams.extractTeamMembers = jest.fn().mockReturnValue([authorName])
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('pass')
})

test('should fail when the author is not a member of the team', async () => {
const author = new Author()
const authorName = 'mergeable'
Expand All @@ -118,6 +199,60 @@ test('should fail when the author is not a member of the team', async () => {
expect(filter.status).toBe('fail')
})

test('should fail when the author is not one_of the members of the team', async () => {
const author = new Author()
const authorName = 'mergeable'
const settings = {
do: 'author',
one_of: ['@org/team-slug']
}
Teams.extractTeamMembers = jest.fn().mockReturnValue([otherAuthorName])
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('fail')
})

test('should pass when the author is not member of the none_of team', async () => {
const author = new Author()
const settings = {
do: 'author',
none_of: ['@org/team-slug']
}
Teams.extractTeamMembers = jest.fn().mockReturnValue([otherAuthorName])
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('pass')
})

test('should fail when the author is member of the none_of team', async () => {
const author = new Author()
const settings = {
do: 'author',
none_of: ['@org/team-slug']
}
Teams.extractTeamMembers = jest.fn().mockReturnValue([authorName])
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('fail')
})

test('should pass when the author is one_of @author', async () => {
const author = new Author()
const settings = {
do: 'author',
one_of: ['@author']
}
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('pass')
})

test('should fail when the author is none_of @author', async () => {
const author = new Author()
const settings = {
do: 'author',
none_of: ['@author']
}
const filter = await author.processFilter(createMockContext(authorName), settings)
expect(filter.status).toBe('fail')
})

const createMockContext = (author) => {
return Helper.mockContext({ author })
}
Loading

0 comments on commit 62481ab

Please sign in to comment.