GitHub webhooks are triggered for various significant actions on GitHub, such as pushing code, opening or closing issues, merging pull requests, and commenting on discussions.
As a Probot app developer, you can listen for these events and automate responses to them. The app.on
method allows your app to subscribe to specific GitHub webhook events and execute logic accordingly.
To handle a webhook event, use app.on(eventName, callback)
. The context
object contains all relevant details about the event, including the payload sent by GitHub.
export default (app) => {
app.on("push", async (context) => {
// Code was pushed to the repository
app.log.info("Received push event", context.payload);
});
};
Many events include an action
property that specifies what happened. For example, the issues
event supports actions like opened
, closed
, and edited
. You can listen for a specific action by appending it to the event name:
export default (app) => {
app.on("issues.opened", async (context) => {
app.log.info("An issue was just opened", context.payload);
});
};
To handle multiple webhook events with the same logic, pass an array of event names:
export default (app) => {
app.on(["issues.opened", "issues.edited"], async (context) => {
app.log.info("An issue was opened or edited", context.payload);
});
};
To log all received webhook events, use app.onAny()
:
export default (app) => {
app.onAny(async (context) => {
app.log.info({ event: context.name, action: context.payload.action });
});
};
Found a mistake or want to help improve this documentation? Suggest changes on GitHub