site_hook 0.8.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.rspec +0 -2
  4. data/.rubocop.yml +2 -0
  5. data/Gemfile +1 -5
  6. data/Rakefile +35 -3
  7. data/bin/site_hook +5 -1
  8. data/features/get_help.feature +3 -0
  9. data/features/step_definitions/get_help_step_definition.rb +1 -0
  10. data/features/step_definitions/my_steps.rb +3 -0
  11. data/features/support/env.rb +1 -0
  12. data/lib/site_hook.rb +2 -28
  13. data/lib/site_hook/cli.rb +22 -39
  14. data/lib/site_hook/commands/config_class.rb +22 -68
  15. data/lib/site_hook/commands/jekyll_class.rb +38 -0
  16. data/lib/site_hook/commands/server_class.rb +38 -34
  17. data/lib/site_hook/config.rb +390 -14
  18. data/lib/site_hook/config_sections.rb +60 -9
  19. data/lib/site_hook/deprecate.rb +34 -11
  20. data/lib/site_hook/env/env.rb +2 -0
  21. data/lib/site_hook/exceptions.rb +26 -2
  22. data/lib/site_hook/log.rb +20 -15
  23. data/lib/site_hook/logger.rb +94 -173
  24. data/lib/site_hook/loggers.rb +10 -0
  25. data/lib/site_hook/loggers/access.rb +20 -0
  26. data/lib/site_hook/loggers/app.rb +78 -0
  27. data/lib/site_hook/loggers/build.rb +77 -0
  28. data/lib/site_hook/loggers/fake.rb +52 -0
  29. data/lib/site_hook/loggers/git.rb +81 -0
  30. data/lib/site_hook/loggers/hook.rb +74 -0
  31. data/lib/site_hook/methods.rb +26 -0
  32. data/lib/site_hook/paths.rb +65 -2
  33. data/lib/site_hook/prelogger.rb +88 -0
  34. data/lib/site_hook/prompt.rb +14 -0
  35. data/lib/site_hook/prompts/prompt_project.rb +48 -0
  36. data/lib/site_hook/runner.rb +36 -0
  37. data/lib/site_hook/sender.rb +4 -9
  38. data/lib/site_hook/string_ext.rb +57 -0
  39. data/lib/site_hook/version.rb +2 -2
  40. data/lib/site_hook/webhook.rb +120 -129
  41. data/site_hook.gemspec +39 -22
  42. data/spec/spec_helper.rb +13 -0
  43. data/spec/string_ext_spec.rb +10 -0
  44. metadata +87 -85
  45. data/CODE_OF_CONDUCT.md +0 -74
  46. data/bin/console +0 -14
  47. data/lib/site_hook/assets/css/styles.css +0 -26
  48. data/lib/site_hook/commands/debug_class.rb +0 -29
  49. data/lib/site_hook/config_sections/cli.rb +0 -24
  50. data/lib/site_hook/config_sections/log_levels.rb +0 -15
  51. data/lib/site_hook/config_sections/projects.rb +0 -22
  52. data/lib/site_hook/const.rb +0 -10
  53. data/lib/site_hook/gem.rb +0 -26
  54. data/lib/site_hook/persist.rb +0 -14
  55. data/lib/site_hook/prompts/gen_config.rb +0 -48
  56. data/lib/site_hook/spinner.rb +0 -19
  57. data/lib/site_hook/views/layout.haml +0 -24
  58. data/lib/site_hook/views/webhooks.haml +0 -7
