Skip to main content

Triggers

The ballerinax/trigger.github package supports event-driven GitHub webhook processing through a listener. The listener receives webhook requests from GitHub and invokes your service callbacks when issues, pull requests, releases, pushes, labels, milestones, project cards, and related repository events occur.

Three components work together:

ComponentRole
github:ListenerExposes the webhook endpoint and dispatches incoming GitHub events to attached services.
github:ListenerConfigDefines listener configuration such as the webhook secret used to validate incoming requests.
github:IssuesServiceDefines issue event callbacks such as onOpened, onClosed, and onReopened.
github:PullRequestServiceDefines pull request event callbacks such as onOpened, onClosed, and onReviewRequested.
github:ReleaseServiceDefines release event callbacks such as onPublished, onCreated, and onDeleted.
github:PushServiceDefines the onPush callback invoked when commits are pushed to a repository.
github:IssuesEventThe issue event payload passed to issue callbacks.
github:PushEventThe push event payload passed to the onPush callback.

For action-based operations, see the Action Reference.


Listener

The github:Listener receives GitHub webhook HTTP requests and routes events to the relevant service type. Configure the GitHub repository webhook to point to the public URL of the running listener, use application/json as the content type, and use the same secret value configured as webhookSecret.

Configuration

The listener supports the following connection strategy:

Config TypeDescription
ListenerConfigConfiguration for the GitHub webhook listener.

ListenerConfig fields:

FieldTypeDefaultDescription
webhookSecretstringDEFAULT_SECRETSecret token used to validate GitHub webhook requests. Configure the same value in the GitHub repository webhook settings.

The listener also accepts a port or an HTTP listener object to expose the webhook endpoint. If you do not provide a port, the listener uses the default port 8090.

Initializing the listener

Listener with a webhook secret and configurable port:

import ballerinax/trigger.github;

configurable string webhookSecret = ?;
configurable int listenerPort = ?;

configurable github:ListenerConfig listenerConfig = {
webhookSecret: webhookSecret
};

listener github:Listener githubListener = new (listenerConfig, listenerPort);

Listener without a webhook secret:

Use this only when no secret is configured in the GitHub webhook settings.

import ballerinax/trigger.github;

listener github:Listener githubListener = new (listenOn = 8090);

Listener with the default port:

import ballerinax/trigger.github;

listener github:Listener githubListener = new ();

Service

A GitHub trigger service is a Ballerina service attached to a github:Listener. Select the service type that matches the GitHub webhook event channel you want to handle. The service implements callbacks that are invoked when matching GitHub webhook actions occur.

Callback signatures

Service TypeCallbackSignatureDescription
github:IssuesServiceonOpened, onClosed, onReopened, onAssigned, onUnassigned, onLabeled, onUnlabeledremote function <callback>(github:IssuesEvent payload) returns error?Invoked when a GitHub issue event is received.
github:IssueCommentServiceonCreated, onEdited, onDeletedremote function <callback>(github:IssueCommentEvent payload) returns error?Invoked when a comment is added to, edited on, or deleted from an issue or pull request.
github:PullRequestServiceonOpened, onClosed, onReopened, onAssigned, onUnassigned, onReviewRequested, onReviewRequestRemoved, onLabeled, onUnlabeled, onEditedremote function <callback>(github:PullRequestEvent payload) returns error?Invoked when a GitHub pull request event is received.
github:PullRequestReviewServiceonSubmitted, onEdited, onDismissedremote function <callback>(github:PullRequestReviewEvent payload) returns error?Invoked when a pull request review event is received.
github:PullRequestReviewCommentServiceonCreated, onEdited, onDeletedremote function <callback>(github:PullRequestReviewCommentEvent payload) returns error?Invoked when a pull request review comment event is received.
github:ReleaseServiceonPublished, onUnpublished, onCreated, onEdited, onDeleted, onPreReleased, onReleasedremote function <callback>(github:ReleaseEvent payload) returns error?Invoked when a GitHub release event is received.
github:LabelServiceonCreated, onEdited, onDeletedremote function <callback>(github:LabelEvent payload) returns error?Invoked when a repository label event is received.
github:MilestoneServiceonCreated, onEdited, onDeleted, onClosed, onOpenedremote function <callback>(github:MilestoneEvent payload) returns error?Invoked when a repository milestone event is received.
github:PushServiceonPushremote function onPush(github:PushEvent payload) returns error?Invoked when commits are pushed to a repository.
github:ProjectCardServiceonCreated, onEdited, onMoved, onConverted, onDeletedremote function <callback>(github:ProjectCardEvent payload) returns error?Invoked when a GitHub project card event is received.
note

You do not need to implement every callback in a service type. Implement only the GitHub event actions relevant to your integration.

Full usage example

import ballerina/log;
import ballerinax/trigger.github;

configurable string webhookSecret = ?;
configurable int listenerPort = ?;

configurable github:ListenerConfig listenerConfig = {
webhookSecret: webhookSecret
};

listener github:Listener githubListener = new (listenerConfig, listenerPort);

