Skip to content

Commit

Permalink
Merge pull request #118 from mheap/number-deprecated
Browse files Browse the repository at this point in the history
Fix deprecated issue/pull number context
  • Loading branch information
JasonEtco authored Jun 8, 2020
2 parents 5146484 + 65356cc commit e5470fa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,11 @@ The name of the workflow that was triggered.

#### tools.context.issue

The `owner`, `repo`, and `number` params for making API requests against an issue or pull request.
The `owner`, `repo`, and `issue_number` params for making API requests against an issue or pull request.

#### tools.context.pullRequest

The `owner`, `repo`, and `pull_number` params for making API requests against a pull request.

#### tools.context.repo

Expand Down
37 changes: 34 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,41 @@ export class Context {
public get issue () {
const payload = this.payload

return {
...this.repo,
number: (payload.issue || payload.pull_request || payload).number
const data: { [k: string]: any } = {
...this.repo
}

if (payload.issue) {
// If it's an issue
data.issue_number = payload.issue.number
} else if (payload.pull_request) {
// If it's a PR
data.issue_number = payload.pull_request.number
} else if (payload.number) {
// Just sittin' there on the payload
data.issue_number = payload.number
} else {
throw new Error('tools.context.issue cannot be used with this event, there is no issue or pull_request object.')
}

return data
}

public get pullRequest () {
const payload = this.payload

const data: { [k: string]: any } = {
...this.repo
}

if (payload.pull_request) {
// If it's a PR, the API expects pull_number
data.pull_number = payload.pull_request.number
} else {
throw new Error('tools.context.pullRequest cannot be used with this event, there is no pull_request object.')
}

return data
}

public get repo () {
Expand Down
37 changes: 34 additions & 3 deletions tests/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Context', () => {

describe('#issue', () => {
it('returns attributes from the repository payload', () => {
expect(context.issue).toEqual({ owner: 'JasonEtco', repo: 'test', number: 1 })
expect(context.issue).toEqual({ owner: 'JasonEtco', repo: 'test', issue_number: 1 })
})

it('works with pull_request payloads', () => {
Expand All @@ -62,15 +62,46 @@ describe('Context', () => {
repository: { owner: { login: 'JasonEtco' }, name: 'test' }
}
expect(context.issue).toEqual({
number: 2, owner: 'JasonEtco', repo: 'test'
issue_number: 2, owner: 'JasonEtco', repo: 'test'
})
})

it('works with payload.number payloads', () => {
context.payload = { number: 2, repository: { owner: { login: 'JasonEtco' }, name: 'test' } }
expect(context.issue).toEqual({
number: 2, owner: 'JasonEtco', repo: 'test'
issue_number: 2, owner: 'JasonEtco', repo: 'test'
})
})

it('throws if no number was found', () => {
context.payload = {
repository: { owner: { login: 'JasonEtco' }, name: 'test' }
}

expect(() => context.issue).toThrow()
})
})

describe('#pullRequest', () => {
it('returns attributes from the repository payload', () => {
context.payload = {
pull_request: { number: 2 },
repository: { owner: { login: 'JasonEtco' }, name: 'test' }
}

expect(context.pullRequest).toEqual({
owner: 'JasonEtco',
repo: 'test',
pull_number: 2
})
})

it('throws if no pull_request object was found', () => {
context.payload = {
repository: { owner: { login: 'JasonEtco' }, name: 'test' }
}

expect(() => context.pullRequest).toThrow()
})
})
})

0 comments on commit e5470fa

Please sign in to comment.