@@ -0,0 +1,88 @@
1
+ #
2
+ # Copyright 2019 Ken Spencer / IotaSpencer <me@iotaspencer.me>
3
+ #
4
+ # File: /lib/site_hook/prelogger.rb
5
+ # Created: 3/9/19
6
+ #
7
+ # License is in project root, MIT License is in use.
8
+
9
+ require 'site_hook/paths'
10
+ module SiteHook
11
+ class PreLogger
12
+ def initialize(input, output, errput)
13
+ self.class.set_base_default
14
+ @@levels = {
15
+ unknown: ::Logger::UNKNOWN,
16
+ fatal: ::Logger::FATAL,
17
+ error: ::Logger::ERROR,
18
+ info: ::Logger::INFO,
19
+ debug: ::Logger::DEBUG
20
+ }
21
+ @@loggers = {
22
+ stdout: ::Logger.new(STDOUT, progname: @@base),
23
+ stderr: ::Logger.new(STDERR, progname: @@base),
24
+ file: ::Logger.new(SiteHook::Paths.make_log_name(self.to_s), progname: @@base)
25
+ }
26
+ @@loggers.each do |_logger, obj|
27
+ obj.datetime_format = '%Y-%m-%dT%H:%M:%S%Z'
28
+ obj.formatter = proc do |severity, datetime, progname, msg|
29
+ "#{severity} [#{datetime}] #{progname} —— #{msg}\n"
30
+ end
31
+ end
32
+ end
33
+ def self.base=(base)
34
+ @@base = base.to_s
35
+ end
36
+ def self.set_base_default
37
+ @@base = 'Logger'
38
+ end
39
+ def self.unknown(obj)
40
+ @@loggers.each do |_key, value|
41
+ value.unknown(obj)
42
+ end
43
+ end
44
+
45
+ def self.error(obj)
46
+ @@loggers.each do |_key, value|
47
+ value.error(obj)
48
+ end
49
+ end
50
+
51
+ def self.info(obj)
52
+ @@loggers.each do |key, value|
53
+ next if key == :stderr
54
+ value.info(obj)
55
+ end
56
+ end
57
+
58
+ def self.debug(obj)
59
+ @@loggers.each do |_key, value|
60
+ value.debug(obj)
61
+ end
62
+ end
63
+ def self.fatal(obj)
64
+ @@loggers.each do |key, value|
65
+ next if key == :stderr
66
+ value.fatal(obj)
67
+ end
68
+ end
69
+
70
+
71
+
72
+ # @param [Symbol] level log level to log at
73
+ # @param [Object] obj some kind of object or msg to log
74
+ def self.log(level, obj)
75
+ @@loggers.each do |logger|
76
+ logger.add(@levels[level], obj)
77
+ end
78
+ end
79
+
80
+ def self.<<(msg)
81
+ @@loggers.each do |logger|
82
+ logger.<<(msg)
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,14 @@
1
+ module SiteHook
2
+ class Prompt
3
+ def self.inherited(base)
4
+ base.class_eval do
5
+ Prompt.class_variable_set(:'@@runnable', runnable)
6
+ end
7
+ end
8
+ define_singleton_method :runnable do |mlist|
9
+ puts mlist
10
+ end
11
+ def self.run
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,48 @@
1
+ ##
2
+ #
3
+ # Copyright 2019 Ken Spencer / IotaSpencer
4
+ #
5
+ #
6
+ # File: prompt_project.rb
7
+ # Created: 2/15/19
8
+ #
9
+ # License is in project root, MIT License is in use.
10
+ ##
11
+
12
+ require 'highline'
13
+ require 'site_hook/prompt'
14
+ module SiteHook
15
+ module Prompts
16
+ class Project < ::SiteHook::Prompt
17
+ runnable [:prompt_name, :prompt_src_path]
18
+ @@hl = HighLine.new($stdin, $stdout, 0, 0, 0, 0)
19
+ desc 'Prompts for project details'
20
+ def prompt_name
21
+ @@hl.say(<<~STATEMENT)
22
+ What's the name of the project?
23
+ STATEMENT
24
+ @@hl.choose do |menu|
25
+ menu.confirm = 'Are you sure? '
26
+ menu.select_by = :index_or_name
27
+ menu.index = '*'
28
+ menu.prompt = '> '
29
+ menu.flow = :rows
30
+ menu.default = Pathname.new(`pwd`).basename.to_s.chomp!
31
+ menu.choice(Pathname.new(`pwd`).basename.to_s.chomp!) do |answer|
32
+ @project_name = answer
33
+ end
34
+ menu.choice('Custom / Input your own?') do
35
+ @project_name = @@hl.ask('> ', String) do |q|
36
+ q.confirm = true
37
+ end
38
+ end
39
+ end
40
+ end
41
+ def prompt_src_path
42
+ @@hl.say(<<~STATEMENT)
43
+ What's the src path?
44
+ STATEMENT
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,36 @@
1
+ require 'site_hook'
2
+ require 'site_hook/logger'
3
+ module SiteHook
4
+ class Runner
5
+ def initialize(argv = ARGV, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel)
6
+ @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
7
+ end
8
+ def execute!
9
+ exit_code = begin
10
+ $stderr = @stderr
11
+ $stdin = @stdin
12
+ $stdout = @stdout
13
+ SiteHook::PreLogger.new($stdin, $stdout, $stderr)
14
+ SiteHook::Config.new
15
+ SiteHook::Log.new($stdin, $stdout, $stderr)
16
+ SiteHook::CLI.start(@argv)
17
+ 0
18
+
19
+ rescue StandardError => e
20
+ b = e.backtrace
21
+ STDERR.puts("#{b.shift}: #{e.message} (#{e.class})")
22
+ STDERR.puts(b.map{|s| "\tfrom #{s}"}.join("\n"))
23
+ 1
24
+ rescue SystemExit => e
25
+ STDERR.puts e.status
26
+ 1
27
+ ensure
28
+
29
+ $stderr = STDERR
30
+ $stdin = STDIN
31
+ $stdout = STDOUT
32
+ end
33
+ @kernel.exit(exit_code)
34
+ end
35
+ end
36
+ end
@@ -2,10 +2,7 @@ require 'open3'
2
2
  require 'site_hook/logger'
