diff --git a/README.md b/README.md index 197fc74..430a52d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/context.ts b/src/context.ts index de2a88a..d8756c7 100644 --- a/src/context.ts +++ b/src/context.ts @@ -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 () { diff --git a/tests/context.test.ts b/tests/context.test.ts index 0f477af..2a2b3d4 100644 --- a/tests/context.test.ts +++ b/tests/context.test.ts @@ -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', () => { @@ -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() + }) }) })