service github:IssuesService on githubListener {
remote function onOpened(github:IssuesEvent payload) returns error? {
log:printInfo("GitHub issue opened",
issue = payload.issue.title,
repository = payload.repository.full_name,
sender = payload.sender.login
);
}

remote function onClosed(github:IssuesEvent payload) returns error? {
log:printInfo("GitHub issue closed",
issue = payload.issue.title,
repository = payload.repository.full_name
);
}
}
note

Each service type uses a different typed payload record. For example, github:IssuesService receives github:IssuesEvent, while github:PushService receives github:PushEvent.

Supporting types

IssuesEvent

FieldTypeDescription
actionIssuesActionsIssue event action.
issueIssueIssue associated with the event.
changesChanges?Changes associated with the issue.
labelLabel?Label associated with the issue event.
assigneeUser?Assignee associated with the issue event.
milestoneMilestone?Milestone associated with the issue event.
repositoryRepositoryRepository where the issue event occurred.
senderUserUser that triggered the issue event.
organizationOrganization?Organization associated with the event, when available.

IssueCommentEvent

FieldTypeDescription
actionIssueCommentActionsIssue comment event action.
issueIssueIssue or pull request associated with the comment.
changesChanges?Changes associated with the issue comment.
commentIssueCommentComment payload.
repositoryRepositoryRepository where the comment event occurred.
senderUserUser that triggered the comment event.
organizationOrganization?Organization associated with the event, when available.

PullRequestEvent

FieldTypeDescription
actionPullRequestActionsPull request event action.
numberintPull request number.
changesChanges?Changes associated with the pull request.
pull_requestPullRequestPull request payload.
assigneeUser?Assignee associated with the pull request event.
labelLabel?Label associated with the pull request event.
requested_reviewerUser?Requested reviewer associated with the pull request event.
repositoryRepositoryRepository where the pull request event occurred.
senderUserUser that triggered the pull request event.
organizationOrganization?Organization associated with the event, when available.

PullRequestReviewEvent

FieldTypeDescription
actionPullRequestReviewActionsPull request review event action.
reviewReviewPull request review payload.
pull_requestPullRequestPull request associated with the review.
changesChanges?Changes associated with the review.
repositoryRepositoryRepository where the review event occurred.
senderUserUser that triggered the review event.
organizationOrganization?Organization associated with the event, when available.

PullRequestReviewCommentEvent

FieldTypeDescription
actionstringPull request review comment event action.
changesChanges?Changes associated with the review comment.
pull_requestPullRequestPull request associated with the review comment.
commentPullRequestReviewCommentReview comment payload.
repositoryRepositoryRepository where the review comment event occurred.
senderUserUser that triggered the review comment event.
organizationOrganization?Organization associated with the event, when available.

ReleaseEvent

FieldTypeDescription
actionReleaseActionsRelease event action.
releaseReleaseRelease payload.
repositoryRepositoryRepository where the release event occurred.
senderUserUser that triggered the release event.
changesChanges?Changes associated with the release event.
organizationOrganization?Organization associated with the event, when available.

PushEvent

FieldTypeDescription
refstringFull Git ref that was pushed.
beforestringSHA before the push.
afterstringSHA after the push.
createdbooleanWhether the push created the ref.
deletedbooleanWhether the push deleted the ref.
forcedbooleanWhether the push was forced.
base_refstring?Base Git ref, when available.
comparestringURL for comparing the before and after commits.
commitsCommit[]Commits included in the push.
head_commitCommit?Head commit for the push.
repositoryRepositoryRepository where the push occurred.
pusherCommitAuthorUser who pushed the commits.
senderUserUser that triggered the push event.
organizationOrganization?Organization associated with the event, when available.

Repository

FieldTypeDescription
idintRepository ID.
namestringRepository name.
full_namestringFull repository name, including owner.
ownerUserRepository owner.
html_urlstringRepository HTML URL.
descriptionstring?Repository description.
default_branchstringDefault branch name.

Issue

FieldTypeDescription
numberintIssue number.
titlestringIssue title.
userUserAuthor of the issue.
labelsLabel[]Labels assigned to the issue.
statestringIssue state.
assigneeUser?Assigned user, when available.
assigneesUser[]Assigned users.
milestoneMilestone?Associated milestone.
commentsintNumber of issue comments.
bodystring?Issue description.

PullRequest

FieldTypeDescription
numberintPull request number.
statestringPull request state.
titlestringPull request title.
userUserAuthor of the pull request.
bodystring?Pull request description.
created_atstringCreation timestamp.
updated_atstringLast update timestamp.
closed_atstring?Close timestamp, when available.
mergedboolean?Whether the pull request was merged.
headBranchHead branch.
baseBranchBase branch.

User

FieldTypeDescription
loginstringGitHub username.
idintUser ID.
avatar_urlstringPublic avatar URL.
html_urlstringGitHub profile URL.
typestringUser type.
site_adminbooleanWhether the user is a site administrator.
namestring?Display name, when available.
emailstring?Email address, when available.