Skip to content

Commit

Permalink
feat: add support for output parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
linhe0x0 committed Mar 28, 2023
1 parent 01c7fd2 commit 6c49610
Show file tree
Hide file tree
Showing 9 changed files with 30,485 additions and 23,395 deletions.
77 changes: 63 additions & 14 deletions __tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ describe('handlePullRequest', () => {
'requestReviewers'
)

await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

expect(addAssigneesSpy).not.toBeCalled()
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers).toHaveLength(3)
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers![0]).toMatch(
/reviewer/
)
expect(output?.reviewers).toEqual(
requestReviewersSpy.mock.calls[0][0]?.reviewers
)
})

test('adds pr-creator as assignee if addAssignees is set to author', async () => {
Expand Down Expand Up @@ -259,14 +262,17 @@ describe('handlePullRequest', () => {
'requestReviewers'
)

await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

expect(addAssigneesSpy.mock.calls[0][0]?.assignees).toHaveLength(3)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![0]).toMatch(/reviewer/)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees).toEqual(
expect.arrayContaining(['reviewer1', 'reviewer2', 'reviewer3'])
)
expect(requestReviewersSpy).not.toBeCalled()
expect(output?.assignees).toEqual(
addAssigneesSpy.mock.calls[0][0]?.assignees
)
})

test('adds assignees to pull requests if the assigness are enabled explicitly', async () => {
Expand Down Expand Up @@ -339,14 +345,20 @@ describe('handlePullRequest', () => {
'requestReviewers'
)

await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

expect(addAssigneesSpy.mock.calls[0][0]?.assignees).toHaveLength(2)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![0]).toMatch(/assignee/)
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers).toHaveLength(2)
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers![0]).toMatch(
/reviewer/
)
expect(output?.assignees).toEqual(
addAssigneesSpy.mock.calls[0][0]?.assignees
)
expect(output?.reviewers).toEqual(
requestReviewersSpy.mock.calls[0][0]?.reviewers
)
})

