site_hook 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b91ef665dece23dcb7dbc74835e8615686fca06f
4
- data.tar.gz: cac8dd89fd31b748136a4ea995b1477cd90a7cdb
2
+ SHA256:
3
+ metadata.gz: 5879510b2f5e8e1bcddcb9b47657167e3293b04f7695289b97400d0699cac9ad
4
+ data.tar.gz: 130891aeee14e1cf6c145e72469996f1092afa2f0d97ea94894e241e8c51ae14
5
5
  SHA512:
6
- metadata.gz: b8ba7d33d2b7a819f1d3ac2a3fc91640ed548165659372631deece7b23c956beeb3d7592415c84901012578e78b3052bbf8f66a52201c6f607e864a04f4c0d66
7
- data.tar.gz: eb7cde1074571eebb5c96dd673f079cdadf1cc4161fb00c165c90df9a02a4aff20462ce108ee6b49388a0f8aa9dc844eebafbac690f44f1cd5e73c59d19c41d5
6
+ metadata.gz: 322b81d08da7de03383dfe498067dd7caa787066fa7dfdb9b2202a22389fc5f57c314b746ca893dbc8ac4b355d587f8ba32cac99e91d0d3b5c405d88ef878e1e
7
+ data.tar.gz: 73b65c7e1cc997fe6335ffddafa8a62ae1f4e33732bc2954beee17c28b0b676edcfa636e91f69b55aa00be8cd622b5dd5a3b87f3cd342e62595f118fe5b4c9c3
@@ -1,13 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- site_hook (0.2.0)
4
+ site_hook (0.5.0)
5
5
  activesupport (~> 5.1)
6
6
  git (~> 1.3)
7
+ haml (~> 5.0)
7
8
  logging (~> 2.2)
8
9
  paint (~> 2.0)
9
10
  pry (~> 0.11)
10
11
  recursive-open-struct (~> 1.1)
12
+ sass (~> 3.5)
11
13
  sinatra (~> 2.0)
12
14
  sinatra-contrib (~> 2.0)
13
15
  thin (~> 1.7)
@@ -16,19 +18,23 @@ PATH
16
18
  GEM
17
19
  remote: https://rubygems.org/
18
20
  specs:
19
- activesupport (5.1.5)
21
+ activesupport (5.2.0)
20
22
  concurrent-ruby (~> 1.0, >= 1.0.2)
21
- i18n (~> 0.7)
23
+ i18n (>= 0.7, < 2)
22
24
  minitest (~> 5.1)
23
25
  tzinfo (~> 1.1)
24
- backports (3.11.1)
26
+ backports (3.11.2)
25
27
  coderay (1.1.2)
26
28
  concurrent-ruby (1.0.5)
27
29
  daemons (1.2.6)
28
30
  diff-lcs (1.3)
29
31
  eventmachine (1.2.5)
32
+ ffi (1.9.23)
30
33
  git (1.3.0)
31
- i18n (0.9.5)
34
+ haml (5.0.4)
35
+ temple (>= 0.8.0)
36
+ tilt
37
+ i18n (1.0.0)
32
38
  concurrent-ruby (~> 1.0)
33
39
  little-plugger (1.1.4)
34
40
  logging (2.2.2)
@@ -46,6 +52,9 @@ GEM
46
52
  rack-protection (2.0.1)
47
53
  rack
48
54
  rake (10.5.0)
55
+ rb-fsevent (0.10.3)
56
+ rb-inotify (0.9.10)
57
+ ffi (>= 0.5.0, < 2)
49
58
  recursive-open-struct (1.1.0)
50
59
  rspec (3.7.0)
51
60
  rspec-core (~> 3.7.0)
@@ -60,6 +69,11 @@ GEM
60
69
  diff-lcs (>= 1.2.0, < 2.0)
61
70
  rspec-support (~> 3.7.0)
62
71
  rspec-support (3.7.1)
72
+ sass (3.5.6)
73
+ sass-listen (~> 4.0.0)
74
+ sass-listen (4.0.0)
75
+ rb-fsevent (~> 0.9, >= 0.9.4)
76
+ rb-inotify (~> 0.9, >= 0.9.7)
63
77
  sinatra (2.0.1)
64
78
  mustermann (~> 1.0)
65
79
  rack (~> 2.0)
@@ -72,6 +86,7 @@ GEM
72
86
  rack-protection (= 2.0.1)
73
87
  sinatra (= 2.0.1)
74
88
  tilt (>= 1.3, < 3)
89
+ temple (0.8.0)
75
90
  thin (1.7.2)
76
91
  daemons (~> 1.0, >= 1.0.9)
77
92
  eventmachine (~> 1.0, >= 1.0.4)
data/README.md CHANGED
@@ -20,15 +20,11 @@ Or install it yourself as:
20
20
 
21
21
  You'll need to setup a `~/.jph-rc` file and make a `~/.jph/` directory. The file is for runtime configuration, and the directory is used for logging and debugging.
22
22
 
