GitHub webhooks toolset for Node.js
GitHub webhooks can be registered in multiple ways
- In repository or organization settings on github.com.
- Using the REST API for repositories or organizations
- By installing a GitHub App.
@octokit/webhooks
helps to handle webhook events sent by GitHub to your server. Note that while setting a secret is optional on GitHub, it is required to be set in order to use @octokit/webhooks
.
// install with: npm install @octokit/webhooks
const WebhooksApi = require('@octokit/webhooks')
const webhooks = new WebhooksApi({
secret: 'mysecret'
})
webhooks.on('*', ({id, name, payload}) => {
console.log(name, 'event received')
})
require('http').createServer(webhooks.middleware).listen(3000)
// can now receive webhook events at port 3000
- Constructor
- webhooks.sign()
- webhooks.verify()
- webhooks.handle()
- webhooks.on()
- webhooks.removeListener()
- webhooks.middleware()
- List of all webhook names
new WebhooksApi({secret[, path]})
secret
(String)
|
Required. Secret as configured in GitHub Settings. |
path
(String)
|
Only relevant for middleware .
Custom path to match requests against. Defaults to / .
|
Returns the webhooks
API.
weebhooks.sign(eventData)
data
(Object)
|
Required. Webhook request body as received from GitHub |
Returns a signature
string. Throws error if data
is not passed.
Can also be used standalone.
weebhooks.verify(eventData, signature)
data
(Object)
|
Required. Webhook event request body as received from GitHub. |
signature
(String)
|
Required.
Signature string as calculated by webhooks.sign() .
|
Returns true
or false
. Throws error if data
or signature
not passed.
Can also be used standalone.
webhooks.handle({name, data, signature})
name
String
|
Required.
Name of the event. (Event names are set as X-GitHub-Event header
in the webhook event request.)
|
data
Object
|
Required. Webhook event request body as received from GitHub. |
signature
String
|
Required.
Passed as X-Hub-Signature header
in the webhook request.
|
Returns a promise. Runs all event handlers set with webhooks.on()
in paralell and waits for them to finish. If one of the handlers rejects or throws an error, then webhooks.handle()
rejects. The returned error has an .errors
property which holds an array of all errors caught from the handlers. If no errors occur, webhooks.handle()
resolves without passing any value.
The .handle()
method belongs to the receiver module which can be used standalone if you don’t need the server middleware.
webhooks.on(eventName, handler)
webhooks.on(eventNames, handler)
eventName
String
|
Required. Name of the event. One of GitHub’s supported event names. |
eventNames
Array
|
Required. Array of event names. |
handler
Function
|
Required.
Method to be run each time the event with the passed name is received.
the handler function can be an async function, throw an error or
return a Promise.
|
The .on()
method belongs to the receiver module which can be used standalone if you don’t need the server middleware.
webhooks.removeHandler(eventName, handler)
webhooks.removeHandler(eventNames, handler)
eventName
String
|
Required. Name of the event. One of GitHub’s supported event names. |
eventNames
Array
|
Required. Array of event names. |
handler
Function
|
Required.
Method which was previously passed to webhooks.on() . If the same handler was registered multiple times for the same event, only the most recent handler gets removed.
|
The .removeHandler()
method belongs to the receiver module which can be used standalone if you don’t need the server middleware.
webhooks.middleware(request, response[, next])
request
Object
|
Required. A Node.js http.ClientRequest. |
response
Object
|
Required. A Node.js http.ServerResponse. |
next
Function
|
Optional function which invokes the next middleware, as used by Connect and Express. |
Returns a requestListener
(or middleware) method which can be directly passed to http.createServer()
, Express and other compatible Node.js server frameworks.
See the full list of event types with example payloads.
If there are actions for a webhook, events are emitted for both, the webhook name as well as a combination of the webhook name and the action, e.g. installation
and installation.created
. The *
event is always emitted.
Webhook | Actions |
---|---|
*
|
|
commit_comment
|
.created
|
create
|
|
delete
|
|
deployment
|
|
deployment_status
|
|
fork
|
|
gollum
|
|
installation
|
.created .deleted
|
installation_repositories
|
.added .removed
|
issue_comment
|
.created .edited .deleted
|
issues
|
.assigned .unassigned .labeled .unlabeled .opened .edited .milestoned .demilestoned .closed .reopened
|
label
|
.created .edited .deleted
|
marketplace_purchase
|
.purchased .cancelled .changed
|
member
|
.added .edited .deleted
|
membership
|
.added .removed
|
milestone
|
.created .closed .opened .edited .deleted
|
org_block
|
.blocked .unblocked
|
organization
|
.member_added .member_removed .member_invited
|
page_build
|
|
ping
|
|
project
|
.created .edited .converted .moved .deleted
|
project_card
|
.created .edited .closed .reopened .deleted
|
project_column
|
.created .edited .moved .deleted
|
public
|
|
pull_request
|
.assigned .unassigned .review_requested .review_request_removed .labeled .unlabeled .opened .edited .closed .reopened .synchronize
|
pull_request_review
|
.submitted .edited .dismissed
|
pull_request_review_comment
|
.created .edited .deleted
|
push
|
|
release
|
.published
|
repository
|
.created .deleted .archived .unarchived .publicized .privatized
|
status
|
|
team
|
.created .deleted .edited .added_to_repository .removed_from_repository
|
team_add
|
|
watch
|
.started
|