Git Product home page Git Product logo

github-webhook-bundle's Introduction

Github WebHook Bundle

Build Status

This bundle aiming to reduce complexity when creating GitHub web hooks apps.

Installation

The recommended way to install this bundle is through Composer:

composer require "swop/github-webhook-bundle"

Then, register the bundle into your AppKernel class. Note that this bundle rely on the SensioFrameworkExtraBundle bundle in order to work properly.

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new Swop\Bundle\GitHubWebHookBundle\GitHubWebHookBundle()
        ];

        // ...
    }
}

Usage

Simply use the GitHubWebHook annotation in your controllers to mark them as GitHub web hook controllers

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Swop\GitHubWebHook\Event\GitHubEvent;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Swop\Bundle\GitHubWebHookBundle\Annotation\GitHubWebHook;

class DefaultController extends Controller
{
    /**
     * @Route("/webhook", name="webhook")
     *
     * @GitHubWebHook(eventType="push")
     * @GitHubWebHook(eventType="pull_request")
     */
    public function indexAction(GitHubEvent $gitHubEvent)
    {
        $payload = $gitHubEvent->getPayload(); // Request payload (array)
        $eventType = $gitHubEvent->getType(); // "pull" or "pull_request"

        // Do something depending on the payload & the event type...
        
        return ['status' => 'success'];
    }
}

By declaring that your controller is a GitHub web hook, the bundle will make the following actions for you:

  • It will check if the incoming web hook event is supported by your controller. If it's not the case, a 400 JSON response will be automatically returned.

  • It will verify that the incoming request has a correct signature, using the web hook specific secret (see "Annotation reference") or the default one (see "Bundle configuration reference"). If the request signature is invalid, a 403 JSON response will be automatically returned.

  • It will build a GitHubEvent object which will be injected in the controller parameters.

  • It will manage the serialization of the data you returned in your controller, and thus will send back a 200 JSON response to GitHub.

Configuration

Bundle configuration reference

github_webhook:
    default_secret: my_secret
  • default_secret: Default secret to use on every hooks when validating the incoming request signature.

Annotation reference

Simple event type handling (will respond only to the push event):

<?php
/**
 * @GitHubWebHook(eventType="push")
 */

Multiple event types handling (will respond to the push and pull_request event):

<?php
/**
 * @GitHubWebHook(eventType="push")
 * @GitHubWebHook(eventType="pull_request")
 */

Configure a dedicated secret to use for signature validation:

<?php
/**
 * @GitHubWebHook(eventType="push", secret="push_secret")
 * @GitHubWebHook(eventType="pull_request", secret="pr_secret")
 */

You also can rely on a container parameter to configure the secret to use:

<?php
/**
 * @GitHubWebHook(eventType="push", secret="%hook.push.secret%")
 */

Contributing

See CONTRIBUTING file.

Original Credits

License

This library is released under the MIT license. See the complete license in the bundled LICENSE file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.