3
3
  require 'git'
4
4
  require 'paint'
5
- require 'site_hook/persist'
6
5
  module SiteHook
7
- autoload :Logs, 'site_hook/log'
8
-
9
6
  module Senders
10
7
  class Jekyll
11
8
  attr :jekyll_source, :build_dest
@@ -30,8 +27,8 @@ module SiteHook
30
27
  end
31
28
 
32
29
  def do_pull
33
- fakelog = SiteHook::HookLogger::FakeLog.new
34
- reallog = SiteHook::HookLogger::GitLog.new(SiteHook::Logs.log_levels['git']).log
30
+ fakelog = SiteHook::Log.fake
31
+ reallog = SiteHook::Log.git
35
32
  jekyll_source = Jekyll.instance_variable_get(JEKYLL_SOURCE_VAR)
36
33
  # build_dest = Jekyll.instance_variable_get('@build_dest')
37
34
  g = Git.open(jekyll_source, log: fakelog)
@@ -46,7 +43,7 @@ module SiteHook
46
43
  build_dest = Jekyll.instance_variable_get('@build_dest')
47
44
  log = Jekyll.instance_variable_get('@log')
48
45
  Open3.popen2e({'BUNDLE_GEMFILE' => Pathname(jekyll_source).join('Gemfile').to_path}, "bundle exec jekyll build --source #{Pathname(jekyll_source).realdirpath.to_path} --destination #{Pathname(build_dest).to_path} --config #{Pathname(jekyll_source).join(@options[:config])}") { |in_io, outerr_io, thr|
49
- # pid = thr.pid
46
+ pid = thr.pid
50
47
 
51
48
  outerr = outerr_io.read.lines
52
49
  outerr.each do |line|
@@ -89,12 +86,10 @@ module SiteHook
89
86
  @log = logger
90
87
  @options = options
91
88
  instance = self::Build.new(options)
92
- meths = [instance.do_grab_version, instance.do_pull, instance.do_build]
89
+ meths = [:do_grab_version, :do_pull, :do_build]
93
90
  begin
94
91
  meths.each do |m|
95
- @log.debug("Running #{m}")
96
92
  instance.send(m)
97
- @log.debug("Ran #{m}")
98
93
  end
99
94
  return {message: 'success', status: 0}
100
95
  rescue TypeError => e
@@ -0,0 +1,57 @@
1
+ class String
2
+ def squish!
3
+ strip!
4
+ gsub!(/\s+/, ' ')
5
+ self
6
+ end
7
+ def squish
8
+ dup.squish!
9
+ end
10
+ def underscore!
11
+ self unless /[A-Z-]|::/.match?(self)
12
+ self.to_s.gsub!("::".freeze, "/".freeze)
13
+ self.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
14
+ self.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
15
+ self.tr!("-".freeze, "_".freeze)
16
+ self.downcase!
17
+ self
18
+ end
19
+ def underscore
20
+ dup.underscore!
21
+ end
22
+ def camelcase!
23
+ to_s.scan(/\w+/).collect(&:capitalize).join
24
+ end
25
+ def camelcase
26
+ dup.camelcase!
27
+ end
28
+ def camelize!
29
+ to_s.split(/_|\s+/).collect(&:capitalize).join
30
+ end
31
+ def camelize
32
+ dup.camelize!
33
+ end
34
+ def safe_log_name
35
+ self.split('::').last.underscore
36
+ end
37
+ end
38
+ module SiteHook
39
+ module StrExt
40
+ def StrExt.mkvar(inspection)
41
+ inspection.to_s.tr('.', '_').tr(' ', '_')
42
+ end
43
+ def StrExt.mkatvar(inspection)
44
+ inspection.dup.to_s.insert(0, '@').to_sym
45
+ end
46
+ def StrExt.mkatatvar(inspection)
47
+ inspection.to_s.insert(0, '@').insert(0, '@').to_sym
48
+ end
49
+ def StrExt.rematvar(inspection)
50
+ inspection.to_s.tr('@', '')
51
+ end
52
+
53
+ def self.mkmvar(inspection)
54
+ inspection.to_s.tr('@', '').to_sym
55
+ end
56
+ end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module SiteHook
2
- VERSION = "0.8.2"
3
- end
2
+ VERSION = '0.9.3'
3
+ end
@@ -1,28 +1,22 @@
1
1
  ##########
2
- # -> File: /home/ken/RubymineProjects/site_hook/lib/site_hook/webhook.rb
2
+ # -> File: /site_hook/lib/site_hook/webhook.rb
3
3
  # -> Project: site_hook
4
4
  # -> Author: Ken Spencer <me@iotaspencer.me>
5
5
  # -> Last Modified: 1/10/2018 21:35:44
6
6
  # -> Copyright (c) 2018 Ken Spencer
7
7
  # -> License: MIT
8
8
  ##########
9
- require 'site_hook/persist'
10
- require 'site_hook/const'
11
- require 'sinatra'
9
+ require 'rack'
10
+ require 'site_hook/logger'
11
+ require 'json'
12
+ require 'grape'
13
+ require 'grape-route-helpers'
12
14
 
13
15
  module SiteHook
14
- class Webhook < Sinatra::Base
15
-
16
- set server: %w[thin]
17
- set quiet: true
18
- set raise_errors: true
19
- set views: Pathname(SiteHook::Paths.lib_dir).join('site_hook', 'views')
20
- set :public_folder, Pathname(SiteHook::Paths.lib_dir).join('site_hook', 'assets')
21
- use CoffeeHandler
22
- def self.set_options(host, port)
23
- self.set bind: host.to_s
24
- self.set port: port.to_i
25
- end
16
+ class Server < Grape::API
17
+ version nil
18
+ prefix ''
19
+ format :json
26
20
 
27
21
  #
28
22
  # @param [String] body JSON String of body
@@ -36,142 +30,139 @@ module SiteHook
36
30
  else
37
31
  case service
38
32
  when 'gogs'
39
- if sig == OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body)
40
- APPLOG.debug "Secret verified: #{sig} === #{OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body)}"
33
+ if sig == OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body.chomp)
34
+ SiteHook::Log.app.debug "Secret verified: #{sig} === #{OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body)}"
41
35
  true
