site_hook 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a2249764ae94c0a409237efeaf8d81005f3a2a1e
4
- data.tar.gz: 2539605c765920916cebaf537f41f74e356e5799
3
+ metadata.gz: 77110224a51928243bd5e336829f11f626f1e9fb
4
+ data.tar.gz: 2ca551ebbcdc329502a333ce000a9154507d061a
5
5
  SHA512:
6
- metadata.gz: 8aa433450d38fc526f833bf63429c56b0d4c29d6634afd7f4f1a10e9d39ff7a09661aa60efb3eacac8d3fea17bf2dc8cb90b0001555c21ebf4b40a7f2916f7f6
7
- data.tar.gz: f140857d0b811d1e14d05641821ca6f04388cefc23bb980ed24677acb029e249e385e89be731c70e94c1fe7bdbdc47c1b9dd112acf81941fdac040c2fa9800cc
6
+ metadata.gz: 042c54b34afff7f71922c9d3c733f59fe4aa96fd5d0d9fa6331b7c721c41e8eddc1c67c29678513f6d52cbd1e0bdcd67b0b54735945b4bcdb92c63cd97afae56
7
+ data.tar.gz: fb99e9a7ab3851eba91a5f309e8e1ee58a0878899e51ed061564edd227b64a9ed4b82aa960091d47d975870e6374218155aca25caf96ffac9ef4745da210fb25
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- site_hook (0.1.0)
4
+ site_hook (0.2.0)
5
5
  activesupport (~> 5.1)
6
6
  git (~> 1.3)
7
7
  logging (~> 2.2)
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- require 'thor'
2
+
3
3
  require 'site_hook'
4
- shook = SiteHook::CLI.new(ARGV)
5
- shook.invoke('start')
4
+ SiteHook::CLI.start(ARGV)
@@ -12,12 +12,17 @@ module SiteHook
12
12
  HOOKLOG = SiteHook::HookLogger::HookLog.new(SiteHook.log_levels['hook']).log
13
13
  BUILDLOG = SiteHook::HookLogger::BuildLog.new(SiteHook.log_levels['build']).log
14
14
  APPLOG = SiteHook::HookLogger::AppLog.new(SiteHook.log_levels['app']).log
15
-
16
- set port: 9090
15
+ JPHRC = YAML.load_file(Pathname(Dir.home).join('.jph-rc'))
16
+ set port: JPHRC.fetch('port', 9090)
17
17
  set bind: '127.0.0.1'
18
18
  set server: %w(thin)
19
19
  set quiet: true
20
20
  set raise_errors: true
21
+
22
+ # @param [String] body JSON String of body
23
+ # @param [String] sig Signature or token from git service
24
+ # @param [String] secret User-defined verification token
25
+ # @param [Boolean] plaintext Whether the verification is plaintext
21
26
  def Webhook.verified?(body, sig, secret, plaintext:)
22
27
  if plaintext
23
28
  if sig === secret
@@ -43,8 +48,8 @@ module SiteHook
43
48
  request.body.rewind
44
49
  req_body = request.body.read
45
50
  js = RecursiveOpenStruct.new(JSON.parse(req_body))
46
- jph_rc = YAML.load_file(Pathname(Dir.home).join('.jph-rc'))
47
- projects = jph_rc['projects']
51
+
52
+ projects = JPHRC['projects']
48
53
  begin
49
54
  project = projects.fetch(params[:hook_name])
50
55
  rescue KeyError => e
@@ -72,23 +77,26 @@ module SiteHook
72
77
  end
73
78
  if Webhook.verified?(req_body.to_s, signature, project['hookpass'], plaintext: plaintext)
74
79
  BUILDLOG.info 'Building...'
75
- jekyllbuild = SiteHook::Senders::Jekyll.build(project['src'], project['dst'], logger: BUILDLOG)
76
- if jekyllbuild == 0
80
+ jekyllbuild = SiteHook::Senders::Jekyll.build(project['src'], project['dst'], BUILDLOG)
81
+ jekyll_status = jekyllbuild.fetch(:status, 1) == 0
82
+ case jekyll_status
83
+
84
+ when 0
77
85
  status 200
78
86
  headers 'Content-Type' => 'application/json'
79
87
  body {
80
88
  {'message': 'success'}.to_json
81
89
  }
82
- else
83
- status 404
90
+ when -1, -2, -3
91
+ status 400
84
92
  headers 'Content-Type' => 'application/json'
85
93
  body {
86
- {'message': 'failure'}
94
+ {'message': 'exception', error: "#{jekyll_status.fetch(:message)}"}
87
95
  }