23
- ## Development
24
-
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
-
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
23
+ For more information, see [here](https://iotaspencer.me/projects/site_hook)
28
24
 
29
25
  ## Contributing
30
26
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/site_hook. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
27
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/IotaSpencer/site_hook](https://github.com/IotaSpencer/site_hook). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
32
28
 
33
29
  ## License
34
30
 
@@ -9,16 +9,17 @@ require 'sass'
9
9
  require 'json'
10
10
  require 'sinatra/json'
11
11
  require 'yaml'
12
-
13
12
  module SiteHook
14
13
  module Gem
15
14
  class Info
16
15
  def self.name
17
16
  'site_hook'
18
17
  end
18
+
19
19
  def self.constant_name
20
20
  'SiteHook'
21
21
  end
22
+
22
23
  def self.author
23
24
  %q(Ken Spencer <me@iotaspencer.me>)
24
25
  end
@@ -39,10 +40,10 @@ module SiteHook
39
40
  end
40
41
  end
41
42
  class Webhook < Sinatra::Base
42
- HOOKLOG = SiteHook::HookLogger::HookLog.new(SiteHook.log_levels['hook']).log
43
+ HOOKLOG = SiteHook::HookLogger::HookLog.new(SiteHook.log_levels['hook']).log
43
44
  BUILDLOG = SiteHook::HookLogger::BuildLog.new(SiteHook.log_levels['build']).log
44
- APPLOG = SiteHook::HookLogger::AppLog.new(SiteHook.log_levels['app']).log
45
- JPHRC = YAML.load_file(Pathname(Dir.home).join('.jph-rc'))
45
+ APPLOG = SiteHook::HookLogger::AppLog.new(SiteHook.log_levels['app']).log
46
+ JPHRC = YAML.load_file(Pathname(Dir.home).join('.jph-rc'))
46
47
  set port: JPHRC.fetch('port', 9090)
47
48
  set bind: '127.0.0.1'
48
49
  set server: %w(thin)
@@ -51,23 +52,30 @@ module SiteHook
51
52
  set views: Pathname(app_file).dirname.join('site_hook', 'views')
52
53
  set :public_folder, Pathname(app_file).dirname.join('site_hook', 'static')
53
54
  use SassHandler
55
+ use CoffeeHandler
54
56
  # @param [String] body JSON String of body
55
57
  # @param [String] sig Signature or token from git service
56
58
  # @param [String] secret User-defined verification token
57
59
  # @param [Boolean] plaintext Whether the verification is plaintext
58
- def Webhook.verified?(body, sig, secret, plaintext:)
59
- if plaintext
60
- if sig === secret
60
+ def Webhook.verified?(body, sig, secret, plaintext:, service:) if plaintext
61
+ if sig === secret
62
+ true
63
+ else false
64
+ end
65
+ else case service
66
+ when 'gogs'
67
+ if sig == OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body)
68
+ APPLOG.debug "Secret verified: #{sig} === #{OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body)}"
61
69
  true
62
- else
63
- false
64
70
  end
65
- else
71
+ when 'github'
66
72
  if sig == OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret, body)
67
73
  APPLOG.debug "Secret verified: #{sig} === #{OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret, body)}"
68
74
  true
69
75
  end
70
- end
76
+ end
77
+
78
+ end
71
79
  end
72
80
 
73
81
  get '/' do
@@ -97,15 +105,15 @@ module SiteHook
97
105
  get '/webhook/*' do
98
106
  if params[:splat]
99
107
  pass
100
- else
101
- halt 405, {'Content-Type' => 'application/json'}, {message: 'GET not allowed'}.to_json
108
+ else halt 405, {'Content-Type' => 'application/json'}, {message: 'GET not allowed'}.to_json
102
109
  end
103
110
 
104
111
  end
105
112
  post '/webhook/:hook_name/?' do
113
+ service = nil
106
114
  request.body.rewind
107
115
  req_body = request.body.read
108
- js = RecursiveOpenStruct.new(JSON.parse(req_body))
116
+ js = RecursiveOpenStruct.new(JSON.parse(req_body))
109
117
 
110
118
  projects = JPHRC['projects']
111
119
  begin
@@ -115,49 +123,84 @@ module SiteHook
115
123
  end
116
124
  plaintext = false
117
125
  signature = nil
118
- event = request.env.fetch('HTTP_X_GITLAB_EVENT', nil) || request.env.fetch('HTTP_X_GITHUB_EVENT', nil)
126
+ event = nil
127
+ github = request.env.fetch('HTTP_X_GITHUB_EVENT', nil)
128
+ unless github.nil?
129
+ if github == 'push'
130
+ event = 'push'
131
+ end
132
+ end
133
+ gitlab = request.env.fetch('HTTP_X_GITLAB_EVENT', nil)
134
+ unless gitlab.nil?
135
+ if gitlab == 'push'
136
+ event = 'push'
137
+ end
138
+ end
139
+ gogs = request.env.fetch('HTTP_X_GOGS_EVENT', nil)
140
+ unless gogs.nil?
141
+ if gogs == 'push'
142
+ event = 'push'
143
+
144
+ end
145
+ end
146
+ events = {'github' => github, 'gitlab' => gitlab, 'gogs' => gogs
147
+ }
148
+ events_m_e = events.values.one?
149
+ case events_m_e
150
+ when true
151
+ event = 'push'
152
+ service = events.select { |key, value| value }.keys.first
153
+ when false
154
+ halt 400, {'Content-Type' => 'application/json'}, {message: 'events are mutually exclusive', status: 'failure'
155
+ }.to_json
156
+
157
+ else halt 400, {'Content-Type' => 'application/json'}, {'status': 'failure', 'message': 'something weird happened'
158
+ }
159
+ end
119
160
  if event != 'push'