42
36
  end
43
37
  when 'github'
44
- if sig == OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret, body)
45
- APPLOG.debug "Secret verified: #{sig} === #{OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret, body)}"
38
+ if sig == OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret, body.chomp)
39
+ SiteHook::Log.app.debug "Secret verified: #{sig} === #{OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret, body)}"
46
40
  true
47
41
  end
48
42
  else
49
43
  # This shouldn't happen
50
44
  end
51
-
52
45
  end
53
46
  end
54
47
 
55
- CONTENT_TYPE = 'Content-Type'
56
- get '/' do
57
- halt 403, {CONTENT_TYPE => 'text/html'}, '<h1>See <a href="/webhooks/">here</a> for the active webhooks</h1>'
58
- end
59
-
60
- APPLICATION_JSON = 'application/json'
61
- get '/webhooks.json', provides: :json do
62
- content_type APPLICATION_JSON
63
- public_projects = JPHRC['projects'].select do |_project, hsh|
64
- !hsh.fetch('private')
48
+ helpers do
49
+ def halt(status, message, headers)
50
+ error!(message, status, headers)
65
51
  end
66
- result = {}
67
- public_projects.each do |project, hsh|
68
- result[project] = {}
69
- hsh.delete('hookpass')
70
- result[project].merge!(hsh)
71
- end
72
- headers CONTENT_TYPE => APPLICATION_JSON, 'Accept' => APPLICATION_JSON
73
- json result, layout: false
74
52
  end
75
-
76
- get '/webhooks/?' do
77
- haml :webhooks, locals: {'projects' => JPHRC['projects']}
53
+ APPLICATION_JSON = 'application/json'
54
+ before do
55
+ remote_addr = request.env['REMOTE_ADDR']
56
+ cf_connecting_ip = request.env.fetch('HTTP_CF_CONNECTING_IP', nil)
57
+ ip = cf_connecting_ip || remote_addr
58
+ SiteHook::Log.access.log "#{ip} - #{request.request_method} #{request.path}"
78
59
  end
