site_hook 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2249764ae94c0a409237efeaf8d81005f3a2a1e
4
+ data.tar.gz: 2539605c765920916cebaf537f41f74e356e5799
5
+ SHA512:
6
+ metadata.gz: 8aa433450d38fc526f833bf63429c56b0d4c29d6634afd7f4f1a10e9d39ff7a09661aa60efb3eacac8d3fea17bf2dc8cb90b0001555c21ebf4b40a7f2916f7f6
7
+ data.tar.gz: f140857d0b811d1e14d05641821ca6f04388cefc23bb980ed24677acb029e249e385e89be731c70e94c1fe7bdbdc47c1b9dd112acf81941fdac040c2fa9800cc
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea/
10
+ .vscode/
11
+ /vendor
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at me@iotaspencer.me. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in site_hook.gemspec
6
+ gemspec
@@ -0,0 +1,95 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ site_hook (0.1.0)
5
+ activesupport (~> 5.1)
6
+ git (~> 1.3)
7
+ logging (~> 2.2)
8
+ paint (~> 2.0)
9
+ pry (~> 0.11)
10
+ recursive-open-struct (~> 1.1)
11
+ sinatra (~> 2.0)
12
+ sinatra-contrib (~> 2.0)
13
+ thin (~> 1.7)
14
+ thor (~> 0.20)
15
+
16
+ GEM
17
+ remote: https://rubygems.org/
18
+ specs:
19
+ activesupport (5.1.5)
20
+ concurrent-ruby (~> 1.0, >= 1.0.2)
21
+ i18n (~> 0.7)
22
+ minitest (~> 5.1)
23
+ tzinfo (~> 1.1)
24
+ backports (3.11.1)
25
+ coderay (1.1.2)
26
+ concurrent-ruby (1.0.5)
27
+ daemons (1.2.6)
28
+ diff-lcs (1.3)
29
+ eventmachine (1.2.5)
30
+ git (1.3.0)
31
+ i18n (0.9.5)
32
+ concurrent-ruby (~> 1.0)
33
+ little-plugger (1.1.4)
34
+ logging (2.2.2)
35
+ little-plugger (~> 1.1)
36
+ multi_json (~> 1.10)
37
+ method_source (0.9.0)
38
+ minitest (5.11.3)
39
+ multi_json (1.13.1)
40
+ mustermann (1.0.2)
41
+ paint (2.0.1)
42
+ pry (0.11.3)
43
+ coderay (~> 1.1.0)
44
+ method_source (~> 0.9.0)
45
+ rack (2.0.4)
46
+ rack-protection (2.0.1)
47
+ rack
48
+ rake (10.5.0)
49
+ recursive-open-struct (1.1.0)
50
+ rspec (3.7.0)
51
+ rspec-core (~> 3.7.0)
52
+ rspec-expectations (~> 3.7.0)
53
+ rspec-mocks (~> 3.7.0)
54
+ rspec-core (3.7.1)
55
+ rspec-support (~> 3.7.0)
56
+ rspec-expectations (3.7.0)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.7.0)
59
+ rspec-mocks (3.7.0)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.7.0)
62
+ rspec-support (3.7.1)
63
+ sinatra (2.0.1)
64
+ mustermann (~> 1.0)
65
+ rack (~> 2.0)
66
+ rack-protection (= 2.0.1)
67
+ tilt (~> 2.0)
68
+ sinatra-contrib (2.0.1)
69
+ backports (>= 2.0)
70
+ multi_json
71
+ mustermann (~> 1.0)
72
+ rack-protection (= 2.0.1)
73
+ sinatra (= 2.0.1)
74
+ tilt (>= 1.3, < 3)
75
+ thin (1.7.2)
76
+ daemons (~> 1.0, >= 1.0.9)
77
+ eventmachine (~> 1.0, >= 1.0.4)
78
+ rack (>= 1, < 3)
79
+ thor (0.20.0)
80
+ thread_safe (0.3.6)
81
+ tilt (2.0.8)
82
+ tzinfo (1.2.5)
83
+ thread_safe (~> 0.1)
84
+
85
+ PLATFORMS
86
+ ruby
87
+
88
+ DEPENDENCIES
89
+ bundler (~> 1.16)
90
+ rake (~> 10.0)
91
+ rspec (~> 3.0)
92
+ site_hook!
93
+
94
+ BUNDLED WITH
95
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Ken Spencer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # SiteHook
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ `gem 'site_hook'`
8
+
9
+ And then execute:
10
+
11
+ `$ bundle`
12
+
13
+ Or install it yourself as:
14
+
15
+ `gem install site_hook --user-install # as a user`
16
+ `sudo gem install site_hook # as a sudoer`
17
+ `gem install site_hook # as root`
18
+
19
+ ## Usage
20
+
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
+
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).
28
+
29
+ ## Contributing
30
+
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.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the SiteHook project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/site_hook/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "site_hook"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
12
+
13
+ #require "irb"
14
+ #IRB.start(__FILE__)
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'thor'
3
+ require 'site_hook'
4
+ shook = SiteHook::CLI.new(ARGV)
5
+ shook.invoke('start')
@@ -0,0 +1,98 @@
1
+ require 'site_hook/version'
2
+ require 'site_hook/sender'
3
+ require 'site_hook/logger'
4
+ require 'recursive-open-struct'
5
+ require 'site_hook/cli'
6
+ require 'sinatra'
7
+ require 'json'
8
+ require 'yaml'
9
+
10
+ module SiteHook
11
+ class Webhook < Sinatra::Base
12
+ HOOKLOG = SiteHook::HookLogger::HookLog.new(SiteHook.log_levels['hook']).log
13
+ BUILDLOG = SiteHook::HookLogger::BuildLog.new(SiteHook.log_levels['build']).log
14
+ APPLOG = SiteHook::HookLogger::AppLog.new(SiteHook.log_levels['app']).log
15
+
16
+ set port: 9090
17
+ set bind: '127.0.0.1'
18
+ set server: %w(thin)
19
+ set quiet: true
20
+ set raise_errors: true
21
+ def Webhook.verified?(body, sig, secret, plaintext:)
22
+ if plaintext
23
+ if sig === secret
24
+ true
25
+ else
26
+ false
27
+ end
28
+ else
29
+ if sig == OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret, body)
30
+ APPLOG.debug "Secret verified: #{sig} === #{OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, secret, body)}"
31
+ true
32
+ end
33
+ end
34
+ end
35
+
36
+ get '/' do
37
+ halt 403, {'Content-Type' => 'application/json'}, {message: 'no permission'}.to_json
38
+ end
39
+ get '/webhook/?' do
40
+ halt 405, {'Content-Type' => 'application/json'}, {message: 'GET not allowed'}.to_json
41
+ end
42
+ post '/webhook/:hook_name' do
43
+ request.body.rewind
44
+ req_body = request.body.read
45
+ js = RecursiveOpenStruct.new(JSON.parse(req_body))
46
+ jph_rc = YAML.load_file(Pathname(Dir.home).join('.jph-rc'))
47
+ projects = jph_rc['projects']
48
+ begin
49
+ project = projects.fetch(params[:hook_name])
50
+ rescue KeyError => e
51
+ halt 404, {'Content-Type' => 'application/json'}, {message: 'no such project', status: 1}.to_json
52
+ end
53
+ plaintext = false
54
+ signature = nil
55
+ event = request.env.fetch('HTTP_X_GITLAB_EVENT', nil) || request.env.fetch('HTTP_X_GITHUB_EVENT', nil)
56
+ APPLOG.info event.inspect
57
+ if event != 'push'
58
+ if event.nil?
59
+ halt 400, {'Content-Type' => 'application/json'}, {message: 'no event header'}.to_json
60
+ end
61
+ end
62
+ case
63
+ when request.env.fetch('HTTP_X_GITLAB_EVENT', nil)
64
+ signature = request.env.fetch('HTTP_X_GITLAB_TOKEN', '')
65
+ plaintext = true
66
+ when request.env.fetch('HTTP_X_GITHUB_EVENT', nil)
67
+
68
+ signature = request.env.fetch('HTTP_X_HUB_SIGNATURE', '').sub!(/^sha1=/, '')
69
+ plaintext = false
70
+ else
71
+ APPLOG.debug(request.env.inspect)
72
+ end
73
+ if Webhook.verified?(req_body.to_s, signature, project['hookpass'], plaintext: plaintext)
74
+ BUILDLOG.info 'Building...'
75
+ jekyllbuild = SiteHook::Senders::Jekyll.build(project['src'], project['dst'], logger: BUILDLOG)
76
+ if jekyllbuild == 0
77
+ status 200
78
+ headers 'Content-Type' => 'application/json'
79
+ body {
80
+ {'message': 'success'}.to_json
81
+ }
82
+ else
83
+ status 404
84
+ headers 'Content-Type' => 'application/json'
85
+ body {
86
+ {'message': 'failure'}
87
+ }
88
+ end
89
+
90
+ else
91
+ halt 403, {'Content-Type' => 'application/json'}, {message: 'incorrect secret'}.to_json
92
+ end
93
+ end
94
+ post '/webhook/?' do
95
+ halt 403, {'Content-Type' => 'application/json'}, {message: 'pick a hook', error: 'root webhook hit'}.to_json
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,42 @@
1
+ require 'thor'
2
+
3
+ module SiteHook
4
+ def self.log_levels
5
+ default = {
6
+ 'hook' => 'info',
7
+ 'build' => 'info',
8
+ 'git' => 'info',
9
+ 'app' => 'info'
10
+ }
11
+ begin
12
+ log_level = YAML.load_file(Pathname(Dir.home).join('.jph-rc')).fetch('log_levels')
13
+ if log_level
14
+ log_level
15
+ end
16
+ rescue KeyError
17
+ default
18
+ rescue Errno::ENOENT
19
+ default
20
+ end
21
+ end
22
+
23
+ class CLI < Thor
24
+ map %w[--version -v] => :__print_version
25
+ desc '--version, -v', 'Print the version'
26
+
27
+ # Prints version string
28
+ # @return [NilClass] nil
29
+ def __print_version
30
+ puts SiteHook::VERSION
31
+ end
32
+
33
+ method_option(:log_levels, type: :hash, banner: 'LEVELS', default: SiteHook.log_levels)
34
+
35
+ desc 'start', 'Start SiteHook'
36
+
37
+ def start
38
+ SiteHook.mklogdir
39
+ SiteHook::Webhook.run!
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,178 @@
1
+ require 'paint'
2
+ require 'logging'
3
+ require 'active_support/core_ext/string'
4
+
5
+ Logging.init %w(NONE DEBUG INFO WARN ERROR FATAL)
6
+ Logging.color_scheme(
7
+ 'bright',
8
+ :levels => {
9
+ :debug => [:yellow, :on_white],
10
+ :info => :blue,
11
+ :warn => :yellow,
12
+ :error => :red,
13
+ :fatal => [:red, :on_white],
14
+ },
15
+ :date => :white,
16
+ :logger => :cyan,
17
+ :message => :green,
18
+ )
19
+
20
+ layout = Logging.layouts.pattern \
21
+ :pattern => '[%d] %-5l %c: %m\n',
22
+ :date_pattern => '%Y-%m-%d %H:%M:%S',
23
+ :color_scheme => 'bright'
24
+
25
+ Logging.appenders.stdout \
26
+ :layout => layout
27
+
28
+ module SiteHook
29
+ def mklogdir
30
+ path = Pathname(Dir.home).join('.jph', 'logs')
31
+ if path.exist?
32
+ else
33
+ FileUtils.mkpath(path.to_s)
34
+ end
35
+ end
36
+
37
+ def safe_log_name(klass)
38
+ klass.class.name.split('::').last.underscore
39
+ end
40
+
41
+ module_function :mklogdir, :safe_log_name
42
+
43
+ class LogLogger
44
+ attr :log
45
+ attr :log_level
46
+
47
+ def initialize(log_level = 'info') # only change this to debug when testing
48
+ @log = Logging.logger[SiteHook.safe_log_name(self)]
49
+ @log_level = log_level
50
+
51
+ flayout = Logging.appenders.rolling_file \
52
+ Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
53
+ :age => 'daily',
54
+ :pattern => '[%d] %-5l %c: %m\n'
55
+ @log.add_appenders 'stdout', flayout
56
+ @log.level = log_level
57
+ end
58
+ end
59
+
60
+ mklogdir
61
+ LL = LogLogger.new.log
62
+ LL.debug "#{SiteHook.safe_log_name(LL.class)} initialized."
63
+
64
+ class HookLogger
65
+ class AppLog
66
+ attr :log
67
+ attr :log_level
68
+
69
+ def initialize(log_level = nil)
70
+ LL.debug "Initializing #{SiteHook.safe_log_name(self)}"
71
+ @log = Logging.logger[SiteHook.safe_log_name(self)]
72
+ @log_level = log_level
73
+
74
+ flayout = Logging.appenders.rolling_file \
75
+ Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
76
+ :age => 'daily',
77
+ :pattern => '[%d] %-5l %c: %m\n'
78
+ @log.add_appenders 'stdout', flayout
79
+ @log.level = log_level
80
+ LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
81
+ end
82
+ end
83
+ class HookLog
84
+ attr :log
85
+ attr :log_level
86
+
87
+ def initialize(log_level = nil)
88
+ LL.debug "Initializing #{SiteHook.safe_log_name(self)}"
89
+ @log = Logging.logger[SiteHook.safe_log_name(self)]
90
+ @log_level = log_level
91
+ flayout = Logging.appenders.rolling_file \
92
+ Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
93
+ :age => 'daily',
94
+ :pattern => '[%d] %-5l %c: %m\n'
95
+ @log.add_appenders 'stdout', flayout
96
+ @log.level = @log_level
97
+ LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
98
+ end
99
+ end
100
+
101
+ class BuildLog
102
+ attr :log
103
+
104
+ def initialize(log_level = nil)
105
+ LL.debug "Initializing #{SiteHook.safe_log_name(self)}"
106
+ @log = Logging.logger[SiteHook.safe_log_name(self)]
107
+ @log_level = log_level
108
+
109
+ flayout = Logging.appenders.rolling_file \
110
+ Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
111
+ :age => 'daily',
112
+ :pattern => '[%d] %-5l %c: %m\n'
113
+ @log.add_appenders 'stdout', flayout
114
+ @log.level = log_level
115
+
116
+ LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
117
+ end
118
+ end
119
+
120
+ class GitLog
121
+ attr :log
122
+
123
+ def initialize(log_level = nil)
124
+ LL.debug "Initializing #{SiteHook.safe_log_name(self)}"
125
+ @log = Logging.logger[SiteHook.safe_log_name(self)]
126
+ @log_level = log_level
127
+
128
+ flayout = Logging.appenders.rolling_file \
129
+ Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
130
+ :age => 'daily',
131
+ :pattern => '[%d] %-5l %c: %m\n'
132
+ @log.add_appenders 'stdout', flayout
133
+ @log.level = log_level
134
+ LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
135
+ end
136
+ end
137
+ class FakeLog < StringIO
138
+ attr :info_output, :debug_output
139
+ def initialize
140
+ @info_output = []
141
+ @debug_output = []
142
+ end
143
+ def info(message)
144
+ case
145
+ when message =~ /git .* pull/
146
+ @info_output << "Starting Git"
147
+ @debug_output << message
148
+ else
149
+ @debug_output << message
150
+ end
151
+ end
152
+ def debug(message)
153
+ case
154
+ when message =~ /\n/
155
+ msgs = message.lines
156
+ msgs.each do |msg|
157
+ msg.squish!
158
+ case
159
+ when msg =~ /From (.*?):(.*?)\/(.*)(\.git)?/
160
+ @info_output << "Pulling via #{$2}/#{$3} on #{$1}."
161
+ when msg =~ /\* branch (.*?) -> .*/
162
+ @info_output << "Using #{$1} branch"
163
+ end
164
+ end
165
+ else
166
+ @debug_output << message
167
+ end
168
+ end
169
+
170
+ def entries
171
+ {
172
+ info: @info_output,
173
+ debug: @debug_output
174
+ }
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,90 @@
1
+ require 'open3'
2
+ require 'site_hook/logger'
3
+ require 'git'
4
+ require 'paint'
5
+ module SiteHook
6
+ module Senders
7
+ class Jekyll
8
+ attr :jekyll_source, :build_dest
9
+
10
+ class Build
11
+
12
+ def do_grab_version
13
+ jekyll_source = Jekyll.instance_variable_get('@jekyll_source')
14
+ build_dest = Jekyll.instance_variable_get('@build_dest')
15
+ log = Jekyll.instance_variable_get('@log')
16
+ begin
17
+ stdout_str, status = Open3.capture2({'BUNDLE_GEMFILE' => Pathname(jekyll_source).join('Gemfile').to_path}, "jekyll --version --source #{jekyll_source}")
18
+ log.info("Jekyll Version: #{stdout_str.chomp!}")
19
+ rescue Errno::ENOENT
20
+ log.fatal('Jekyll not installed! Gem and Webhook will not function')
21
+ Process.kill('INT', Process.pid)
22
+ end
23
+ end
24
+
25
+ def do_pull
26
+ fakelog = SiteHook::HookLogger::FakeLog.new
27
+ reallog = SiteHook::HookLogger::GitLog.new(SiteHook.log_levels['git']).log
28
+ jekyll_source = Jekyll.instance_variable_get('@jekyll_source')
29
+ build_dest = Jekyll.instance_variable_get('@build_dest')
30
+ g = Git.open(jekyll_source, :log => fakelog)
31
+ g.pull
32
+ fakelog.entries.each do |level, entries|
33
+ entries.each { |entry| reallog.send("#{level}", entry) }
34
+ end
35
+ end
36
+
37
+ def do_build
38
+ jekyll_source = Jekyll.instance_variable_get('@jekyll_source')
39
+ build_dest = Jekyll.instance_variable_get('@build_dest')
40
+ log = Jekyll.instance_variable_get('@log')
41
+ begin
42
+ Open3.popen2e({'BUNDLE_GEMFILE' => Pathname(jekyll_source).join('Gemfile').to_path}, "bundle exec jekyll build --source #{Pathname(jekyll_source).to_path} --destination #{Pathname(build_dest).to_path}") { |in_io, outerr_io, thr|
43
+ pid = thr.pid
44
+
45
+ outerr = outerr_io.read.lines
46
+ outerr.each do |line|
47
+ line = Paint.unpaint(line)
48
+ line.squish!
49
+ # Configuration file: /home/ken/sites/iotaspencer.me/_config.yml
50
+ # Source: /home/ken/sites/iotaspencer.me
51
+ # Destination: /var/www/iotaspencer.me
52
+ # Incremental build: disabled. Enable with --incremental
53
+ # Generating...
54
+ # GitHub Metadata: No GitHub API authentication could be found. Some fields may be missing or have incorrect data.
55
+ # done in 6.847 seconds.
56
+ # Auto-regeneration: disabled. Use --watch to enable.
57
+ case
58
+ when line =~ /done in .* seconds/
59
+ log.info(line)
60
+ when line =~ /Generating/
61
+ log.info(line)
62
+ when line =~ /Configuration file:|Source:|Destination:/
63
+ log.debug(line)
64
+ when line =~ /Incremental|Auto-regeneration/
65
+ print ''
66
+ else
67
+ print ''
68
+ end
69
+ end
70
+ exit_status = thr.value
71
+ }
72
+ end
73
+ end
74
+ end
75
+
76
+ def self.build(jekyll_source, build_dest, logger:)
77
+ @jekyll_source = jekyll_source
78
+ @build_dest = build_dest
79
+ @log = logger
80
+ instance = self::Build.new
81
+ meths = instance.methods.select { |x| x =~ /^do_/ }
82
+ meths.each do |m|
83
+ @log.debug("Running #{m}")
84
+ instance.method(m).call
85
+ @log.debug("Ran #{m}")
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,19 @@
1
+ module SiteHook
2
+ module_function
3
+ def show_wait_spinner(fps = 10)
4
+ chars = %w[| / - \\]
5
+ delay = 1.0 / fps
6
+ iter = 0
7
+ spinner = Thread.new do
8
+ while iter do # Keep spinning until told otherwise
9
+ print chars[(iter += 1) % chars.length]
10
+ sleep delay
11
+ print "\b"
12
+ end
13
+ end
14
+ yield.tap {# After yielding to the block, save the return value
15
+ iter = false # Tell the thread to exit, cleaning up after itself…
16
+ spinner.join # …and wait for it to do so.
17
+ } # Use the block's return value as the method's
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module SiteHook
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'site_hook/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'site_hook'
8
+ spec.version = SiteHook::VERSION
9
+ spec.authors = ['Ken Spencer']
10
+ spec.email = ['me@iotaspencer.me']
11
+
12
+ spec.summary = %q{Catch a github webhook and execute a plugin}
13
+ spec.homepage = 'https://iotaspencer.me/projects/site_hook/'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['source_uri'] = 'https://github.com/IotaSpencer/site_hook'
20
+ end
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
23
+ f.match(%r{^(spec)/})
24
+ end
25
+ spec.bindir = 'bin'
26
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+ spec.add_runtime_dependency 'sinatra', '~> 2.0'
29
+ spec.add_runtime_dependency 'sinatra-contrib', '~> 2.0'
30
+ spec.add_runtime_dependency 'thor', '~> 0.20'
31
+ spec.add_runtime_dependency 'paint', '~> 2.0'
32
+ spec.add_runtime_dependency 'git', '~> 1.3'
33
+ spec.add_runtime_dependency 'logging', '~> 2.2'
34
+ spec.add_runtime_dependency 'pry', '~> 0.11'
35
+ spec.add_runtime_dependency 'thin', '~> 1.7'
36
+ spec.add_runtime_dependency 'activesupport', '~> 5.1'
37
+ spec.add_runtime_dependency 'recursive-open-struct', '~> 1.1'
38
+ spec.add_development_dependency 'bundler', '~> 1.16'
39
+ spec.add_development_dependency 'rake', '~> 10.0'
40
+ spec.add_development_dependency 'rspec', '~> 3.0'
41
+ end
metadata ADDED
@@ -0,0 +1,247 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: site_hook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ken Spencer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sinatra-contrib
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.20'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.20'
55
+ - !ruby/object:Gem::Dependency
56
+ name: paint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: git
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: logging
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.11'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.11'
111
+ - !ruby/object:Gem::Dependency
112
+ name: thin
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.7'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.7'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activesupport
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '5.1'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '5.1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: recursive-open-struct
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.1'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.1'
153
+ - !ruby/object:Gem::Dependency
154
+ name: bundler
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.16'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.16'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rake
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '10.0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '10.0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rspec
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '3.0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '3.0'
195
+ description:
196
+ email:
197
+ - me@iotaspencer.me
198
+ executables:
199
+ - console
200
+ - site_hook
201
+ extensions: []
202
+ extra_rdoc_files: []
203
+ files:
204
+ - ".gitignore"
205
+ - ".rspec"
206
+ - ".travis.yml"
207
+ - CODE_OF_CONDUCT.md
208
+ - Gemfile
209
+ - Gemfile.lock
210
+ - LICENSE.txt
211
+ - README.md
212
+ - Rakefile
213
+ - bin/console
214
+ - bin/site_hook
215
+ - lib/site_hook.rb
216
+ - lib/site_hook/cli.rb
217
+ - lib/site_hook/logger.rb
218
+ - lib/site_hook/sender.rb
219
+ - lib/site_hook/spinner.rb
220
+ - lib/site_hook/version.rb
221
+ - site_hook.gemspec
222
+ homepage: https://iotaspencer.me/projects/site_hook/
223
+ licenses:
224
+ - MIT
225
+ metadata:
226
+ source_uri: https://github.com/IotaSpencer/site_hook
227
+ post_install_message:
228
+ rdoc_options: []
229
+ require_paths:
230
+ - lib
231
+ required_ruby_version: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - ">="
234
+ - !ruby/object:Gem::Version
235
+ version: '0'
236
+ required_rubygems_version: !ruby/object:Gem::Requirement
237
+ requirements:
238
+ - - ">="
239
+ - !ruby/object:Gem::Version
240
+ version: '0'
241
+ requirements: []
242
+ rubyforge_project:
243
+ rubygems_version: 2.5.2.1
244
+ signing_key:
245
+ specification_version: 4
246
+ summary: Catch a github webhook and execute a plugin
247
+ test_files: []