Using github webhook

Webhooks allow you to build or set up integrations, such as GitHub Apps or OAuth Apps, which subscribe to certain events on GitHub.com. github

  1. Getting started with Github webhook
  2. References

Getting started with Github webhook

  1. Read a Github API
  2. Build a webhook server/script
     <?php
     require(__DIR__ . "/vendor/autoload.php");
     use GitHubWebhook\Handler;
    
     $handler = new Handler("<your secret>", __DIR__);
     if($handler->handle()) {
         echo "OK";
     } else {
         echo "Wrong secret";
     }
    
  3. Register webhook service at github project webhook

    Settings > Webhooks & services > Add webhook

    itemdescription
    Payload URLEnter full URL to your webhook script
    Content typeCan be either “application/json” or “application/x-www-form-urlencoded”
    SecretSame secret you pass to constructor of Handler object

    PHP GitHub webhook handler

    functiondescription
    __construct($secret, $gitDir, $remote = null)Constructor. Constructs new webhook handler that will verify that requests coming to it are signed with $secret. $gitDir must be set to path to git repo that must be updated. Optional $remote specifies which remote should be pulled.
    getData()Getter. After successful validation returns parsed array of data in payload. Otherwise returns null.
    getDelivery()Getter. After successful validation returns unique delivery number coming from GitHub. Otherwise returns null.
    getEvent()Getter. After successful validation returns name of event that triggered this webhook. Otherwise returns null.
    getGitDir()Getter. Returns $gitDir that was passed to constructor.
    getGitOutputGetter. After successful validation returns output of git as array of lines. Otherwise returns null.
    getRemote()Getter. Returns $remote that was passed to constructor.
    getSecret()Getter. Returns $secret that was passed to constructor.
    handle()Handle the request. Validates that incoming request is signed correctly with $secret and executes git pull upon successful validation. Returns true on succes or false if validation failed.
    validate()Validate request only. Returns boolean that indicates whether the request is correctly signed by $secret.

References


© 2018. All rights reserved.

Powered by Hydejack v8.4.0