88
96
  end
89
97
 
90
98
  else
91
- halt 403, {'Content-Type' => 'application/json'}, {message: 'incorrect secret'}.to_json
99
+ halt 403, {'Content-Type' => 'application/json'}, {message: 'incorrect secret', status: 1}.to_json
92
100
  end
93
101
  end
94
102
  post '/webhook/?' do
@@ -1,5 +1,6 @@
1
1
  require 'thor'
2
2
 
3
+ require 'site_hook/config_class'
3
4
  module SiteHook
4
5
  def self.log_levels
5
6
  default = {
@@ -35,8 +36,12 @@ module SiteHook
35
36
  desc 'start', 'Start SiteHook'
36
37
 
37
38
  def start
38
- SiteHook.mklogdir
39
+
40
+ SiteHook.mklogdir unless Pathname(Dir.home).join('.jph', 'logs').exist?
39
41
  SiteHook::Webhook.run!
40
42
  end
43
+
44
+ desc 'config SUBCOMMAND [OPTIONS]', 'Configure site_hook options'
45
+ subcommand('config', SiteHook::ConfigClass)
41
46
  end
42
47
  end
@@ -0,0 +1,37 @@
1
+ require 'thor'
2
+ require 'yaml'
3
+ require 'recursive-open-struct'
4
+ module SiteHook
5
+ class ConfigClass < Thor
6
+ YML = open(Pathname(Dir.home).join('.jph-rc'), 'r')
7
+
8
+ desc 'list QUERY [options]', 'List configured options'
9
+
10
+ def list
11
+ puts YML.read
12
+ end
13
+ method_option '-f', type: :boolean, banner: 'FILE', default: false
14
+ desc 'gen [options]', "Generate a example config file if one doesn't exist"
15
+ def gen
16
+ #return if Pathname(Dir.home).join('.jph-rc').exist?
17
+
18
+ yaml = [
19
+ "# fatal, error, warn, info, debug",
20
+ "log_levels:",
21
+ " hook: info",
22
+ " build: info",
23
+ " git: info",
24
+ " app: info",
25
+ "projects:",
26
+ " PROJECT.NAME: # Use the name you put as your webhook url",
27
+ " # https://jekyllhook.example.com/webhook/PROJECT.NAME",
28
+ " src: /path/to/jekyll/site/source # Directory you 'git pull' into",
29
+ " dst: /path/to/build/destination/ # The web root will be this folder",
30
+ " hookpass: SOMERANDOMSTRING # set your Gitlab-Token or GitHub secret to this",
31
+ "",
32
+ ]
33
+
34
+ puts yaml
35
+ end
36
+ end
37
+ end
@@ -62,6 +62,8 @@ module SiteHook
62
62
  LL.debug "#{SiteHook.safe_log_name(LL.class)} initialized."
63
63
 
64
64
  class HookLogger
65
+
66
+ # Log App Actions
65
67
  class AppLog
66
68
  attr :log
67
69
  attr :log_level
@@ -80,6 +82,8 @@ module SiteHook
80
82
  LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
81
83
  end
82
84
  end
85
+
86
+ # Log Hook Actions
83
87
  class HookLog
84
88
  attr :log
85
89
  attr :log_level
@@ -98,6 +102,7 @@ module SiteHook
98
102
  end
99
103
  end
100
104
 
105
+ # Log Build Actions
101
106
  class BuildLog
102
107
  attr :log
103
108
 
@@ -117,6 +122,7 @@ module SiteHook
117
122
  end
118
123
  end
119
124
 
125
+ # Log Git Actions
120
126
  class GitLog
121
127
  attr :log
122
128
 
@@ -134,12 +140,15 @@ module SiteHook
134
140
  LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
135
141
  end
136
142
  end
143
+
144
+ # Fake Logger for GitLog to preprocess output
137
145
  class FakeLog < StringIO
138
146
  attr :info_output, :debug_output
139
147
  def initialize
140
148
  @info_output = []
141
149
  @debug_output = []
142
150
  end
151
+ # @param [Any] message message to log
143
152
  def info(message)
144
153
  case
145
154
  when message =~ /git .* pull/
@@ -149,6 +158,7 @@ module SiteHook
149
158
  @debug_output << message
150
159
  end
151
160
  end
161
+ # @param [Any] message message to log
152
162
  def debug(message)
153
163
  case
154
164
  when message =~ /\n/
@@ -167,6 +177,7 @@ module SiteHook
167
177
  end
168
178
  end
169
179
 
180
+ # @return [Hash] Hash of log entries
170
181
  def entries
171
182
  {
172
183
  info: @info_output,
@@ -11,7 +11,6 @@ module SiteHook
11
11
 
12
12
  def do_grab_version
13
13
  jekyll_source = Jekyll.instance_variable_get('@jekyll_source')
14
- build_dest = Jekyll.instance_variable_get('@build_dest')
15
14
  log = Jekyll.instance_variable_get('@log')
16
15
  begin
17
16
  stdout_str, status = Open3.capture2({'BUNDLE_GEMFILE' => Pathname(jekyll_source).join('Gemfile').to_path}, "jekyll --version --source #{jekyll_source}")
@@ -23,8 +22,8 @@ module SiteHook
23
22
  end
24
23
 
25
24
  def do_pull
26
- fakelog = SiteHook::HookLogger::FakeLog.new
27
- reallog = SiteHook::HookLogger::GitLog.new(SiteHook.log_levels['git']).log
25
+ fakelog = SiteHook::HookLogger::FakeLog.new
26
+ reallog = SiteHook::HookLogger::GitLog.new(SiteHook.log_levels['git']).log
28
27
  jekyll_source = Jekyll.instance_variable_get('@jekyll_source')
29
28
  build_dest = Jekyll.instance_variable_get('@build_dest')
30
29
  g = Git.open(jekyll_source, :log => fakelog)
@@ -37,53 +36,66 @@ module SiteHook
37
36
  def do_build
38
37
  jekyll_source = Jekyll.instance_variable_get('@jekyll_source')
39
38
  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
39
+ log = Jekyll.instance_variable_get('@log')
40
+ 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}") { |in_io, outerr_io, thr|
41
+ pid = thr.pid
44
42
 
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
43
+ outerr = outerr_io.read.lines
44
+ outerr.each do |line|
45
+ line = Paint.unpaint(line)
46
+ line.squish!
47
+ # Configuration file: /home/ken/sites/iotaspencer.me/_config.yml
48
+ # Source: /home/ken/sites/iotaspencer.me
49
+ # Destination: /var/www/iotaspencer.me
50
+ # Incremental build: disabled. Enable with --incremental
51
+ # Generating...
52
+ # GitHub Metadata: No GitHub API authentication could be found. Some fields may be missing or have incorrect data.
53
+ # done in 6.847 seconds.
54
+ # Auto-regeneration: disabled. Use --watch to enable.
55
+ case
56
+ when line =~ /done in .*/
57
+ log.info(line)
58
+ when line =~ /Generating/
59
+ log.info(line)
60
+ when line =~ /Configuration file:|Source:|Destination:/
61
+ log.debug(line)
62
+ when line =~ /Incremental|Auto-regeneration/
63
+ print ''
64
+ else
65
+ log.debug line
69
66
  end
70
- exit_status = thr.value
71
- }
72
- end
67
+ end
68
+ exit_status = thr.value
69
+ }
70
+
73
71
  end