79
-
80
- get '/webhook/*' do
81
- if params[:splat]
82
- pass
83
- else
84
- halt 405, {CONTENT_TYPE => APPLICATION_JSON}, {message: 'GET not allowed'}.to_json
85
- end
60
+ after do
61
+ SiteHook::Log.access.log "#{status}"
86
62
  end
87
- post '/webhook/:hook_name/?' do
88
- service = nil
89
- request.body.rewind
90
- req_body = request.body.read
91
- js = RecursiveOpenStruct.new(JSON.parse(req_body))
92
-
93
- projects = JPHRC['projects']
94
- project = projects.fetch(params[:hook_name], nil)
95
- if project.nil?
96
- halt 404, {CONTENT_TYPE => APPLICATION_JSON}, {message: 'no such project', status: 1}.to_json
97
- end
98
- plaintext = false
99
- signature = nil
100
- event = nil
101
- gogs = request.env.fetch('HTTP_X_GOGS_EVENT', nil)
102
- unless gogs.nil?
103
- event = 'push' if gogs == 'push'
104
- end
105
- github = request.env.fetch('HTTP_X_GITHUB_EVENT', nil)
106
- unless github.nil?
107
- event = 'push' if github == 'push'
108
- end
109
- gitlab = request.env.fetch('HTTP_X_GITLAB_EVENT', nil)
110
- unless gitlab.nil?
111
- event = 'push' if gitlab == 'push'
112
- end
113
-
114
- events = {'github' => github, 'gitlab' => gitlab, 'gogs' => gogs}
115
- if events['github'] && events['gogs']
116
- events['github'] = nil
117
- end
118
- events_m_e = events.values.one?
119
- case events_m_e
120
- when true
121
- event = 'push'
122
- service = events.select { |_key, value| value }.keys.first
123
- when false
124
- halt 400, {CONTENT_TYPE: APPLICATION_JSON}, {message: 'events are mutually exclusive', status: 'failure'}.to_json
125
-
126
- else
127
- halt 400,
128
- {CONTENT_TYPE: APPLICATION_JSON},
129
- 'status': 'failure', 'message': 'something weird happened'
130
- end
131
- if event != 'push' && event.nil?
132
- halt 400, {CONTENT_TYPE: APPLICATION_JSON}, {message: 'no event header'}.to_json
133
- end
134
- case service
135
- when 'gitlab'
136
- signature = request.env.fetch('HTTP_X_GITLAB_TOKEN', '')
137
- plaintext = true
138
- when 'github'
139
- signature = request.env.fetch('HTTP_X_HUB_SIGNATURE', '').sub!(/^sha1=/, '')
140
- plaintext = false
63
+ resource :webhook do
64
+ route_param :hook_name do
65
+ get do
66
+ STDOUT.puts params[:hook_name]
67
+ project = SiteHook::Config.projects.find_project(StrExt.mkvar(params[:hook_name]))
68
+ if project.nil?
69
+ {message: 'project not found or private', status: 1, project: {}}
70
+ else
71
+ project_obj = {}
72
+ %i[src dst repo host].each do |option|
73
+ project_obj[option] = project.instance_variable_get(StrExt.mkvar(option))
74
+ end
75
+ {project: project_obj}
76
+ end
77
+ end
78
+ post do
79
+ service = nil
80
+ request.body.rewind
81
+ req_body = request.body.read
82
+ project = SiteHook::Config.projects.get(StrExt.mkvar(params[:hook_name]))
83
+ CONTENT_TYPE = 'Content-Type'
84
+ if project == :not_found
85
+ halt 404, {message: 'no such project', status: 1}.to_json, {CONTENT_TYPE => APPLICATION_JSON}
86
+ elsif project == :no_projects
87
+ halt 500, {message: 'no projects defined', status: 2}.to_json, {CONTENT_TYPE => APPLICATION_JSON}
88
+ end
89
+ plaintext = false
90
+ signature = nil
91
+ event = nil
92
+ gogs = request.env.fetch('HTTP_X_GOGS_EVENT', nil)
93
+ unless gogs.nil?
94
+ event = 'push' if gogs == 'push'
95
+ end
96
+ github = request.env.fetch('HTTP_X_GITHUB_EVENT', nil)
97
+ unless github.nil?
98
+ event = 'push' if github == 'push'
99
+ end
100
+ gitlab = request.env.fetch('HTTP_X_GITLAB_EVENT', nil)
101
+ unless gitlab.nil?
102
+ event = 'push' if gitlab == 'push'
103
+ end
141
104
 
