Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 77c19fb

Browse files
authored
Merge pull request #61 from jclem/issue-repo-objects
Make `context.issue` and `context.repo` objects
2 parents c3a8dbe + 5e210e8 commit 77c19fb

File tree

3 files changed

+25
-79
lines changed

3 files changed

+25
-79
lines changed

README.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -316,23 +316,13 @@ The Git `sha` at which the action was triggered
316316

317317
The name of the workflow that was triggered.
318318

319-
#### tools.context.issue([object])
319+
#### tools.context.issue
320320

321-
Return the `owner`, `repo`, and `number` params for making API requests against an issue or pull request. The object passed in will be merged with the repo params.
321+
The `owner`, `repo`, and `number` params for making API requests against an issue or pull request.
322322

323-
```js
324-
const params = context.issue({body: 'Hello World!'})
325-
// Returns: {owner: 'username', repo: 'reponame', number: 123, body: 'Hello World!'}
326-
```
327-
328-
#### tools.context.repo([object])
323+
#### tools.context.repo
329324

330-
Return the `owner` and `repo` params for making API requests against a repository.
331-
332-
```js
333-
const params = context.repo({path: '.github/config.yml'})
334-
// Returns: {owner: 'username', repo: 'reponame', path: '.github/config.yml'}
335-
```
325+
The `owner` and `repo` params for making API requests against a repository.
336326

337327
## Actions using actions-toolkit
338328

src/context.ts

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,47 +70,26 @@ export class Context {
7070
this.actor = process.env.GITHUB_ACTOR as string
7171
}
7272

73-
/**
74-
* Return the `owner` and `repo` params for making API requests against a
75-
* repository.
76-
*
77-
* ```js
78-
* const params = context.repo({path: '.github/config.yml'})
79-
* // Returns: {owner: 'username', repo: 'reponame', path: '.github/config.yml'}
80-
* ```
81-
*
82-
* @param object - Params to be merged with the repo params.
83-
*
84-
*/
85-
public repo<T> (object?: T) {
73+
public get issue () {
74+
const payload = this.payload
75+
76+
return {
77+
...this.repo,
78+
number: (payload.issue || payload.pull_request || payload).number
79+
}
80+
}
81+
82+
public get repo () {
8683
const repo = this.payload.repository
8784

8885
if (!repo) {
89-
throw new Error('context.repo() is not supported for this webhook event.')
86+
throw new Error('context.repo is not supported for this webhook event.')
9087
}
9188

92-
return Object.assign({
89+
return {
9390
owner: repo.owner.login,
9491
repo: repo.name
95-
}, object)
92+
}
9693
}
9794

98-
/**
99-
* Return the `owner`, `repo`, and `number` params for making API requests
100-
* against an issue or pull request. The object passed in will be merged with
101-
* the repo params.
102-
*
103-
* ```js
104-
* const params = context.issue({body: 'Hello World!'})
105-
* // Returns: {owner: 'username', repo: 'reponame', number: 123, body: 'Hello World!'}
106-
* ```
107-
*
108-
* @param object - Params to be merged with the issue params.
109-
*/
110-
public issue<T> (object?: T) {
111-
const payload = this.payload
112-
return Object.assign({
113-
number: (payload.issue || payload.pull_request || payload).number
114-
}, this.repo(object))
115-
}
11695
}

tests/context.test.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,61 +27,38 @@ describe('Context', () => {
2727

2828
describe('#repo', () => {
2929
it('returns attributes from repository payload', () => {
30-
expect(context.repo()).toEqual({ owner: 'JasonEtco', repo: 'test' })
30+
expect(context.repo).toEqual({ owner: 'JasonEtco', repo: 'test' })
3131
})
3232

33-
it('merges attributes', () => {
34-
expect(context.repo({ foo: 1, bar: 2 })).toEqual({
35-
bar: 2, foo: 1, owner: 'JasonEtco', repo: 'test'
36-
})
37-
})
38-
39-
it('overrides repo attributes', () => {
40-
expect(context.repo({ owner: 'muahaha' })).toEqual({
41-
owner: 'muahaha', repo: 'test'
42-
})
43-
})
44-
45-
it('return error for context.repo() when repository doesn\'t exist', () => {
33+
it('return error for context.repo when repository doesn\'t exist', () => {
4634
context.payload = {}
4735
try {
48-
context.repo()
36+
// tslint:disable-next-line no-unused-expression
37+
context.repo
4938
} catch (e) {
50-
expect(e.message).toMatch('context.repo() is not supported')
39+
expect(e.message).toMatch('context.repo is not supported')
5140
}
5241
})
5342
})
5443

5544
describe('#issue', () => {
5645
it('returns attributes from the repository payload', () => {
57-
expect(context.issue()).toEqual({ owner: 'JasonEtco', repo: 'test', number: 1 })
58-
})
59-
60-
it('merges attributes', () => {
61-
expect(context.issue({ foo: 1, bar: 2 })).toEqual({
62-
bar: 2, foo: 1, number: 1, owner: 'JasonEtco', repo: 'test'
63-
})
64-
})
65-
66-
it('overrides repo attributes', () => {
67-
expect(context.issue({ owner: 'muahaha', number: 5 })).toEqual({
68-
number: 5, owner: 'muahaha', repo: 'test'
69-
})
46+
expect(context.issue).toEqual({ owner: 'JasonEtco', repo: 'test', number: 1 })
7047
})
7148

7249
it('works with pull_request payloads', () => {
7350
context.payload = {
7451
pull_request: { number: 2 },
7552
repository: { owner: { login: 'JasonEtco' }, name: 'test' }
7653
}
77-
expect(context.issue()).toEqual({
54+
expect(context.issue).toEqual({
7855
number: 2, owner: 'JasonEtco', repo: 'test'
7956
})
8057
})
8158

8259
it('works with payload.number payloads', () => {
8360
context.payload = { number: 2, repository: { owner: { login: 'JasonEtco' }, name: 'test' } }
84-
expect(context.issue()).toEqual({
61+
expect(context.issue).toEqual({
8562
number: 2, owner: 'JasonEtco', repo: 'test'
8663
})
8764
})

0 commit comments

Comments
 (0)