74
72
  end
75
73
 
76
- def self.build(jekyll_source, build_dest, logger:)
74
+ # @param [String,Pathname] jekyll_source Jekyll Source
75
+ # @param [String,Pathname] build_dest Build Destination
76
+ # @param [BuildLog] logger Build Logger Instance
77
+ def self.build(jekyll_source, build_dest, logger)
77
78
  @jekyll_source = jekyll_source
78
79
  @build_dest = build_dest
79
80
  @log = logger
80
81
  instance = self::Build.new
81
82
  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}")
83
+ begin
84
+ meths.each do |m|
85
+ @log.debug("Running #{m}")
86
+ instance.method(m).call
87
+ @log.debug("Ran #{m}")
88
+ end
89
+ return {message: 'success', status: 0}
90
+ rescue TypeError => e
91
+ return {message: "#{e}", status: -1}
92
+ rescue KeyError => e
93
+ return {message: "#{e}", status: -2}
94
+ rescue ArgumentError => e
95
+ return {message: "#{e}", status: -3}
96
+
86
97
  end
98
+
87
99
  end
88
100
  end
89
101
  end
@@ -1,3 +1,3 @@
1
1
  module SiteHook
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: site_hook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Spencer
@@ -214,6 +214,7 @@ files:
214
214
  - bin/site_hook
215
215
  - lib/site_hook.rb
216
216
  - lib/site_hook/cli.rb
217
+ - lib/site_hook/config_class.rb
217
218
  - lib/site_hook/logger.rb
218
219
  - lib/site_hook/sender.rb
219
220
  - lib/site_hook/spinner.rb