Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add options 'one_of' and 'none_of' for filters and validators #758

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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