Blog Detail

29

Jul
Handle GitHub webhooks in a Laravel Application cover image

Handle GitHub webhooks in a Laravel Application

GitHub can notify your application of events utilizing webhooks. Spatie comes up with a new spatie/laravel-github-webhooks package that can assist you to handle those webhooks.

Unconventionally, it will check the GitHub signature of all incoming requests. All authentic calls will be logged to the database. This package enables you to effortlessly define jobs or events that should be dispatched when particular webhooks hit your application.

Installation

You can install the package via composer:

composer require spatie/laravel-github-webhooks

You've to publish the config file with this command:

php artisan vendor:publish --provider="Spatie\GitHubWebhooks\GitHubWebhooksServiceProvider" --tag="github-webhooks-config"

Usage

After installation, you can start configuring what should come up when GitHub webhooks hit your app. GitHub expects your application to react as fast as possible. When a webhook comes in and it will generate a GitHubWebhookCall model with the payload of the webhook in the database. It will pass that model to a queued job so that an HTTP response can be sent instantly. After that, you can perform the actual work on the queue.

In the job's key of the github-webhooks config file, you can arrange which jobs should be carried out when some events come in.

For example:

// in the `github-webhooks` config file

'jobs' => [
    'issues.opened' => \App\Jobs\HandleIssueOpenedWebhookJob::class,
],

In the above example, the HandleIssueOpenedWebhookJob job will be dispatched when a webhook of type issues comes in with an action of opened in the payload. Now the HandeIssueOpenedWebhookJob could look like this:

namespace App\Jobs\GitHubWebhooks;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\GitHubWebhooks\Models\GitHubWebhookCall;

class HandleIssueOpenedWebhookJob implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    public GitHubWebhookCall $gitHubWebhookCall;

    public function __construct(
        public GitHubWebhookCall $webhookCall
    ) {}

    public function handle()
    {
        // Respond  to the issue opened at the GitHub event here

        // You can approach the payload of the GitHub webhook call with `$this->webhookCall->payload()`
    }
}

You've to follow the above instructions to handle an incoming GitHub webhook in your application.

Closing Notes

spatie/laravel-github-webhooks is an unpretentious kind of package. No new consequential ideas are interpolated here. Still, this package handles enough work for you:

  • Storing the incoming webhook
  • Easy configuration of webhook handling jobs
  • Pruning handle webhooks
  • Sane exception management

If you want to study more about this package you can view its full documentation here GitHub.

Published at : 29-07-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

web application development client
web application development client
web application development client
web application development client
web application development client

Launch project