Skip to content

Instantly share code, notes, and snippets.

@sarguru
Created July 11, 2012 09:15
Show Gist options
  • Save sarguru/3089217 to your computer and use it in GitHub Desktop.
Save sarguru/3089217 to your computer and use it in GitHub Desktop.
sample-sintra-gitlabhook
require 'rubygems' if RUBY_VERSION < '1.9'
require 'sinatra';
require 'json';
require 'mail';
get '/' do
'Minimal Sinatra Hello World!'
end
post "/gitlab" do
request.body.rewind # in case someone already read it
data = JSON.parse request.body.read
#puts data.inspect, data.class
#puts data.keys
puts "Hello #{data['user_name']}"
user_name = data['user_name']
ref = data['ref']
repojson = data['repository']
reponame = repojson['name']
commitarr = data['commits']
nocommit = commitarr.length
last_cjson = commitarr[-1]
cmesg = last_cjson['message']
id = last_cjson['id']
c_url = last_cjson['url']
subject = "The repository #{reponame} , branch #{ref} has been updated by #{user_name}"
message = "#{user_name} says\n #{cmesg}\n You can check the commit at the following url\n #{c_url}\n KTHXBYE!!!"
puts "#{subject}"
puts "#{message}"
mail = Mail.new do
from 'abcd@efgh.in'
to 'xyz@efgh.in'
subject "#{subject}"
body "#{message}"
end
mail.deliver!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment