lib/site_hook.rb in site_hook-0.2.0 vs lib/site_hook.rb in site_hook-0.3.0
- old
+ new
@@ -10,16 +10,21 @@
module SiteHook
class Webhook < Sinatra::Base
HOOKLOG = SiteHook::HookLogger::HookLog.new(SiteHook.log_levels['hook']).log
BUILDLOG = SiteHook::HookLogger::BuildLog.new(SiteHook.log_levels['build']).log
APPLOG = SiteHook::HookLogger::AppLog.new(SiteHook.log_levels['app']).log
-
- set port: 9090
+ JPHRC = YAML.load_file(Pathname(Dir.home).join('.jph-rc'))
+ set port: JPHRC.fetch('port', 9090)
set bind: '127.0.0.1'
set server: %w(thin)
set quiet: true
set raise_errors: true
+
+ # @param [String] body JSON String of body
+ # @param [String] sig Signature or token from git service
+ # @param [String] secret User-defined verification token
+ # @param [Boolean] plaintext Whether the verification is plaintext
def Webhook.verified?(body, sig, secret, plaintext:)
if plaintext
if sig === secret
true
else
@@ -41,12 +46,12 @@
end
post '/webhook/:hook_name' do
request.body.rewind
req_body = request.body.read
js = RecursiveOpenStruct.new(JSON.parse(req_body))
- jph_rc = YAML.load_file(Pathname(Dir.home).join('.jph-rc'))
- projects = jph_rc['projects']
+
+ projects = JPHRC['projects']
begin
project = projects.fetch(params[:hook_name])
rescue KeyError => e
halt 404, {'Content-Type' => 'application/json'}, {message: 'no such project', status: 1}.to_json
end
@@ -70,26 +75,29 @@
else
APPLOG.debug(request.env.inspect)
end
if Webhook.verified?(req_body.to_s, signature, project['hookpass'], plaintext: plaintext)
BUILDLOG.info 'Building...'
- jekyllbuild = SiteHook::Senders::Jekyll.build(project['src'], project['dst'], logger: BUILDLOG)
- if jekyllbuild == 0
+ jekyllbuild = SiteHook::Senders::Jekyll.build(project['src'], project['dst'], BUILDLOG)
+ jekyll_status = jekyllbuild.fetch(:status, 1) == 0
+ case jekyll_status
+
+ when 0
status 200
headers 'Content-Type' => 'application/json'
body {
{'message': 'success'}.to_json
}
- else
- status 404
+ when -1, -2, -3
+ status 400
headers 'Content-Type' => 'application/json'
body {
- {'message': 'failure'}
+ {'message': 'exception', error: "#{jekyll_status.fetch(:message)}"}
}
end
else
- halt 403, {'Content-Type' => 'application/json'}, {message: 'incorrect secret'}.to_json
+ halt 403, {'Content-Type' => 'application/json'}, {message: 'incorrect secret', status: 1}.to_json
end
end
post '/webhook/?' do
halt 403, {'Content-Type' => 'application/json'}, {message: 'pick a hook', error: 'root webhook hit'}.to_json
end