120
161
  if event.nil?
121
162
  halt 400, {'Content-Type' => 'application/json'}, {message: 'no event header'}.to_json
122
163
  end
123
164
  end
124
- case
125
- when request.env.fetch('HTTP_X_GITLAB_EVENT', nil)
126
- signature = request.env.fetch('HTTP_X_GITLAB_TOKEN', '')
127
- plaintext = true
128
- when request.env.fetch('HTTP_X_GITHUB_EVENT', nil)
129
-
130
- signature = request.env.fetch('HTTP_X_HUB_SIGNATURE', '').sub!(/^sha1=/, '')
131
- plaintext = false
132
- else
133
- APPLOG.debug(request.env.inspect)
165
+ case service
166
+ when 'gitlab'
167
+ signature = request.env.fetch('HTTP_X_GITLAB_TOKEN', '')
168
+ plaintext = true
169
+ when 'github'
170
+ signature = request.env.fetch('HTTP_X_HUB_SIGNATURE', ''
171
+ ).sub!(/^sha1=/, ''
172
+ )
173
+ plaintext = false
174
+
175
+ when 'gogs'
176
+ signature = request.env.fetch('HTTP_X_GOGS_SIGNATURE', '')
177
+ plaintext = false
134
178
  end
135
- if Webhook.verified?(req_body.to_s, signature, project['hookpass'], plaintext: plaintext)
179
+ if Webhook.verified?(req_body.to_s, signature, project['hookpass'], plaintext: plaintext, service: service)
136
180
  BUILDLOG.info 'Building...'
181
+
137
182
  jekyllbuild = SiteHook::Senders::Jekyll.build(project['src'], project['dst'], BUILDLOG)
138
- jekyll_status = jekyllbuild.fetch(:status, 1) == 0
183
+ jekyll_status = jekyllbuild.fetch(:status, 1)
139
184
  case jekyll_status
140
185
 
141
- when 0
142
- status 200
143
- headers 'Content-Type' => 'application/json'
144
- body {
145
- {'message': 'success'}.to_json
146
- }
147
- when -1, -2, -3
148
- status 400
149
- headers 'Content-Type' => 'application/json'
150
- body {
151
- {'message': 'exception', error: "#{jekyll_status.fetch(:message)}"}
152
- }
186
+ when 0
187
+ status 200
188
+ headers 'Content-Type' => 'application/json'
189
+ body { {'status': 'success'}.to_json
190
+ }
191
+ when -1, -2, -3
192
+ status 400
193
+ headers 'Content-Type' => 'application/json'
194
+ body { {'status': 'exception', error: "#{jekyll_status.fetch(:message)}"}
195
+ }
153
196
  end
154
197
 
155
- else
156
- halt 403, {'Content-Type' => 'application/json'}, {message: 'incorrect secret', status: 1}.to_json
198
+ else halt 403, {'Content-Type' => 'application/json'}, {message: 'incorrect secret', 'status': 'failure'}.to_json
157
199
  end
158
200
  end
159
201
  post '/webhook/?' do
160
- halt 403, {'Content-Type' => 'application/json'}, {message: 'pick a hook', error: 'root webhook hit'}.to_json
202
+ halt 403, {'Content-Type' => 'application/json'}, {message: 'pick a hook', error: 'root webhook hit', 'status': 'failure'
203
+ }.to_json
161
204
  end
162
205
  end
163
206
  end
@@ -80,11 +80,15 @@ module SiteHook
80
80
  @log = logger
81
81
  instance = self::Build.new
82
82
  meths = instance.methods.select { |x| x =~ /^do_/ }
83
+ @thrs = []
83
84
  begin
84
- meths.each do |m|
85
- @log.debug("Running #{m}")
86
- instance.method(m).call
87
- @log.debug("Ran #{m}")
85
+ @thrs << Thread.new do
86
+ meths.each do |m|
87
+ @log.debug("Running #{m}")
88
+ instance.method(m).call
89
+ @log.debug("Ran #{m}")
90
+ end
91
+ Thread.exit
88
92
  end
89
93
  return {message: 'success', status: 0}
90
94
  rescue TypeError => e
@@ -1,3 +1,3 @@
1
1
  module SiteHook
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: site_hook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Spencer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-23 00:00:00.000000000 Z
11
+ date: 2018-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -272,7 +272,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
272
272
  version: '0'
273
273
  requirements: []
274
274
  rubyforge_project:
275
- rubygems_version: 2.5.2.1
275
+ rubygems_version: 2.7.6
276
276
  signing_key:
277
277
  specification_version: 4
278
278
  summary: Catch a github webhook and execute a plugin