Skip to content

Commit 999828b

Browse files
author
Hubot
committed
Sync changes from upstream repository
1 parent 4f37172 commit 999828b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

content/guides/best-practices-for-integrators.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,39 @@ For the stability of your app, you shouldn't try to parse this data or try to gu
5454

5555
For example, when working with paginated results, it's often tempting to construct URLs that append `?page=<number>` to the end. Avoid that temptation. [Our guide on pagination](/guides/traversing-with-pagination) offers some safe tips on dependably following paginated results.
5656

57+
## Check the action before processing the event
58+
Webhook events can have multiple actions. As GitHub's feature set grows, the list of actions may change. Ensure that your application explicitly checks the action before doing any processing. For example, the [`IssuesEvent`](https://developer.github.com/v3/activity/events/types/#issuesevent) has several possible actions, such as `opened` when the issue is created, `closed` when the issue is closed, and `assigned` when the issue is assigned to someone.
59+
60+
Two code examples are given below, Fig 1.1 and Fig 1.2. In Fig 1.1, the `process_closed` method will be called for any event action which is not `opened` or `assigned`. This means that the `process_closed` method will be called for events with the `closed` action, but also other events with different actions delivered to the webhook.
61+
62+
Instead, the suggested approach is to explicitly check event actions and act accordingly such as in Fig 1.2. In this example the `closed` action is checked first before calling the `process_closed` method.
63+
64+
**Fig 1.1: Not Recommended: catch-all else block**
65+
```ruby
66+
case action
67+
when "opened"
68+
process_opened
69+
when "assigned"
70+
process_assigned
71+
else
72+
process_closed
73+
end
74+
```
75+
76+
**Fig 1.2: Recommended: explicitly check each action**
77+
```ruby
78+
case action
79+
when "opened"
80+
process_opened
81+
when "assigned"
82+
process_assigned
83+
when "closed"
84+
process_closed
85+
end
86+
```
87+
88+
We may also add new webhook event types from time to time. If your webhook is configured to "Send me everything" then your code should also explicitly check for the event type in a similar way as we have done with checking for the action type above.
89+
5790
## Dealing with rate limits
5891

5992
The GitHub API [rate limit](/v3/#rate-limiting) ensures that the API is fast and available for everyone.

0 commit comments

Comments
 (0)