Code Monkey home page Code Monkey logo

github_webhook's Introduction

Gem Version

GithubWebhook

This gem will help you to quickly setup a route in your Rails application which listens to a GitHub webhook

Installation

Add this line to your application's Gemfile:

gem 'github_webhook', '~> 1.1'

And then execute:

$ bundle install

Configuration

First, configure a route to receive the github webhook POST requests.

# config/routes.rb
resource :github_webhooks, only: :create, defaults: { formats: :json }

Then create a new controller:

# app/controllers/github_webhooks_controller.rb
class GithubWebhooksController < ActionController::Base
  include GithubWebhook::Processor

  # Handle push event
  def github_push(payload)
    # TODO: handle push webhook
  end

  # Handle create event
  def github_create(payload)
    # TODO: handle create webhook
  end

  private

  def webhook_secret(payload)
    ENV['GITHUB_WEBHOOK_SECRET']
  end
end

Add as many instance methods as events you want to handle in your controller.

All events are prefixed with github_. So, a push event can be handled by github_push(payload), or a create event can be handled by github_create(payload), etc.

You can read the full list of events GitHub can notify you about.

Adding the Webhook to your git repository:

First, install octokit, then run a rails console.

$ gem install octokit
$ rails console

In the rails console, add the WebHook to GitHub:

require "octokit"
client = Octokit::Client.new(:login => 'ssaunier', :password => 's3cr3t!!!')

repo = "ssaunier/github_webhook"
callback_url = "yourdomain.com/github_webhooks"
webhook_secret = "a_gr34t_s3cr3t"  # Must be set after that in ENV['GITHUB_WEBHOOK_SECRET']

# Create the WebHook
client.subscribe "https://github.com/#{repo}/events/push.json", callback_url, webhook_secret

The secret is set at the webhook creation. Store it in an environment variable, GITHUB_WEBHOOK_SECRET as per the example. It is important to have such a secret, as it will guarantee that your process legit webhooks requests, thus only from GitHub.

You can have an overview of your webhooks at the following URL:

https://github.com/:username/:repo/settings/hooks

Contributing

Specs

This project uses Appraisal to test against multiple versions of Rails.

On Travis, builds are also run on multiple versions of Ruby, each with multiple versions of Rails.

When you run bundle install, it will use the latest version of Rails. You can then run bundle exec rake spec to run the test with that version of Rails.

To run the specs against each version of Rails, use bundle exec appraisal rake spec.

github_webhook's People

Contributors

ssaunier avatar robindaugherty avatar indirect avatar kledal avatar dennmart avatar probablyfaiz avatar girasquid avatar looneym avatar nicholaskim94 avatar mebezac avatar

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.