Skip to content
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
69 changes: 67 additions & 2 deletions __tests__/utils/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {getAssignees} from '../../src/utils/context';
import {getContext} from '../util';

describe('getAssignees', () => {
it('should get assignees', () => {
it('should get issue assignees', () => {
expect(getAssignees(getContext({
eventName: 'issues',
payload: {
sender: {
type: 'User',
Expand All @@ -16,8 +17,24 @@ describe('getAssignees', () => {
}))).toEqual(['test']);
});

it('should empty', () => {
it('should get PR assignees', () => {
expect(getAssignees(getContext({
eventName: 'pull_request',
payload: {
sender: {
type: 'User',
login: 'test',
},
pull_request: {
assignees: [],
},
},
}))).toEqual(['test']);
});

it('should empty 1', () => {
expect(getAssignees(getContext({
eventName: 'issues',
payload: {
sender: {
type: 'User',
Expand All @@ -33,8 +50,27 @@ describe('getAssignees', () => {
}))).toEqual([]);
});

it('should empty 2', () => {
expect(getAssignees(getContext({
eventName: 'pull_request',
payload: {
sender: {
type: 'User',
login: 'test',
},
pull_request: {
assignees: [
{login: 'test'},
{login: 'test2'},
],
},
},
}))).toEqual([]);
});

it('should return false 1', () => {
expect(getAssignees(getContext({
eventName: 'issues',
payload: {
issue: {
assignees: [],
Expand All @@ -45,6 +81,7 @@ describe('getAssignees', () => {

it('should return false 2', () => {
expect(getAssignees(getContext({
eventName: 'issues',
payload: {
sender: {
type: 'User',
Expand All @@ -54,4 +91,32 @@ describe('getAssignees', () => {
},
}))).toBeFalsy();
});

it('should return false 3', () => {
expect(getAssignees(getContext({
eventName: 'pull_request',
payload: {
sender: {
type: 'User',
login: 'test',
},
pull_request: {},
},
}))).toBeFalsy();
});

it('should return false 4', () => {
expect(getAssignees(getContext({
eventName: 'push',
payload: {
sender: {
type: 'User',
login: 'test',
},
issue: {
assignees: [],
},
},
}))).toBeFalsy();
});
});
10 changes: 9 additions & 1 deletion src/utils/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ export const getAssignees = (context: Context): string[] | false => {

const getSender = (context: Context): string | false => context.payload.sender && context.payload.sender.type === 'User' ? context.payload.sender.login : false;

const getCurrentAssignees = (context: Context): string[] | false => context.payload.issue && 'assignees' in context.payload.issue ? context.payload.issue.assignees.map(assignee => assignee.login) : false;
const getCurrentAssignees = (context: Context): string[] | false => {
if ('issues' === context.eventName) {
return context.payload.issue && 'assignees' in context.payload.issue ? context.payload.issue.assignees.map(assignee => assignee.login) : false;
}
if ('pull_request' === context.eventName) {
return context.payload.pull_request && 'assignees' in context.payload.pull_request ? context.payload.pull_request.assignees.map(assignee => assignee.login) : false;
}
return false;
};