142
- when 'gogs'
143
- signature = request.env.fetch('HTTP_X_GOGS_SIGNATURE', '')
144
- plaintext = false
145
- else
146
- # This shouldn't happen
147
- end
148
- if Webhook.verified?(req_body.to_s, signature, project['hookpass'], plaintext: plaintext, service: service)
149
- BUILDLOG.info 'Building...'
150
- begin
151
- jekyll_status = SiteHook::Senders::Jekyll.build(project['src'], project['dst'], BUILDLOG, options: {config: project['config']})
152
- case jekyll_status
105
+ events = {'github' => github, 'gitlab' => gitlab, 'gogs' => gogs}
106
+ if events['github'] && events['gogs']
107
+ events['github'] = nil
108
+ end
109
+ events_m_e = events.values.one?
110
+ case events_m_e
111
+ when true
112
+ event = 'push'
113
+ service = events.select { |_key, value| value }.keys.first
114
+ when false
115
+ halt 400, {message: 'events are mutually exclusive', status: 'failure'}.to_json, {CONTENT_TYPE => APPLICATION_JSON}
153
116
 
154
- when 0
155
- status 200
156
- headers CONTENT_TYPE => APPLICATION_JSON
157
- body { {'status': 'success'}.to_json }
158
- when -1, -2, -3
159
- halt 400, {CONTENT_TYPE => APPLICATION_JSON}, {'status': 'exception', error: jekyll_status.fetch(:message).to_s}
117
+ else
118
+ halt 400, {'status': 'failure', 'message': 'something weird happened'}, {CONTENT_TYPE => APPLICATION_JSON}
119
+ end
120
+ if event != 'push' && event.nil?
121
+ halt 400, {message: 'no event header', status: 'failure'}.to_json, {CONTENT_TYPE => APPLICATION_JSON}
122
+ end
123
+ case service
124
+ when 'gitlab'
125
+ signature = request.env.fetch('HTTP_X_GITLAB_TOKEN', '')
126
+ plaintext = true
127
+ when 'github'
128
+ signature = request.env.fetch('HTTP_X_HUB_SIGNATURE', '').sub!(/^sha1=/, '')
129
+ plaintext = false
130
+
131
+ when 'gogs'
132
+ signature = request.env.fetch('HTTP_X_GOGS_SIGNATURE', '')
133
+ plaintext = false
160
134
  else
161
135
  # This shouldn't happen
162
136
  end
163
-
164
- rescue => e
165
- halt 500, {CONTENT_TYPE => APPLICATION_JSON}, {'status': 'exception', error: e.to_s}
166
-
137
+ if Server.verified?(req_body.to_s, signature, project.hookpass, plaintext: plaintext, service: service)
138
+ SiteHook::Log.build.info 'Building...'
139
+ begin
140
+ jekyll_status = SiteHook::Senders::Jekyll.build(project.src, project.dst, SiteHook::Log.build, options: {config: project.config})
141
+ case jekyll_status[:status]
142
+
143
+ when 0
144
+ status 200
145
+ {status: 'success'}
146
+ when -1, -2, -3
147
+ halt 400, {'status': 'exception', error: jekyll_status.fetch(:message).to_s}, {CONTENT_TYPE => APPLICATION_JSON}
148
+ else
149
+ # This shouldn't happen
150
+ end
151
+ rescue => e
152
+ halt 500, {'status': 'exception', error: e.to_s}, {CONTENT_TYPE => APPLICATION_JSON}
153
+ end
154
+ else
155
+ halt 403, {message: 'incorrect secret', 'status': 'failure'}.to_json, {CONTENT_TYPE => APPLICATION_JSON}
156
+ end
167
157
  end
168
-
169
- else
170
- halt 403, {CONTENT_TYPE => APPLICATION_JSON}, {message: 'incorrect secret', 'status': 'failure'}.to_json
171
158
  end
172
159
  end
173
- post '/webhook/?' do
174
- halt 403, {CONTENT_TYPE => APPLICATION_JSON}, {message: 'pick a hook', error: 'root webhook hit', 'status': 'failure'}.to_json
160
+
161
+ resource :webhooks do
162
+ get do
163
+ SiteHook::Config.projects.to_h
164
+ end
165
+
175
166
  end
176
167
  end
177
168
  end