Skip to content
Open
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
70 changes: 45 additions & 25 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,66 +89,82 @@ describe('main.ts', () => {
)
})

it('Skips adding a message if this is not the first contribution', async () => {
it('Skips adding a message if this is not the first PR', async () => {
mocktokit.paginate
// Issues
// PRs
.mockResolvedValueOnce([
{
number: 10
number: 10,
user: { login: 'mona' }
},
{
number: 5
number: 3,
user: { login: 'mona' }
}
])
// PRs

await main.run()

expect(core.info).toHaveBeenCalledWith(
'Skipping...Not First Pull Request'
)
expect(mocktokit.rest.issues.createComment).not.toHaveBeenCalled()
})

it('Skips adding a message if this is not the first issue', async () => {
github.context.payload.issue = {
number: 10
}
github.context.payload.pull_request = undefined as any

mocktokit.paginate
// Issues
.mockResolvedValueOnce([
{
number: 3
number: 10
},
{
number: 5
}
])

await main.run()

expect(core.info).toHaveBeenCalledWith(
'Skipping...Not First Contribution'
)
expect(core.info).toHaveBeenCalledWith('Skipping...Not First Issue')
expect(mocktokit.rest.issues.createComment).not.toHaveBeenCalled()
})

it('Adds an issue message if this is the first contribution', async () => {
it('Adds an issue message if this is the first issue', async () => {
github.context.payload.issue = {
number: 10
}
github.context.payload.pull_request = undefined as any

mocktokit.paginate
// Issues
// Issues - only the current issue exists
.mockResolvedValueOnce([
{
number: 10
}
])
// PRs
.mockResolvedValueOnce([])

await main.run()

expect(mocktokit.rest.issues.createComment).toHaveBeenCalled()
})

it('Adds a PR message if this is the first contribution', async () => {
it('Adds a PR message if this is the first PR', async () => {
github.context.payload.issue = undefined as any
github.context.payload.pull_request = {
number: 10
}

mocktokit.paginate
// Issues
.mockResolvedValueOnce([])
// PRs
// PRs - only the current PR exists
.mockResolvedValueOnce([
{
number: 10
number: 10,
user: { login: 'mona' }
}
])

Expand Down Expand Up @@ -243,7 +259,8 @@ describe('main.ts', () => {
it('Returns true if only the current PR is present', async () => {
mocktokit.paginate.mockResolvedValueOnce([
{
number: 10
number: 10,
user: { login: 'mona' }
}
])

Expand All @@ -255,10 +272,12 @@ describe('main.ts', () => {
it('Returns false if older PRs are present', async () => {
mocktokit.paginate.mockResolvedValueOnce([
{
number: 10
number: 10,
user: { login: 'mona' }
},
{
number: 5
number: 5,
user: { login: 'mona' }
}
])

Expand All @@ -267,20 +286,21 @@ describe('main.ts', () => {
expect(result).toBe(false)
})

it('Does not ignore pull requests', async () => {
it('Ignores pull requests from other users', async () => {
mocktokit.paginate.mockResolvedValueOnce([
{
number: 10
number: 10,
user: { login: 'mona' }
},
{
number: 5,
pull_request: {}
user: { login: 'other-user' }
}
])

const result = await main.isFirstPullRequest(mocktokit)

expect(result).toBe(false)
expect(result).toBe(true)
})

it('Returns false if there is an error', async () => {
Expand Down
18 changes: 11 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

19 changes: 13 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ export async function run() {
auth: core.getInput('repo_token', { required: true })
})

// Check if this is the user's first contribution.
if (!(await isFirstIssue(octokit)) && !(await isFirstPullRequest(octokit)))
return core.info('Skipping...Not First Contribution')
// Check if this is the user's first contribution of the relevant type.
if (isIssue && !(await isFirstIssue(octokit)))
return core.info('Skipping...Not First Issue')
if (isPullRequest && !(await isFirstPullRequest(octokit)))
return core.info('Skipping...Not First Pull Request')

core.info(`Adding Message to #${github.context.issue.number}`)

Expand Down Expand Up @@ -95,9 +97,14 @@ export async function isFirstPullRequest(octokit: Octokit): Promise<boolean> {
})

return (
// Filter out any PRs that are newer than the current one.
pulls.filter((pull) => pull.number < github.context.issue.number)
.length === 0
pulls
// Filter to only the current user's PRs.
.filter(
(pull) => pull.user?.login === github.context.payload.sender!.login
)
// Filter out any PRs that are newer than the current one.
.filter((pull) => pull.number < github.context.issue.number).length ===
0
)
} catch (error) {
core.setFailed((error as any).message)
Expand Down