test("doesn't add assignees if the reviewers contain only a pr creator and assignees are not explicit", async () => {
Expand Down Expand Up @@ -414,10 +426,11 @@ describe('handlePullRequest', () => {

const spy = jest.spyOn(client.rest.issues, 'addAssignees')

await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

expect(spy.mock.calls[0][0]?.assignees).toHaveLength(2)
expect(spy.mock.calls[0][0]?.assignees![0]).toMatch(/maintainer/)
expect(output?.assignees).toEqual(spy.mock.calls[0][0]?.assignees)
})

test('adds reviewers to pull requests if throws error to add assignees', async () => {
Expand Down Expand Up @@ -448,10 +461,11 @@ describe('handlePullRequest', () => {

const spy = jest.spyOn(client.rest.pulls, 'requestReviewers')

await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

expect(spy.mock.calls[0][0]?.reviewers).toHaveLength(2)
expect(spy.mock.calls[0][0]?.reviewers![0]).toMatch(/reviewer/)
expect(output?.reviewers).toEqual(spy.mock.calls[0][0]?.reviewers)
})

/*
Expand Down Expand Up @@ -541,14 +555,17 @@ describe('handlePullRequest', () => {
} as any

// WHEN
await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

// THEN
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers).toHaveLength(1)
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers![0]).toMatch(
/reviewer/
)
expect(addAssigneesSpy).not.toBeCalled()
expect(output?.reviewers).toEqual(
requestReviewersSpy.mock.calls[0][0]?.reviewers
)
})

test('adds reviewers to pull request from two different groups if review groups are enabled', async () => {
Expand Down Expand Up @@ -586,7 +603,7 @@ describe('handlePullRequest', () => {
} as any

// WHEN
await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

// THEN
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers).toHaveLength(2)
Expand All @@ -597,6 +614,9 @@ describe('handlePullRequest', () => {
/group2/
)
expect(addAssigneesSpy).not.toBeCalled()
expect(output?.reviewers).toEqual(
requestReviewersSpy.mock.calls[0][0]?.reviewers
)
})

test('adds all reviewers from a group that has less members than the number of reviews requested', async () => {
Expand Down Expand Up @@ -634,7 +654,7 @@ describe('handlePullRequest', () => {
} as any

// WHEN
await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

// THEN
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers).toHaveLength(3)
Expand All @@ -648,6 +668,9 @@ describe('handlePullRequest', () => {
/group2-user1/
)
expect(addAssigneesSpy).not.toBeCalled()
expect(output?.reviewers).toEqual(
requestReviewersSpy.mock.calls[0][0]?.reviewers
)
})

test('adds assignees to pull request from two different groups if groups are enabled and number of assignees is specified', async () => {
Expand Down Expand Up @@ -688,14 +711,17 @@ describe('handlePullRequest', () => {
} as any

// WHEN
await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

// THEN
expect(addAssigneesSpy.mock.calls[0][0]?.assignees).toHaveLength(3)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![0]).toMatch(/group1/)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![1]).toMatch(/group2/)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![2]).toMatch(/group3/)
expect(requestReviewersSpy).not.toBeCalled()
expect(output?.assignees).toEqual(
addAssigneesSpy.mock.calls[0][0]?.assignees
)
})

test('adds assignees to pull request from two different groups using numberOfReviewers if groups are enabled and number of assignees is not specified', async () => {
Expand Down Expand Up @@ -735,14 +761,17 @@ describe('handlePullRequest', () => {
} as any

// WHEN
await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

// THEN
expect(addAssigneesSpy.mock.calls[0][0]?.assignees).toHaveLength(3)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![0]).toMatch(/group1/)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![1]).toMatch(/group2/)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![2]).toMatch(/group3/)
expect(requestReviewersSpy).not.toBeCalled()
expect(output?.assignees).toEqual(
addAssigneesSpy.mock.calls[0][0]?.assignees
)
})

test('adds assignees to pull request from two different groups and reviewers are not specified', async () => {
Expand Down Expand Up @@ -782,14 +811,17 @@ describe('handlePullRequest', () => {
} as any

// WHEN
await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

// THEN
expect(addAssigneesSpy.mock.calls[0][0]?.assignees).toHaveLength(3)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![0]).toMatch(/group1/)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![1]).toMatch(/group2/)
expect(addAssigneesSpy.mock.calls[0][0]?.assignees![2]).toMatch(/group3/)
expect(requestReviewersSpy).not.toBeCalled()
expect(output?.assignees).toEqual(
addAssigneesSpy.mock.calls[0][0]?.assignees
)
})

test('adds normal reviewers and assignees from groups into the pull request', async () => {
Expand Down Expand Up @@ -830,7 +862,7 @@ describe('handlePullRequest', () => {
} as any

// WHEN
await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

// THEN
expect(addAssigneesSpy.mock.calls[0][0]?.assignees).toHaveLength(3)
Expand All @@ -845,6 +877,13 @@ describe('handlePullRequest', () => {
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers![1]).toMatch(
/reviewer/
)

expect(output?.assignees).toEqual(
addAssigneesSpy.mock.calls[0][0]?.assignees
)
expect(output?.reviewers).toEqual(
requestReviewersSpy.mock.calls[0][0]?.reviewers
)
})

test('adds normal assignees and reviewers from groups into the pull request', async () => {
Expand Down Expand Up @@ -885,7 +924,7 @@ describe('handlePullRequest', () => {
} as any

// WHEN
await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

// THEN
expect(addAssigneesSpy.mock.calls[0][0]?.assignees).toHaveLength(1)
Expand All @@ -901,6 +940,13 @@ describe('handlePullRequest', () => {
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers![3]).toMatch(
/group3-reviewer/
)

expect(output?.assignees).toEqual(
addAssigneesSpy.mock.calls[0][0]?.assignees
)
expect(output?.reviewers).toEqual(
requestReviewersSpy.mock.calls[0][0]?.reviewers
)
})

test('skips pull requests that do not have any of the filterLabels.include labels', async () => {
Expand Down Expand Up @@ -971,12 +1017,15 @@ describe('handlePullRequest', () => {
'requestReviewers'
)

await handler.handlePullRequest(client, context, config)
const output = await handler.handlePullRequest(client, context, config)

expect(addAssigneesSpy).not.toBeCalled()
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers).toHaveLength(3)
expect(requestReviewersSpy.mock.calls[0][0]?.reviewers![0]).toMatch(
/reviewer/
)
expect(output?.reviewers).toEqual(
requestReviewersSpy.mock.calls[0][0]?.reviewers
)
})
})
15 changes: 14 additions & 1 deletion __tests__/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,30 @@ describe.only('run', () => {
return ''
}
})
coreMocked.setOutput.mockImplementation(() => {})

mockedUtils.fetchConfigurationFile.mockImplementation(async () => ({
addAssignees: false,
addReviewers: true,
reviewers: ['reviewerA', 'reviewerB', 'reviewerC'],
}))
mockedUtils.toMentions.mockImplementation(() => 'mocked value')

mockedHandler.handlePullRequest.mockImplementation(async () => {})
mockedHandler.handlePullRequest.mockImplementation(async () => ({
reviewers: ['reviewerA'],
assignees: [],
}))

await run()

expect(mockedHandler.handlePullRequest).toBeCalled()
expect(coreMocked.setOutput.mock.calls[0]).toEqual([
'reviewers',
'mocked value',
])
expect(coreMocked.setOutput.mock.calls[1]).toEqual([
'assignees',
'mocked value',
])
})
})
13 changes: 13 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
chooseUsersFromGroups,
includesSkipKeywords,
fetchConfigurationFile,
toMentions,
} from '../src/utils'
import * as github from '@actions/github'

Expand Down Expand Up @@ -242,3 +243,15 @@ describe('fetchConfigurationFile', () => {
).rejects.toThrow(/the configuration file is not found/)
})
})

describe('toMentions', () => {
test('returns a mention string', () => {
const usernames = ['foo', 'bar']

expect(toMentions(usernames)).toEqual('@foo, @bar')
})

test('returns a empty string if the given list is empty', () => {
expect(toMentions([])).toEqual('')
})
})
Loading

0 comments on commit 6c49610

Please sign in to comment.